Problem with database

fanfalveto

Active Member
Licensed User
Longtime User
I created a database with a table (table1) with Sqlite expert, but with the following code tells me the table1 does not exist, if I open the database with Sqlite is okay.
Please if you can help me.
thanks
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("culturilla")
SQL1.Initialize(File.DirInternal, "base.db", True)
la1=SQL1.ExecQuery("SELECT pregunta FROM table1 WHERE id=1")
Label1.Text=la1
End Sub
 

klaus

Expert
Licensed User
Longtime User
You must be careful:
How many results are you expecting.
- SQL.ExecQuery returns a Cursor object with multiple results
- SQL.ExecQuerySingleResult returns a single value

I think in your case you should use SQL.ExecQuerySingleResult.

Best regards.
 
Upvote 0

fanfalveto

Active Member
Licensed User
Longtime User
The problem is the same with ExecQuerySingleResult,this is the log:
android.database.sqlite.SQLiteException: no such table: table1: , while compiling: SELECT pregunta FROM table1 WHERE id=1
but table1 exist,sure.I can see the table in the database with sqlite expert.
Thanks
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
I haven't looked at your project but did you add the file to your project in the b4a ide? and you must, in your code, copy the database to a writeable location.

Search the forum for DBUtils, this helps simplify the process a bit.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
barx is right, you didn't copy the database to the File.DirInternal folder.
This code works having the database file in the Files folder of the project:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("culturilla")
    If FirstTime = True AND File.Exists(File.DirInternal, "base.db") = False Then
        File.Copy(File.DirAssets, "base.db", File.DirInternal, "base.db")
    End If
    
    SQL1.Initialize(File.DirInternal, "base.db", True)
    
    la1=SQL1.ExecQuerySingleResult("SELECT pregunta FROM table1 WHERE id=1")
    
    Label1.Text=la1
End Sub
 
Upvote 0
Top