Android Question Why data can not show from db file ?

Volga_

Member
Hi everyone,
I try to read data from db file and print them, but I can not. I get error:
Error::
android.database.sqlite.SQLiteException: no such table: Cungcap (code 1 SQLITE_ERROR): , while compiling: SELECT FirstName FROM Cungcap
This is my code:
B4A:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim SQL1 As SQL
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    If FirstTime Then
        SQL1.Initialize(File.DirInternal, "Data.db", True)
    End If
    Dim Cursor1 As Cursor
    Cursor1 = SQL1.ExecQuery("SELECT FirstName FROM Cungcap")
    For i = 0 To Cursor1.RowCount - 1
        Cursor1.Position = i
        Log("************************")
        Log(Cursor1.GetString("FirstName"))
    Next
    Cursor1.Close
End Sub
And this is my project (attached).
How to fix all. Thanks.
 

Attachments

  • SQLI.rar
    498.4 KB · Views: 79
Solution
This is helpful for me.
The only other recommendation I can give you besides what @John Naylor gave you, is use RESULTSET not CURSOR as object, particularly when you are new and feeling your way with SQLite.. So your relevant code becomes:
B4X:
Dim cursor1 As ResultSet = SQL1.ExecQuery("SELECT FirstName FROM Cungcap")
    Do While cursor1.NextRow
        Log("************************")
        Log(cursor1.GetString("FirstName"))
    Loop
    cursor1.Close
I wanted to wait until you had a chance to see John's post first in order not to drown it with mine.

John Naylor

Active Member
Licensed User
Longtime User
Your database file remains in your assets folder. You have not copied it into the right place to be used.

Add a reference to xui then...


B4X:
    If FirstTime Then
        If File.Exists(xui.DefaultFolder, "Data.db") = False Then
            File.Copy(File.DirAssets, "Data.db", xui.DefaultFolder , "Data.db")
        End If
        SQL1.Initialize(xui.DefaultFolder, "Data.db", True)
    End If

Also I would strongly advise that you switch to B4XPages it makes things so much easier.
 
Last edited:
Upvote 2

Mahares

Expert
Licensed User
Longtime User
This is helpful for me.
The only other recommendation I can give you besides what @John Naylor gave you, is use RESULTSET not CURSOR as object, particularly when you are new and feeling your way with SQLite.. So your relevant code becomes:
B4X:
Dim cursor1 As ResultSet = SQL1.ExecQuery("SELECT FirstName FROM Cungcap")
    Do While cursor1.NextRow
        Log("************************")
        Log(cursor1.GetString("FirstName"))
    Loop
    cursor1.Close
I wanted to wait until you had a chance to see John's post first in order not to drown it with mine.
 
Upvote 1
Solution

Volga_

Member
I wanted to wait until you had a chance to see John's post first in order not to drown it with mine.
I try to learn, and this is what I see:
B4A:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Dim SQL1 As SQL
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    If File.Exists(xui.DefaultFolder, "Data.db") = False Then
        File.Copy(File.DirAssets, "Data.db", xui.DefaultFolder , "Data.db")
    End If
    SQL1.Initialize(xui.DefaultFolder, "Data.db", True)
    Dim Cursor1 As ResultSet = SQL1.ExecQuery("SELECT FirstName FROM Cungcap")
    Do While Cursor1.NextRow
        Log("************************")
        Log(Cursor1.GetString("FirstName"))
    Loop
    Cursor1.Close
End Sub
Thanks all. I got it.
 
Upvote 0
Top