Android Example SQLiteLight four simple SQLite projects

Pencil3

Member
Licensed User
Thanks for your help Klaus. Have another question with your SQLiteLight2 project. If I wanted to have the table column "Last name" display in the webview based on a PreferenceManager would that be done with CASE expressions? This is all new for me. Thanks again.
 

klaus

Expert
Licensed User
Longtime User
The column names are given in the query in the showTable routine, in this Lines:
Query = "SELECT ID, FirstName As [First name], LastName As [Last name], City FROM persons"
The displayed text is the one between the square braquets.
If you want to use a variable, you could use this:
Private AnyName = "Any namw" As String
Query = "SELECT ID, FirstName As [First name], LastName As [" & AnyName & "], City FROM persons"

The same is valid for the other columns.
You may have a look at SQLiteLight3, in this project the colum names and column pseudo names are with variables and not hard coded.
 

Pencil3

Member
Licensed User
I think I follow. So if I don't want the table column "Last name" to display in the webview based on a PreferenceManager I could use:

B4X:
Private AnyName = "AnyName" As String

If Manager.GetBoolean ("AnyNameDisplay") = False Then AnyName StillNeedToFigureOutHowtoRemoveFromBelowQuery

End If

Query = "SELECT ID, FirstName As [First name], LastName As [" & AnyName & "], City FROM persons"

Am I on the right track?

 

Pencil3

Member
Licensed User
Thank you again Klaus, I was able to figure it out by looking more closely at the SQLiteLight3 example.

Now I have another question. In the ShowTable Sub I use a ORDER BY DateTime(Date) DESC. I seem to be running into an issue with the UpdateSelectedItem Sub where I can select an entry but the information displayed in the lblSelectedItem label is not correct for the selection.

It appears to me that the Cursor is selecting based on a ascending list? I've been focusing on the Curs.Position = 0 in the UpdateSelectedItem Sub, but have not gotten very far. Any ideas?

Yes.
But again, you might have a look at SQLiteLight3.
 
Last edited:

klaus

Expert
Licensed User
Longtime User
You need to post your project as a zip file.
Without seeing what exactly you have done and how it's impossible to give a concrete advice.
In the examples I read the row IDs in a List in the ReadDataBaseIDs routine !
This ensures that you know the row ID for each row according to the query.
Do you use it ?
 
Last edited:

Pencil3

Member
Licensed User
Thank you again Klaus, figured it out. I had to change my ExecQuery around in the ReadDataBaseID sub. Thanks again for pointing me in the right direction.

 

Junctbr

Member
Hello sir Klaus. I just wanna ask how to make another column? In your example SQLiteLigh1 I added an edit text "Age". But it say's column 'Age' does not exist.
Thanks
@klaus
 
Last edited:

klaus

Expert
Licensed User
Longtime User
You need to modify the database:
All references to the database must be changed.
Just a few examples:
B4X:
Private Sub CreateDataBase
    Private Query As String
   
    'create the database with 4 columns
    Query = "CREATE TABLE persons (FirstName TEXT, LastName TEXT, City TEXT, Age INTEGER)"
    SQL1.ExecNonQuery(Query)

    'Fill a few entries
    Query = "INSERT INTO persons VALUES (?, ?, ?, ?)"
    SQL1.ExecNonQuery2(Query, Array As Object("John", "KENNEDY", "New York", 45))
    SQL1.ExecNonQuery2(Query, Array As Object("Peter", "FALK", "Los Angeles", 60))
    SQL1.ExecNonQuery2(Query, Array As Object("Jack", "LONDON", "Seattle", 34))
    SQL1.ExecNonQuery2(Query, Array As Object("Ronald", "REGAN", "Los Angeles", 83))
End Sub

B4X:
Private Sub ShowEntry(EntryIndex As Int)
    Private ResultSet1 As ResultSet
    Private RowID As Int
   
    If RowIDList.Size = 0 Then         'check if the database is empty
        Return                                            'if yes leave the routine
    End If
   
    RowID = RowIDList.Get(EntryIndex)        'get the RowID for the given entry index
    'read the entry with the given RowID
    ResultSet1 = SQL1.ExecQuery("SELECT * FROM persons WHERE rowid = " & RowID)
    lblRowID.Text = RowID                                                                    'display the RowID
    ResultSet1.NextRow                                                                        'set the next row
    edtFirstName.Text = ResultSet1.GetString("FirstName")    'read the FirstName column
    edtLastName.Text = ResultSet1.GetString("LastName")        'read the LasstName column
    edtCity.Text = ResultSet1.GetString("City")                        'read  the City column
    edtAge.Text = ResultSet1.GetString("Age")                        'read  the Age column
    ResultSet1.Close                                        'close the ResultSet, we don't it anymore
End Sub

B4X:
Private Sub UpdateEntry
    Private Query As String
   
    Query = "UPDATE persons Set FirstName = ?, LastName = ?, City = ?, Age = ? WHERE rowid = " & RowIDList.Get(CurrentIndex)
    SQL1.ExecNonQuery2(Query, Array As String(edtFirstName.Text, edtLastName.Text, edtCity.Text, edtAge.Text))
    ToastMessageShow("Entry updated", False)        'display a confirmation message
End Sub

I leave it up to you to check all the other lines.

You may have a look at SQLiteLight3, there the columns are in variables and not hardcoded, it might be easier to change.
I suggest you to look at the B4X SQLite Database booklet. The SQLiteLight projects are explained.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…