Android Question problem about access SQL database

kcwenfrank

New Member
I am trying to do some exercise about sql database with B4A, However, I am stuck here for a while,
I tried to build a sample sql-database in advance and then write some simple code in B4A.
The code is very simple as below
B4X:
        SQL1.Initialize(File.DirInternal,"example.db", True)

    SQL1_Index=SQL1.ExecQuery("SELECT * from sql_exercise")
the problem is every time the code crash after running ExecQuery.
Can some one give me a hint for this ?
 

Attachments

  • sql_exercise.zip
    8.2 KB · Views: 161

ronell

Well-Known Member
Licensed User
Longtime User
include the error logs in your post .. we cant help without it

edit: change your code
B4X:
File.DirInternal
to
B4X:
File.DirAssets
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
The files in the File folder of your project are in File.DirAssets and not in the File.DirInternal folder.
These are two different folders.
Databases cannot be accessed in File.DirAssets, they must be copied somewhere else, for example to File.DirInternal.
Attached a modified project.
I suggest you to have a look at the B4X Booklets and more specific the SQLite chapter in the B4A User's Guide.
You may also have a look at the SQLiteLight examples, which are shipped with the User's Guide.
 

Attachments

  • sql_exercise1.zip
    8.4 KB · Views: 187
Upvote 0

ronell

Well-Known Member
Licensed User
Longtime User
Databases cannot be accessed in File.DirAssets, they must be copied somewhere else, for example to File.DirInternal.
forgot about that lol :confused:, it should be like this
B4X:
If File.Exists(File.DirInternal,"example.db") = False Then
        File.Copy(File.DirAssets,"example.db",File.DirInternal,"example.db")
    End If
 
Upvote 0

kcwenfrank

New Member
ronell, thank you for the reply.
I am not sure if I misunderstanding or not. The original code will give me the error msg as below.
android.database.sqlite.SQLiteException: no such table: sql_exercise (code 1): , while compiling: SELECT * from sql_exercise
I think the SQL command should be correct as I tried in SQLite Expert already.
I also modified the code as your suggestion and got the error msg as below.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
I think the initialize should be no problem , just don't know if i miss anything or not.
 
Upvote 0

kcwenfrank

New Member
klaus, thank you for the replay. Sorry, I just replied ronell's post.
Anyway I just saw your replied and tried the code already. It works great and your replied help me as well.
 
Upvote 0
Top