I have a project based on the SQLiteLight4 example.
I have stripped it to the minimum to show the problem.
I have added functionality to move rows up or down. I do it by creating a temp copy of the table in a different order, and then I copy the temp table over the old table.
It seems that the table is resorted back to the old order when the program closes. More specifically, the rowID of the records change between the time the program is closed and reopened, or so it seems.
Here is the code that displays the table:
Here is the code that moves the selected row one up:
About the example project:
There is a table with 5 entries. They are displayed sorted by rowID.
I have added a pair of buttons to allow moving rows up and down to let the user place the entries in the order that he or she wants. It does not have to be alphabetical.
Only the Up button actually rearranges the table. It does work as you can see it reorganizes the rows when you select a row first, then press the Up button.
For now, the down button only prints the table in the Log for debugging.
However, once you exit the program and restart, the table comes back in the old order.
I would rather not have to add a separate "index" column because I have the app on several machines and some have a large existing database that I would prefer not to have to recreate. In any case, I am not sure what a separate column would do that the rowID would not do.
Thanks in advance for any suggestion.
I have stripped it to the minimum to show the problem.
I have added functionality to move rows up or down. I do it by creating a temp copy of the table in a different order, and then I copy the temp table over the old table.
It seems that the table is resorted back to the old order when the program closes. More specifically, the rowID of the records change between the time the program is closed and reopened, or so it seems.
Here is the code that displays the table:
B4X:
Query = "SELECT Name, SchedC FROM Businesses ORDER BY rowID "
wbvTable.LoadHtml( Utils.ExecuteHtml( Starter.SQL1, Query, Null, 0, True ))
ReadDataBaseIDs ' this is the code from SQLiteLight4
Here is the code that moves the selected row one up:
B4X:
' move the selected row one up
Sub btnUp_Click
Dim Query, s As String
Dim Cursor1 As Cursor
If Starter.SelectedBusinessID = 1 Then Return
' start a transaction so we do everything or nothing
Starter.SQL1.BeginTransaction
' create tempBusinesses table
Starter.SQL1.ExecNonQuery( "DROP TABLE IF EXISTS tempBusinesses" )
Query = "CREATE TABLE tempBusinesses( Name TEXT, SchedC TEXT )"
Starter.SQL1.ExecNonQuery( Query )
' get all the Businesses from the database
Query = "SELECT * FROM Businesses "
Cursor1 = Starter.SQL1.ExecQuery( Query )
Log( "SelectedBusinessID: " & Starter.SelectedBusinessID ) ' SelectedBusinessID starts at 1, row starts at 0
For Row = 0 To Cursor1.RowCount - 1
If Row = Starter.SelectedBusinessID-2 Then
' here we are at the row BEFORE the selected one
' so we copy the data from the next row into the new table
Cursor1.Position = Row + 1
Else If Row = Starter.SelectedBusinessID-1 Then
' here we are at the row that is selected
' so we copy the data from the previous row into the new table
Cursor1.Position = Row - 1
Else
Cursor1.Position = Row
End If
s = Cursor1.GetString2( 1 )
If s = Null Then s = ""
Query = "INSERT INTO tempBusinesses( rowID, Name, SchedC ) VALUES( " & (Row+1) & ", '" & Cursor1.GetString2( 0 ) & "', '" & s & "' )"
Log( Query )
Starter.SQL1.ExecNonQuery( Query )
Next
' copy the temp table back over the Business table
Starter.SQL1.BeginTransaction ' <=== this statement was causing the problem!
Try
Starter.SQL1.ExecNonQuery( "DROP TABLE IF EXISTS Businesses" )
Starter.SQL1.ExecNonQuery( "CREATE TABLE Businesses( Name TEXT, SchedC TEXT )" )
Starter.SQL1.ExecNonQuery( "INSERT INTO Businesses( rowID, Name, SchedC ) SELECT rowID, Name, SchedC FROM tempBusinesses" )
Starter.SQL1.TransactionSuccessful
Catch
Log( "Transaction failed" )
End Try
Starter.SQL1.EndTransaction
' update the display
ShowTableBusinesses
' can we continue or not?
If Starter.SelectedBusinessID > 1 Then
Starter.SelectedBusinessID = Starter.SelectedBusinessID - 1
Else
lblSelectedItem.Text = ""
btnUp.Visible = False
btnDown.Visible = False
End If
End Sub ' btnUp_Click
About the example project:
There is a table with 5 entries. They are displayed sorted by rowID.
I have added a pair of buttons to allow moving rows up and down to let the user place the entries in the order that he or she wants. It does not have to be alphabetical.
Only the Up button actually rearranges the table. It does work as you can see it reorganizes the rows when you select a row first, then press the Up button.
For now, the down button only prints the table in the Log for debugging.
However, once you exit the program and restart, the table comes back in the old order.
I would rather not have to add a separate "index" column because I have the app on several machines and some have a large existing database that I would prefer not to have to recreate. In any case, I am not sure what a separate column would do that the rowID would not do.
Thanks in advance for any suggestion.
Attachments
Last edited: