deleting all entries in mysql

tufanv

Expert
Licensed User
Longtime User
hello . in mysql tutorial it is possible to select items and delete them one by one. Is it possible to clear all with one click ?

The tutorials code is below
B4X:
'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,"db.sql") = False Then
      File.Copy(File.DirAssets,"db.sql",File.DirInternal,"db.sql")
   End If
   
   If SQL1.IsInitialized = False Then
      SQL1.Initialize(File.DirInternal, "db.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("Username")& " | " & cursor1.GetString("Password"))
LVDb.SingleLineLayout.ItemHeight = 40
LVDb.SingleLineLayout.Label.TextSize = 20
LVDb.SingleLineLayout.label.TextColor = Colors.Black
LVDb.SingleLineLayout.label.Color = Colors.Gray
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
SQL1.ExecNonQuery("UPDATE tblUsers set Username ='"& txtUsername.text &"',Password ='"& txtPassword.text &"' WHERE ID = " & ID)
DBload
End Sub
Sub cmdExit_Click
   Activity.finish
   End Sub

ty
 

Cableguy

Expert
Licensed User
Longtime User
A very quick Google query showed me this...

B4X:
DELETE FROM table_name [WHERE Clause]

If WHERE clause is not specified then all the records will be deleted from the given MySQL table.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
Does

TRUNCATE table_name;

work?

Truncate should empty the table and reset any auto increment index columns to zero.
Delete will not reset any indexes.

Martin.

Sent from my GT-I9300 using Tapatalk 2
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Does

TRUNCATE table_name;

work?

Truncate should empty the table and reset any auto increment index columns to zero.
Delete will not reset any indexes.

Martin.

Sent from my GT-I9300 using Tapatalk 2
Not in sqlite, at least so I remember. A simple delete will have the same effect if no triggers are present.
 
Upvote 0
Top