Android Question Creating the database when the application is first installed

DeerBear

Member
Licensed User
Longtime User
Hello!

I would like to generate the database when the application is installed.
I am currently using this code:

Private Sub CreateDDItemTable
Dim Fields As Map

Fields.Initialize
Fields.Put( "ID", DBUtils.DB_INTEGER )
Fields.Put( "DD_NAME",DBUtils.DB_TEXT )
Fields.Put( "DD_DATE", DBUtils.DB_TEXT )
Fields.Put( "DD_VALUE",DBUtils.DB_REAL )
Fields.Put( "DD_STATUS",DBUtils.DB_INTEGER )
DBUtils.CreateTable( SQLEngine,"DD_ITEMS",Fields,"ID" )
End Sub

Public Sub InitializeSQL( FirstTime As Boolean )
If FirstTime Then
SQLEngine.Initialize( File.DirInternal,"DDLog.db",True )
If DBUtils.TableExists( SQLEngine,"DD_ITEMS" ) = False Then
CreateDDItemTable
End If
End If
End Sub

Inside DBUtils.bas

Sub TableExists ( SQL As SQL, TableName As String ) As Boolean
Dim Query As String
Dim QCursor As Cursor
Query = "SELECT DISTINCT TBL_NAME FROM SQLITE_MASTER WHERE TBL_NAME = '" & TableName & "'"
Log( "TableExists: " & Query )
QCursor = SQL.ExecQuery( Query )
Return ( QCursor.GetString( "TBL_NAME" ) <> "" )
End Sub

Unfortunately, my TableExists code doesn't work on all of my emulators. I suspect this is down to some changes in SQLite but I am not an expert on the topic (I really don't like SQLite).
Does anybody have a way to determine whether a table is present? For those cases where this doesn't work, the error is that the field TBL_NAME was not found.

I am unsure of what to do.

Suggestions?

A
 

barx

Well-Known Member
Licensed User
Longtime User
Why not just use

USE TABLE IF NOT EXISTS......
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
If understood your meaning, then include a text file to be installed in dir internal, check its existence and if it is there then create the tables, then delete or alter it.
 
Upvote 0
Top