Android Question How to read an existing data base?

MikeSimpson

Member
Licensed User
Longtime User
I want to read an existing data base and display some fields in text labels.

I created a data base with SQLightDatabaseBrowser and made a small test program. But I got already an error opening the data base file.

I only need to access a data base file and read the content, I don't want to create or change anything.

What's wrong? Do I have to create the data base with another program? What is the code to display the contend of a cell? (Like in the db_test sample)?

B4X:
ub Process_Globals
   
    Dim SQL1 As SQL

End Sub

Sub Globals

    Private Label1 As Label
    Private Label2 As Label
    Private Label3 As Label
    Private Label4 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
    Activity.LoadLayout("Main")
   
    SQL1.Initialize(File.DirAssets, "Persons.db", True)
   
   
    Label1.Text = "Name nr.: " '$ 'Index 2 from Persons.db
    Label2.Text = "First name: " '$ 'First name 2 from Persons.db
    Label3.Text = "Name: " '$ 'Name 2 from Persons.db
    Label4.Text = "Nick name: " '$ 'Nick name 2 from Persons.db
   
End Sub
 

Attachments

  • error.JPG
    error.JPG
    103.7 KB · Views: 148
  • db.JPG
    db.JPG
    62 KB · Views: 138
  • db_test.zip
    7.5 KB · Views: 116

mangojack

Expert
Licensed User
Longtime User
have you seen these examples by Klaus ... 3 Simple SQLlite examples

this is also very handy website (although syntax is slightly different) SQL Tutorial ..


try this
B4X:
'## your database file is called "persons.db" but the table name is "Names"

'if you were wanting to write to the db you must first copy it from Assets.
If File.Exists(File.DirDefaultExternal, "Persons.db") = False Then
     File.Copy(File.DirAssets, "persons.db", File.DirDefaultExternal, "persons.db")
  End If

SQL1.Initialize(File.DirDefaultExternal, "Persons.db", False)

dim Cursor1 As Cursor
Cursor1 = SQL1.ExecQuery("SELECT * FROM Names")  '## your table name is called "Names"

Cursor1.Position = 0   '### the first record
  Label1.Text = Cursor1.GetString("Index")   '### what data type did you assign 'Index' Integer or String ??
  Label2.Text = Cursor1.GetString("First name")
  Label3.Text = Cursor1.GetString("Name")
  Label4.Text = Cursor1.GetString("Nick name")
Cursor1.Close
 
Last edited:
Upvote 0
Top