Android Tutorial New Easy to Use DataBase Functions V1.0 with Samples

* * * UPDATE * * *

July 8th, 2012 - New DataBase Class http://www.b4x.com/forum/additional-libraries-classes-official-updates/22118-class-databases.html



JUST POSTED - The New Easy DataBase V1.6 Functions with Samples and Full Working App! - September 3rd, 2011 5:00 PM

Just added some new date functions per request to Version 1.6. The 1.6 sample is attached below. It now includes these new date functions:

db.Date() --- Returns the current date
db.DateNew("09/03/2011", 30) --- Returns a new date from any selected date, enter positive or negative numbers as second parameter
db.DateNOD("09/03/2011", "03/11/2009") --- Returns the number of days between any two dates passed to it

See Post # 24 for other details: http://www.b4x.com/forum/basic4andr...sy-use-database-functions-v1-0-samples-3.html


Hello All. I just added Version 1.2 to this post. I found a bug that the db.pointer did not update correctly depending on the sorted results in the db.AddRecord and db.UpdateRecord functions. This has been corrected. I also made all functions not return any exceptions if you called them and the database didn't exist. It returns a Toast message stating that the file could not be found.

*** Older Post ***
I Just added Version 1.1. It has a few new things added and the variable names corrected to be more in line with the other functions. In version 1.1:

db.Pointer
db.RecordCount
db.FilePath
db.FileName
db.Field

I have also moved all variable declarations out of the main project. This leaves a totally clean main module to work in. All support for the database is now in the code module. I have attached a new Zip of version 1.1 but I also left the original. I changed over to 1.1 just this afternoon and went through it pretty quick. I feel it is tested and working just as it should, but I went through it quick. Please let me know if you find any errors or issues and I will fix them.

*** Original Post Below **************************************

Hello All,

No extra libraries are needed, just the CORE B4A library. I have tested some of the functions on my NookColor with up to 2500 records. With 2500 records, it moves very fast and searches are instant. All data stored will be in string format, however, you will be able to store links to images, web pages, etc. I wrote and posted the Strings functions here: http://www.b4x.com/forum/basic4android-getting-started-tutorials/10365-string-functions.html, and these functions will work in a similar way.

The functions and sample code are now attached. Some benefits may be that it is easier for new users to learn and less overhead on the processor. You don't have to learn SQL, etc. and the code required for each function is much less. Each device will have its limit on the number of records you can have before the system slows down, based on memory and processor. This will do just fine for most apps and contacts style programs. If you need to save images, objects, etc. directly into the database this will not work for you. You can have as many fields of any size as B4A and Android will support. It will also include the s.GenerateRecords(1000) functions, so you can populate the database with hundreds or thousands of records to test how the app performs under max load. Also, the database handles the pointer for you, it also handles BOF, EOF and no records in the database. It makes your coding job much easier!

If you have problems or questions, please post them and I will try to help. Hope you find it useful!:sign0089:

___________________________________________________________________
db.AddRecord()

db.DeleteRecord()

db.Exist()

db.FindRecord()

db.GenerateRecords()

db.GetRecord()

db.GetStructure()

db.ListRecords()

db.UpdateRecord()

db.FirstRecord()

db.LastRecord()

db.PreviousRecord()

db.NextRecord()

*** Added in Version 1.1 ***

db.Pointer

db.RecordCount

db.FilePath

db.FileName

db.Field

___________________________________________________________________


Thanks,

Margret

If you are using a version less than 1.2, please download the newest version, db.pointer issues were found in previous version(s).
 

Attachments

  • ADR.DataBaseProgram_1.5.zip
    12.6 KB · Views: 1,842
  • ADR.DataBaseSample_1.6.zip
    9.3 KB · Views: 2,239
Last edited:

Merlot2309

Active Member
Licensed User
Longtime User
Hello Margret,

Great job. I'm sure that this is very, very helpfull for many people, like me.

:sign0188:

Helen.
 

Rui

Member
Licensed User
Longtime User
Thank you Margret.

I have been looking into your code. Is there a statement missing in the deleteRecord Sub ?

B4X:
If Main.MySel < MyList.Size -1 Then
Else
         Main.MySel = MyList.Size -1
End If
 

ZJP

Active Member
Licensed User
Longtime User
Great. It reminds me of the good old days of Borland dBase 2.

JP
 

margret

Well-Known Member
Licensed User
Longtime User
About Questions or Problems

Thank you Margret.

I have been looking into your code. Is there a statement missing in the deleteRecord Sub ?

B4X:
If Main.MySel < MyList.Size -1 Then
Else
         Main.MySel = MyList.Size -1
End If

Nothing is missing. The code is as it was intended. At one point I had a message box for debugging and later removed it. Thanks for checking.

Thanks,

Margret
 
Last edited:

awama

Active Member
Licensed User
Longtime User
Encrypt Database?

Hello Margret,

Very Good! I'm sure that this is very helpfull for many people.

Do you have an idea how to encrypt the database. That's what I need.
I think it is not possible in MySQL.

Many thanks.
Walter
 

margret

Well-Known Member
Licensed User
Longtime User
Encrypt database

Hello Margret,

Very Good! I'm sure that this is very helpfull for many people.

Do you have an idea how to encrypt the database. That's what I need.
I think it is not possible in MySQL.

Many thanks.
Walter

Are you wanting to do the whole database or just certain fields?

Margret
 

myriaddev

Active Member
Licensed User
Longtime User
Many different DBs in same app ?

How do I add several different DBs into the same app ?
Thanks, Jerry

P.S. Thanks, I love it!
 

margret

Well-Known Member
Licensed User
Longtime User
Changing DBs

How do I add several different DBs into the same app ?
Thanks, Jerry

P.S. Thanks, I love it!

Hello Jerry,

You can just set and define the structure of as many database files as you need for your app. I would define them in subs. Then when you want to change databases, just call the selected sub. The Code Module, if you are using Ver 1.1, is defined for 20 fields. You can change it if you need to set it larger or you can reduce it if you like. See sample below:

B4X:
Sub Set_db1   
   db.Pointer = 0      
   db.FilePath = File.DirInternal & "/"      
   db.FileName = "mycontacts.dat"            
   db.Field(0,0) = "name"      :db.Field(0,1)=35   
   db.Field(1,0) = "address"   :db.Field(1,1)=35   
   db.Field(2,0) = "city"      :db.Field(2,1)=20   
   db.Field(3,0) = "state"      :db.Field(3,1)=2   
   db.Field(4,0) = "zip"      :db.Field(4,1)=5   
   db.Field(5,0) = "phone"      :db.Field(5,1)=14   
   db.Field(6,0) = ""      :db.Field(6,1)=0   
   'Last entry should have field name empty "" and length set to 0
End Sub   
Sub Set_db2   
   db.Pointer = 0      
   db.FilePath = File.DirInternal & "/"      
   db.FileName = "mychecking.dat"            
   db.Field(0,0) = "CK No"      :db.Field(0,1)=6   
   db.Field(1,0) = "paid to"   :db.Field(1,1)=35   
   db.Field(2,0) = "memo"      :db.Field(2,1)=20   
   db.Field(3,0) = "date"       :db.Field(3,1)=8   
   db.Field(4,0) = "cleared"   :db.Field(4,1)=1   
   db.Field(5,0) = ""      :db.Field(5,1)=0   
        'Last entry should have field name empty "" and length set to 0
End Sub

If you want to keep the pointer position in your current database, you can save the db.Pointer to a memory variable. Then just call Set_db2. Once the db.FileName changes it starts working with that database and the new defined db.Field(s). You don't have to set the db.FilePath in the subs if they are both using the same one. The only thing you really have to change is the db.FileName and the db.Field(s) info. Hope this helps.

Thanks,

Margret
 
Last edited:

margret

Well-Known Member
Licensed User
Longtime User
UPDATE to Easy DataBase

UPDATE - The DataBase V1.2 Functions & Samples are Attached to Post #1! - August 4th, 2011 1:39 PM

Hello All. I just added Version 1.2 to the first post in this thread. I found a bug that the db.pointer did not update correctly depending on the sorted results in the db.AddRecord and db.UpdateRecord functions. This has been corrected. I also made all functions not return any exceptions if you called them and the database didn't exist. It returns a Toast message stating that the file could not be found. ;)

Thanks,

Margret
 
Top