sql sort

tufanv

Expert
Licensed User
Longtime User
Hello I have the tutorial of H.Ubalde about adding simple string to sql file . The code is below. İn this example the entries are sorted with id numbers ascending. 1,2,3,4 ... I want to sort them descending like 4 3 2 1 (The last one must be the first line) How Can i do that With changing the code below ? TY

B4X:
#Region Module Attributes
   #FullScreen: False
   #IncludeTitle: True
   #ApplicationLabel: SQLDemo
   #VersionCode: 1
   #VersionName: 
   #SupportedOrientations: portrait
   #CanInstallToExternalStorage: False
#End Region

'Activity module
Sub Process_Globals
   Dim SQL1 As SQL
   Dim cursor1 As Cursor
End Sub

Sub Globals
   Dim txtUsername As EditText
   Dim txtPassword As EditText
   Dim LVDb As ListView
   Dim cmdAdd As Button
   Dim cmdDelete As Button
   Dim cmdEdit As Button
   Dim ID As String

End Sub

Sub Activity_Create(FirstTime As Boolean)

   Activity.LoadLayout("main")
   If File.Exists(File.DirInternal,"db2.sql") = False Then
      File.Copy(File.DirAssets,"db2.sql",File.DirInternal,"db2.sql")
   End If
   
   If SQL1.IsInitialized = False Then
      SQL1.Initialize(File.DirInternal, "db2.sql", False)
   End If
   
   DBload
   
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub DBload
LVDb.Clear'need to clear the list
cursor1 = SQL1.ExecQuery("SELECT * FROM tblUsers")
For i = 0 To cursor1.RowCount - 1
cursor1.Position = i
LVDb.AddSingleLine(cursor1.GetString("ID")& "|" &cursor1.GetString("Type")& " | " & cursor1.GetString("Time"))
LVDb.SingleLineLayout.ItemHeight = 40
LVDb.SingleLineLayout.Label.TextSize = 20
LVDb.SingleLineLayout.Label.TextColor = Colors.Black
LVDb.SingleLineLayout.label.Color = Colors.White
Next
End Sub

Sub cmdAdd_Click
      
   If txtUsername.Text = "" OR txtPassword.Text = "" Then
      Msgbox("You have to enter all fields","Missed data field")
Else
   
   'Grab the last ID number which is the highest number
   cursor1 = SQL1.ExecQuery("SELECT ID FROM tblUsers")
   If cursor1.RowCount > 0 Then
      For i = 0 To cursor1.RowCount - 1   
   cursor1.Position = i
   
   Dim NewID As Int
   NewID = cursor1.GetInt("ID")
Next

End If
   NewID = NewID +1 ' add 1 to the ID number to make a new ID field
   SQL1.ExecNonQuery("INSERT INTO tblUsers VALUES('" & NewID & "','" & txtUsername.Text & "','" & txtPassword.Text & "')")
   DBload
   txtUsername.Text = ""
   txtPassword.Text = ""
   txtUsername.RequestFocus
End If


End Sub
Sub cmdDelete_Click
   
   
   SQL1.ExecNonQuery("DELETE FROM tblUsers where ID = '" &ID & "' ")
   DBload
   txtUsername.Text = ""
   txtPassword.Text =""
   
End Sub

Sub LVDb_ItemClick (Position As Int, Value As Object)' click on the entry in the list
Dim idvalue As String
Dim countIt As Int

idvalue = Value
countIt = idvalue.IndexOf("|") 'find location of sperator
idvalue = idvalue.SubString2(0,countIt) 'find first part of label text
ID = idvalue
cursor1 = SQL1.ExecQuery("SELECT * FROM tblUsers where ID = '" & ID & "' ")
For i = 0 To cursor1.RowCount - 1
cursor1.Position = i
   txtUsername.text=cursor1.getString("Username")
   txtPassword.text=cursor1.getString("Password")
Next
End Sub

Sub cmdEdit_Click
 If txtUsername.Text = "" OR txtPassword.Text = "" Then
      Msgbox("Select item to edit","Missed data item")
Else
   SQL1.ExecNonQuery("UPDATE tblUsers set Username ='"& txtUsername.text &"',Password ='"& txtPassword.text &"' WHERE ID = " & ID)
   DBload
End If

End Sub
Sub cmdExit_Click
   Activity.finish
   End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…