Android Question Database Unable to Open

Vivek Bhatt

Member
Licensed User
when i run program then i show no such and file directory error. i solve this problem.
after that it can show this error and i try to best for solve this problem but i cant that.
*** Service (starter) Create ***
Error occurred on line: 21 (Starter)
android.database.sqlite.SQLiteException: unable to open database file
at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1829)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
at anywheresoftware.b4a.sql.SQL.Initialize(SQL.java:44)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:755)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:345)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at b4a.example.starter.onCreate(starter.java:55)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:1920)
at android.app.ActivityThread.access$2500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:982)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3647)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
 

Vivek Bhatt

Member
Licensed User
Can you post your code
****-----Code of Starter Module-----****
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Public sql1 As SQL
Public dbpath="D:\B4A\Files\data.db" As String
Public dbname="data.db" As String
Public sqlinit As SQL
End Sub

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(dbpath,dbname) = True Then
sql1.Initialize(dbpath,dbname,True)
Else
sql1.Initialize(dbpath,dbname,False)
End If

If File.Exists(dbpath,dbname)=True Then
sql1.Initialize(dbpath,dbname,True)
Else
File.Copy(File.DirAssets,dbname,dbpath,dbname)
sql1.Initialize(dbpath,dbname,True)

End If
End Sub


*****-----Code of Main Module-----********
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Public sql1 As SQL
Public dbpath As String
Public dbname As String
End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.

Dim btnlogin As Button
Dim unametxt As EditText
Dim passtxt As EditText
Public idtxt As EditText

End Sub

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
If FirstTime Then
If File.Exists(File.DirInternal, "data.db") = False Then
sql1.Initialize(File.DirInternal,"data.db",True)
CreateDatabase
End If
End If
Activity.LoadLayout("main")

End Sub

Sub Activity_Resume
If sql1.IsInitialized = False Then
sql1.Initialize(File.DirInternal,"data.db",True)
End If

End Sub

Sub Activity_Pause (UserClosed As Boolean)
If UserClosed Then
sql1.Close
End If
End Sub


Sub unametxt_TextChanged (Old As String, New As String)

End Sub

Sub passtxt_TextChanged (Old As String, New As String)

End Sub

Sub CreateDatabase'(id1 As Int,name1 As String,pass1 As String)
Private query As String
query = "CREATE TABLE data (id Integer Primary Key,Name TEXT,Password TEXT)"
sql1.ExecNonQuery(query)
End Sub

Public Sub AddEntry(id As Int , Name As String,Password As String)
Private query As String
Private cursor1 As Cursor
Private id As Int
query = "SELCET * FROM data where id = ? AND Name = ? AND Password = ?"
cursor1 = sql1.ExecQuery2(query,Array As String(unametxt.Text,passtxt.Text))
If cursor1.RowCount>0 Then
ToastMessageShow("This Entry is Already Exist", False)
Else
query = "INSERT INTO data VALUES (NULL, ?, ?)"
sql1.ExecNonQuery2(query, Array As String(unametxt.text,passtxt.text))
ToastMessageShow("Entery Added", False)
id = sql1.ExecQuerySingleResult("SELECT max(id) from data")
idtxt.Text=id
'RowNumber = RowNumber + 1
End If
cursor1.Close

End Sub
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
check at your line , D: doesn't exist on Android, as well as putting the file name in the path with your code

B4X:
Public dbpath="D:\B4A\Files\data.db" As String
...
If File.Exists(File.DirInternal, "data.db") = False Then
 
Upvote 0

Vivek Bhatt

Member
Licensed User
check at your line , D: doesn't exist on Android, as well as putting the file name in the path with your code

B4X:
Public dbpath="D:\B4A\Files\data.db" As String
...
If File.Exists(File.DirInternal, "data.db") = False Then


Thank You Sir.

I will try this code.
Sorry sir But this error come again.

Thank You.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
when i run program then i show no such and file directory error. i solve this problem.
after that it can show this error and i try to best for solve this problem but i cant that.

You need to use code tags when you post code. Also, when you post a lot of code, it is always better to export your project.
There are many problems with your code. Among them, you initialize in many places. The database should be initialized only once in Starter module. To help you out, I have redone your project where the database is created in File.DirRootExternal so you can see it, but you can change its location to File.DirInternal. I made a lot of changes so it gives you a jump start. I tested it well and it works now. You take over from there:
 

Attachments

  • CrateDataBaseVivek090917.zip
    8.2 KB · Views: 285
Upvote 0

BillMeyer

Well-Known Member
Licensed User
Longtime User
EDIT - Mahares was quicker than me - there' s a good solution !!

I was once in your situation and the brothers on this forum helped me, so maybe I can help you.

Firstly, to make it easier for us to help you, please use
upload_2017-9-9_15-43-0.png
(insert) and then select code and insert your code into the space provided by the popup that is presented to you. This makes it much easier for us all to read and offer help much quicker.

Secondly, your code is missing a vital line of code. No where are you starting the "Starter" Service - so according to your posted code - your Main module does not know where to look or what to do with your code you are asking it to execute.

Thirdly, to avoid having to create and repeat create and by so doing multiplying your chances of making a mistake, try to declare everything you can in "Starter" Service - BUT - please remember to start the Starter Service.

Then, try to avoid calling your table in your SQLite scheme the same as your database file - this will cause confusion later on when your app grows (eg. data for table name and data.db for file name)

Finally, your are not developing for a PC = the "D:" drive does not exist on an Android device

I have modified your code (BUT I HAVE NOT TESTED IT) - it could still have some errors in it. Wherever I have changed something I have either commented it or have used 4x ' - like '''' Slectc * From....

Right - let's go - Your Main Modules code is here:
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
''''    Public sql1 As SQL - Not required Here
    Public dbpath As String
    Public dbname As String

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim btnlogin As Button
    Dim unametxt As EditText
    Dim passtxt As EditText
    Public idtxt As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    StartService("Starter")
''''    If FirstTime Then
''''        If File.Exists(File.DirInternal, "data.db") = False Then
''''            sql1.Initialize(File.DirInternal,"data.db",True)
''''            CreateDatabase
''''        End If
''''    End If
    Activity.LoadLayout("main")
  
End Sub

Sub Activity_Resume
''''    If sql1.IsInitialized = False Then
''''        sql1.Initialize(File.DirInternal,"data.db",True)
''''    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        Starter.sql1.Close ' Changed Here
    End If
End Sub


Sub unametxt_TextChanged (Old As String, New As String)

End Sub

Sub passtxt_TextChanged (Old As String, New As String)

End Sub


Public Sub AddEntry(id As Int , Name As String, Password As String)
        Private query As String
        Private cursor1 As Cursor
        Private id As Int
        Private Cnt As Int = 0
      
    query = "Select Count(*) FROM UserInfo"     ' Determine Counter for Primary Key in your table
    Cnt = Starter.sql1.ExecQuerySingleResult(query) ' Changed Here
    Cnt = Cnt + 1
  
    query = "SELECT * FROM UserInfo where id = "&id&" AND Name = "&Name&" AND Password = "&Password ' Spelling Here Changed = SELECT As well as make use of Parameters
''''        cursor1 = sql1.ExecQuery2(query,Array As String(unametxt.Text,passtxt.Text))
        cursor1 = Starter.sql1.ExecQuery(query) '- Changed Here
      
        If cursor1.RowCount>0 Then
            ToastMessageShow("This Entry Already Exists", False)
        Else
            query = "INSERT INTO UserInfo VALUES (?, ?, ?)" ' Changed Here - removed Null and replaced with ?
            Starter.sql1.ExecNonQuery2(query, Array As String(Cnt, unametxt.text, passtxt.text)) ' Changed Here - Array now has Cnt
            ToastMessageShow("Entery Added", False)
            id = Starter.sql1.ExecQuerySingleResult("SELECT max(id) from UserInfo") ' - Changed Here
            idtxt.Text=id
            'RowNumber = RowNumber + 1
        End If
        cursor1.Close

End Sub

And your Starter modules code is here:

B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Public sql1 As SQL
''''    Public dbpath="D:\B4A\Files\data.db" As String
''''    Public dbname="data.db" As String
''''    Public sqlinit As SQL
End Sub
End Sub

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(dbpath,dbname) = True Then
''''        sql1.Initialize(dbpath,dbname,True)
''''    Else
''''        sql1.Initialize(dbpath,dbname,False)
''''    End If
''''
''''    If File.Exists(dbpath,dbname)=True Then
''''        sql1.Initialize(dbpath,dbname,True)
''''    Else
''''        File.Copy(File.DirAssets,dbname,dbpath,dbname)
''''        sql1.Initialize(dbpath,dbname,True)
''''
    ''''    End If

    If sql1.IsInitialized = False Then
        sql1.Initialize(File.DirInternal, "data.db", True) ' Consider Naming this differently
    End If
  
    CreateDatabase
  
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub

Sub CreateDatabase'(id1 As Int,name1 As String,pass1 As String)
  
    Private query As String
''''    query = "CREATE TABLE data (id Integer Primary Key, Name TEXT, Password TEXT)"
    query = "CREATE TABLE IF NOT EXISTS  UserInfo (id INTEGER PRIMARY KEY, Name TEXT, Password TEXT)" ' Changed This - Check the syntax
    sql1.ExecNonQuery(query)
End Sub

I trust that you are not such a newbie that you know how to put these two together.

Note the referral to the Starter Service: eg. Starter.sql1.ExecQuery(query)

Just keep at it - you get better and better - and the brothers here are always willing to help.

Any questions - please fire away - we are ready to help
 
Upvote 0

fixit30

Active Member
Licensed User
Longtime User
BUT - please remember to start the Starter Service.
This is incorrect. @Erel has said many times in the past that the Starter service should never be explicitly started. It will be started automatically, and will run before the Main module.
 
Upvote 0

eps

Expert
Licensed User
Longtime User
plus check and re-check filenames - they are case sensitive

Ideally you need to move the DB from the internal space to a readwrite space as well - especially if you are adding or updating records.

This is a snippet of my code...

B4X:
                If File.Exists(File.DirInternal,"thedatabase.db") = False Then

                    'ToastMessageShow("Database hasn't been copied to read/write area",True)
                   
                    File.Copy(File.DirAssets,"thedatabase.db",File.DirInternal,"thedatabase.db")
......
 
Upvote 0

BillMeyer

Well-Known Member
Licensed User
Longtime User
This is incorrect. @Erel has said many times in the past that the Starter service should never be explicitly started. It will be started automatically, and will run before the Main module.

I humbly apologize - I missed that - and I've learnt something yet again - I've always started it - Sorry !!
 
Upvote 0

Vivek Bhatt

Member
Licensed User
T
EDIT - Mahares was quicker than me - there' s a good solution !!

I was once in your situation and the brothers on this forum helped me, so maybe I can help you.

Firstly, to make it easier for us to help you, please use View attachment 59481 (insert) and then select code and insert your code into the space provided by the popup that is presented to you. This makes it much easier for us all to read and offer help much quicker.

Secondly, your code is missing a vital line of code. No where are you starting the "Starter" Service - so according to your posted code - your Main module does not know where to look or what to do with your code you are asking it to execute.

Thirdly, to avoid having to create and repeat create and by so doing multiplying your chances of making a mistake, try to declare everything you can in "Starter" Service - BUT - please remember to start the Starter Service.

Then, try to avoid calling your table in your SQLite scheme the same as your database file - this will cause confusion later on when your app grows (eg. data for table name and data.db for file name)

Finally, your are not developing for a PC = the "D:" drive does not exist on an Android device

I have modified your code (BUT I HAVE NOT TESTED IT) - it could still have some errors in it. Wherever I have changed something I have either commented it or have used 4x ' - like '''' Slectc * From....

Right - let's go - Your Main Modules code is here:
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
''''    Public sql1 As SQL - Not required Here
    Public dbpath As String
    Public dbname As String

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim btnlogin As Button
    Dim unametxt As EditText
    Dim passtxt As EditText
    Public idtxt As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    StartService("Starter")
''''    If FirstTime Then
''''        If File.Exists(File.DirInternal, "data.db") = False Then
''''            sql1.Initialize(File.DirInternal,"data.db",True)
''''            CreateDatabase
''''        End If
''''    End If
    Activity.LoadLayout("main")
 
End Sub

Sub Activity_Resume
''''    If sql1.IsInitialized = False Then
''''        sql1.Initialize(File.DirInternal,"data.db",True)
''''    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        Starter.sql1.Close ' Changed Here
    End If
End Sub


Sub unametxt_TextChanged (Old As String, New As String)

End Sub

Sub passtxt_TextChanged (Old As String, New As String)

End Sub


Public Sub AddEntry(id As Int , Name As String, Password As String)
        Private query As String
        Private cursor1 As Cursor
        Private id As Int
        Private Cnt As Int = 0
     
    query = "Select Count(*) FROM UserInfo"     ' Determine Counter for Primary Key in your table
    Cnt = Starter.sql1.ExecQuerySingleResult(query) ' Changed Here
    Cnt = Cnt + 1
 
    query = "SELECT * FROM UserInfo where id = "&id&" AND Name = "&Name&" AND Password = "&Password ' Spelling Here Changed = SELECT As well as make use of Parameters
''''        cursor1 = sql1.ExecQuery2(query,Array As String(unametxt.Text,passtxt.Text))
        cursor1 = Starter.sql1.ExecQuery(query) '- Changed Here
     
        If cursor1.RowCount>0 Then
            ToastMessageShow("This Entry Already Exists", False)
        Else
            query = "INSERT INTO UserInfo VALUES (?, ?, ?)" ' Changed Here - removed Null and replaced with ?
            Starter.sql1.ExecNonQuery2(query, Array As String(Cnt, unametxt.text, passtxt.text)) ' Changed Here - Array now has Cnt
            ToastMessageShow("Entery Added", False)
            id = Starter.sql1.ExecQuerySingleResult("SELECT max(id) from UserInfo") ' - Changed Here
            idtxt.Text=id
            'RowNumber = RowNumber + 1
        End If
        cursor1.Close

End Sub

And your Starter modules code is here:

B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Public sql1 As SQL
''''    Public dbpath="D:\B4A\Files\data.db" As String
''''    Public dbname="data.db" As String
''''    Public sqlinit As SQL
End Sub
End Sub

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(dbpath,dbname) = True Then
''''        sql1.Initialize(dbpath,dbname,True)
''''    Else
''''        sql1.Initialize(dbpath,dbname,False)
''''    End If
''''
''''    If File.Exists(dbpath,dbname)=True Then
''''        sql1.Initialize(dbpath,dbname,True)
''''    Else
''''        File.Copy(File.DirAssets,dbname,dbpath,dbname)
''''        sql1.Initialize(dbpath,dbname,True)
''''
    ''''    End If

    If sql1.IsInitialized = False Then
        sql1.Initialize(File.DirInternal, "data.db", True) ' Consider Naming this differently
    End If
 
    CreateDatabase
 
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub

Sub CreateDatabase'(id1 As Int,name1 As String,pass1 As String)
 
    Private query As String
''''    query = "CREATE TABLE data (id Integer Primary Key, Name TEXT, Password TEXT)"
    query = "CREATE TABLE IF NOT EXISTS  UserInfo (id INTEGER PRIMARY KEY, Name TEXT, Password TEXT)" ' Changed This - Check the syntax
    sql1.ExecNonQuery(query)
End Sub

I trust that you are not such a newbie that you know how to put these two together.

Note the referral to the Starter Service: eg. Starter.sql1.ExecQuery(query)

Just keep at it - you get better and better - and the brothers here are always willing to help.

Any questions - please fire away - we are ready to help
ha
EDIT - Mahares was quicker than me - there' s a good solution !!

I was once in your situation and the brothers on this forum helped me, so maybe I can help you.

Firstly, to make it easier for us to help you, please use View attachment 59481 (insert) and then select code and insert your code into the space provided by the popup that is presented to you. This makes it much easier for us all to read and offer help much quicker.

Secondly, your code is missing a vital line of code. No where are you starting the "Starter" Service - so according to your posted code - your Main module does not know where to look or what to do with your code you are asking it to execute.

Thirdly, to avoid having to create and repeat create and by so doing multiplying your chances of making a mistake, try to declare everything you can in "Starter" Service - BUT - please remember to start the Starter Service.

Then, try to avoid calling your table in your SQLite scheme the same as your database file - this will cause confusion later on when your app grows (eg. data for table name and data.db for file name)

Finally, your are not developing for a PC = the "D:" drive does not exist on an Android device

I have modified your code (BUT I HAVE NOT TESTED IT) - it could still have some errors in it. Wherever I have changed something I have either commented it or have used 4x ' - like '''' Slectc * From....

Right - let's go - Your Main Modules code is here:
B4X:
#Region  Project Attributes
    #ApplicationLabel: B4A Example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
''''    Public sql1 As SQL - Not required Here
    Public dbpath As String
    Public dbname As String

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim btnlogin As Button
    Dim unametxt As EditText
    Dim passtxt As EditText
    Public idtxt As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    StartService("Starter")
''''    If FirstTime Then
''''        If File.Exists(File.DirInternal, "data.db") = False Then
''''            sql1.Initialize(File.DirInternal,"data.db",True)
''''            CreateDatabase
''''        End If
''''    End If
    Activity.LoadLayout("main")
 
End Sub

Sub Activity_Resume
''''    If sql1.IsInitialized = False Then
''''        sql1.Initialize(File.DirInternal,"data.db",True)
''''    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        Starter.sql1.Close ' Changed Here
    End If
End Sub


Sub unametxt_TextChanged (Old As String, New As String)

End Sub

Sub passtxt_TextChanged (Old As String, New As String)

End Sub


Public Sub AddEntry(id As Int , Name As String, Password As String)
        Private query As String
        Private cursor1 As Cursor
        Private id As Int
        Private Cnt As Int = 0
     
    query = "Select Count(*) FROM UserInfo"     ' Determine Counter for Primary Key in your table
    Cnt = Starter.sql1.ExecQuerySingleResult(query) ' Changed Here
    Cnt = Cnt + 1
 
    query = "SELECT * FROM UserInfo where id = "&id&" AND Name = "&Name&" AND Password = "&Password ' Spelling Here Changed = SELECT As well as make use of Parameters
''''        cursor1 = sql1.ExecQuery2(query,Array As String(unametxt.Text,passtxt.Text))
        cursor1 = Starter.sql1.ExecQuery(query) '- Changed Here
     
        If cursor1.RowCount>0 Then
            ToastMessageShow("This Entry Already Exists", False)
        Else
            query = "INSERT INTO UserInfo VALUES (?, ?, ?)" ' Changed Here - removed Null and replaced with ?
            Starter.sql1.ExecNonQuery2(query, Array As String(Cnt, unametxt.text, passtxt.text)) ' Changed Here - Array now has Cnt
            ToastMessageShow("Entery Added", False)
            id = Starter.sql1.ExecQuerySingleResult("SELECT max(id) from UserInfo") ' - Changed Here
            idtxt.Text=id
            'RowNumber = RowNumber + 1
        End If
        cursor1.Close

End Sub

And your Starter modules code is here:

B4X:
#Region  Service Attributes
    #StartAtBoot: False
    #ExcludeFromLibrary: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Public sql1 As SQL
''''    Public dbpath="D:\B4A\Files\data.db" As String
''''    Public dbname="data.db" As String
''''    Public sqlinit As SQL
End Sub
End Sub

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(dbpath,dbname) = True Then
''''        sql1.Initialize(dbpath,dbname,True)
''''    Else
''''        sql1.Initialize(dbpath,dbname,False)
''''    End If
''''
''''    If File.Exists(dbpath,dbname)=True Then
''''        sql1.Initialize(dbpath,dbname,True)
''''    Else
''''        File.Copy(File.DirAssets,dbname,dbpath,dbname)
''''        sql1.Initialize(dbpath,dbname,True)
''''
    ''''    End If

    If sql1.IsInitialized = False Then
        sql1.Initialize(File.DirInternal, "data.db", True) ' Consider Naming this differently
    End If
 
    CreateDatabase
 
End Sub

Sub Service_Start (StartingIntent As Intent)

End Sub

Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
End Sub

'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Sub Service_Destroy

End Sub

Sub CreateDatabase'(id1 As Int,name1 As String,pass1 As String)
 
    Private query As String
''''    query = "CREATE TABLE data (id Integer Primary Key, Name TEXT, Password TEXT)"
    query = "CREATE TABLE IF NOT EXISTS  UserInfo (id INTEGER PRIMARY KEY, Name TEXT, Password TEXT)" ' Changed This - Check the syntax
    sql1.ExecNonQuery(query)
End Sub

I trust that you are not such a newbie that you know how to put these two together.

Note the referral to the Starter Service: eg. Starter.sql1.ExecQuery(query)

Just keep at it - you get better and better - and the brothers here are always willing to help.

Any questions - please fire away - we are ready to help


Thank You So Much sir.
 
Upvote 0
Top