Android Question Having Issues Creating Database - SQL Lite - Need Help

neomewilson

Member
Licensed User
Longtime User
I am new to Basic for Android but not development. I have reworked this many times and can't seem to ever see a database. I am using SQLite Editor to look for the database based on the application name and it never sees one for this application.

I have read all the documentation and tutorials but can't seem to get this working. I am using version 3.0 on a Nexus 7 32GB Tablet. Here is my code:

B4X:
Sub Activity_Create(FirstTime As Boolean)

    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("myworldmain")
       
    'Test if this is the first time for the application to run and if so, create and initialize the database.
    If FirstTime Then
        If File.Exists(File.DirRootExternal, "MyWorld.db") = False Then
            SQLMyWorld.Initialize(File.DirRootExternal,"MyWorld.db", True)
            'Create the tables
            CreateTables
        Else
            SQLMyWorld.Initialize(File.DirRootExternal,"MyWorld.db", True)           
        End If   
       
    End If
   
End Sub

I have been using this to create the table and insert a single record.

B4X:
Sub CreateTables

    'If Table Doesn't Exist (FirstTime = True) Then Create Table, If (FirstTime = False) Do Nothing.
    SQLMyWorld.ExecNonQuery("CREATE TABLE ProjectList (prjName TEXT)")
    'Remove line below once database creation confirmed - DEBUG ONLY
    SQLMyWorld.ExecNonQuery("INSERT INTO ProjectList VALUES ('Project Test Value')")    

End Sub
 

LucaMs

Expert
Licensed User
Longtime User
I do not know why, but from the windows explorer "sometimes" you can not find the db!

If you can, try to use Ecliplse and you'll see that there is!

P.S. It is better to use DirDefaultExternal instead of rootexternal.
There is a helpful Code Module, DBUtils.bas on site
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
There is nothing wrong with your code. It works. Make sure you declare:
B4X:
Dim SQLMyWorld As SQL    'in Process_global.
To see your database, use a file manager or download a similar app on your Nexus and go to 'Main Storage'. That is DirRootExternal on the Nexus 7. Tap it, you will see the database: MyWorld.db. I tested your code, of course by commenting the loadlayout line since I do not have your layout.
 
Upvote 0

neomewilson

Member
Licensed User
Longtime User
Update! Thanks.... That helped me. I downloaded a different editor named "aSQLiteManager" from Google Play on my device and it saw the database right away created based on the parameter DIRRootExternal vs. DIRDefaultExternal. I looked at the path and saw that it was not where the other app was looking and changed it to the correct file path and then it found the database too.

One note of advise is that I would suggest everyone makes clearer on these post is the importance of specifying where each parameter will store the database. For instance when I set the parameter back to DirDefaultExternal the database is stored in the following path:

B4X:
"/sdcard/Android/data/AppName/files/databasename.db"

In my case the actual path based on my application named 'MyWorld' was the following:

B4X:
"/sdcard/Android/data/My.World/files/MyWorld.db"

The path for the parameter set to DirRootExternal is the following:

B4X:
"/storage/emulated/0/MyWorld.db"

I hope this helps others who are new to this language and Android.

Again, my many thanks to your assistance.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
A few tips / notes:
1. You can force the media scanner to scan the newly created file. It should then appear in Windows explorer: http://www.b4x.com/android/forum/threads/saving-a-picture.10200/#post-56686
2. Check DBUtils.CopyFromAssetsFile code. You shouldn't assume that there is a writable external storage available.
3. The actual path of File.DirDefaultExternal should normally not matter.
 
Upvote 0
Top