Android Question SQLite Connection Error

YongTheBeginner

New Member
Good day. I am new to B4A and I want to create an application with SQLite database. I set up the DB Browser properly and added the sql file to the file manager in b4a. But the problem is I always encounter this error "android.database.sqlite.SQLiteException: no such table: user_table (code 1 SQLITE_ERROR): , while compiling: select * from user_table". Here is the code that i follow

Sub Service_Create
'This is the program entry point.
'This is a good place to load resources that are not specific to a single activity.
If File.Exists(File.DirInternal,"sample_db.db")=False Then
File.Copy(File.DirAssets,"sample_db.db",File.DirInternal,"sample_db.db")

Else
sql.Initialize(File.DirInternal,"sample_db.db",True)

If sql.IsInitialized Then
Log("Database connection opened successfully.")
Else
Log("Failed to open database connection.")
End If

End If
End Sub



Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Layout")
Try
cursor=Starter.sql.ExecQuery("select * from user_table")
cursor.Position=0
Activity.Title=cursor.GetString("name")
cursor.Close
Catch
Log(LastException.Message)
End Try
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Please use [code]code here...[/code] tags when posting code.

2. Switch to B4XPages.

3.
This is not needed:
B4X:
If sql.IsInitialized Then
Log("Database connection opened successfully.")
Else
Log("Failed to open database connection.")
End If
An exception will be raised if it failed to initialize.

4.
B4X:
sql.Initialize(File.DirInternal,"sample_db.db",False) 'last parameter should be False if you are loading an existing db (it will work with True as well)

5. About the actual error, I can only guess that at some point a db was copied without this table. Delete it and copy the db again.
 
Upvote 0
Top