Android Question SQL - How to Read 1 Record that was Added by Another B4A Program

Bradley James Marks

Member
Licensed User
Longtime User
All,

I just started to experiment with SQL in B4A

Based on one of the examples in the B4A documentation, I have a very simple test program that writes one record to an SQL table and then reads this one record. This simple program works nicely. Here is the code.

~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

‘ Write one record to a database table and then read it and display it

Dim SQL1 As SQL


Dim Cursor1 As Cursor


SQL1.Initialize(File.DirDefaultExternal, "test1.db", True)


SQL1.ExecNonQuery("Drop Table if Exists Table1")


SQL1.ExecNonQuery("Create Table Table1 (Column1 Text)")


SQL1.ExecNonQuery("INSERT INTO Table1 Values ('Test Record 1')")


Cursor1 = SQL1.ExecQuery("Select Column1 from Table1")


Cursor1.Position = 0


Msgbox (Cursor1.GetString("Column1"),"Record from Database")


Cursor1.Close

~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

I then wrote a second very small program to simply read the one record that the first program wrote to the database. When I run this program I receive this error message in the log file “android.database.sqlite.SQLiteException: no such table: Table1: , while compiling: Select Column1 from Table1”


The code for this second program is shown below. I must be missing something. I have looked at other examples in the documentation, but I have not been able to find the missing piece.


Thanks for your assistance.

Brad


~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~

‘ Obtain a single record from a database table that was added by another program

Dim SQL1 As SQL


Dim Cursor1 As Cursor


SQL1.Initialize(File.DirDefaultExternal, "test1.db", True)


Cursor1 = SQL1.ExecQuery("Select Column1 from Table1")


Cursor1.Position = 0


Msgbox (Cursor1.GetString("Column1"),"Record from Database")


Cursor1.Close

~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
 

Bradley James Marks

Member
Licensed User
Longtime User
Erel,

Thanks for your quick reply.

B4X:
Log(File.DirDefaultExternal)

yields this from the first program.

/mnt/sdcard/Android/data/b4a.example/files


B4X:
Log(File.DirDefaultExternal)

yields this from the second program.


/mnt/sdcard/Android/data/b4a.example/files


They appear to be the same.

I am confused.

Thanks,

Brad

PS. I am testing via AVD in case that makes a difference.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@Brad: You cannot give the two apps the same package name. Use , say b4a.example.bradone for the first one as a package name and for the second program use say: b4a.example.bradtwo. Otherwise they overwrite each other. Then, if you want to display the data using the 2nd program from the first program, use this in your 2nd program code:

B4X:
SQL1.Initialize("/mnt/sdcard/Android/data/b4a.example.bradone/files", "test1.db", True)
Cursor1 = SQL1.ExecQuery("Select Column1 from Table1")
Cursor1.Position = 0
Msgbox (Cursor1.GetString("Column1"),"Record from Database")
Cursor1.Close
 
Upvote 0
Top