Android Question File.Exist(......) always False

casher2

Member
Licensed User
Longtime User
Sub Process_Globals
Dim SQL1 As SQL
End Sub

Sub Globals
Dim qry as String
End Sub

SQL1.Initialize(File.DirInternal,"Test.db",True)

If File.Exists(File.DirInternal,"MyTable") = True Then
qry = "Drop Table MyTable"
SQL1.ExecNonQuery (qry)
Else
qry="Create Table MyTable (MyField,Int)"
SQL1.ExecNonQuery(qry)
End If

First run: "MyTable" doesn't exist, table is created. Great!
Second run: File.Exists still = False and tries to create table again. Error occurs saying table already exists (from first run Create Table). In short, File.Exists is always False. I've tried every possible change to SQL Initialize and File.Exists I can think of to no avail. What am I missing? Thanks!!
 

JakeBullet70

Well-Known Member
Licensed User
Longtime User
MyTable is not a file. It is contained inside the database.

B4X:
Sub TableExists(oSQL As SQL,TableName As String) As Boolean'ignore
    Dim Ret As String 
    Ret = oSQL.ExecQuerySingleResult("SELECT name FROM sqlite_master WHERE type='table' AND name='" & TableName & "'")
    Try
        If Ret = TableName Then 
            Return True           
        Else
            Return False
        End If
    Catch
        Return False
    End Try
End Sub
 
Upvote 0

casher2

Member
Licensed User
Longtime User
MyTable is not a file. It is contained inside the database.

B4X:
Sub TableExists(oSQL As SQL,TableName As String) As Boolean'ignore
    Dim Ret As String
    Ret = oSQL.ExecQuerySingleResult("SELECT name FROM sqlite_master WHERE type='table' AND name='" & TableName & "'")
    Try
        If Ret = TableName Then
            Return True          
        Else
            Return False
        End If
    Catch
        Return False
    End Try
End Sub
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I've tried every possible change to SQL Initialize and File.Exists I can think of to no avail. What am I missing? Thanks!!
Your complete code should be something like this:
B4X:
Sub Process_Globals
    Dim SQL1 as SQL
End Sub

Sub Globals
  Dim qry as String
End Sub


Sub Activity_Create(FirstTime AsBoolean)
  If FirstTime Then
       SQL1.Initialize(File.DirInternal,"Test.db",True)
  End If
  qry="Create Table if not exists MyTable (MyField Integer)"
  SQL1.ExecNonQuery(qry)
End sub
 
Upvote 0

JakeBullet70

Well-Known Member
Licensed User
Longtime User
Here is a bas module that has been floating around the forums for a few years.
You can add to your project or just grab code from it.
 

Attachments

  • db.zip
    4.9 KB · Views: 154
Upvote 0

casher2

Member
Licensed User
Longtime User
Great. Thanks again. There's a huge conceptual difference from MSAccess handling DBs. Slowly getting there thanks to help from people like you.
 
Upvote 0
Top