使用 sqlite 导入删除 vb 中的 sqlite 行
Delete sqlite row in vb with sqlite import
我已经在我的 vb 项目中成功安装了 sqlite import,并且我能够从 .sqlite file.However 中获取数据,当我尝试从同一个文件中删除一行时,我没有任何错误,但也没有任何结果(它不起作用)。该文件不是只读的。所以,基本上我希望代码使用 vb 和 sqlite 导入从 sqlite 文件中删除一行。
我使用的代码:
Private Sub DelBtn_Click(sender As Object, e As EventArgs) Handles DelBtn.Click
Me.Delete("places", "date = '10/06/2016'")
End Sub
Public Function ExecuteNonQuery(sql As String) As Integer
Dim cnn As New SQLiteConnection("Data Source=C:\file.sqlite")
cnn.Open()
Dim mycommand As New SQLiteCommand(cnn)
mycommand.CommandText = sql
Dim rowsUpdated As Integer = mycommand.ExecuteNonQuery()
cnn.Dispose()
cnn.Close()
Return rowsUpdated
End Function
Public Function Delete(tableName As [String], where As [String]) As Boolean
Dim returnCode As [Boolean] = True
Try
Me.ExecuteNonQuery([String].Format("delete from {0} where {1};", tableName, where))
Catch fail As Exception
MessageBox.Show(fail.Message)
returnCode = False
End Try
Return returnCode
End Function
您认为查询相同的假设是错误的。
也就是说,这是一个使用您的 SQL 作为参考的参数化查询示例。
Public Function DeleteFromPlacesByDate(dt As DateTime) As Integer
Dim rowsUpdated As Integer = 0
Using connection As New SQLiteConnection("Data Source=C:\file.sqlite")
connection.Open()
'SQL with parameter included.
Dim sql As String = "DELETE FROM places WHERE date = @date"
'create command to execute sql query on connection
Dim command As New SQLiteCommand(sql, connection)
'add parameter and value
command.Parameters.AddWithValue("@date", dt)
'Call Prepare after setting the Commandtext and Parameters.
command.Prepare()
'execute command
rowsUpdated = command.ExecuteNonQuery()
connection.Close()
End Using
Return rowsUpdated
End Function
你也应该看看SQL As Understood By SQLite: Date And Time Functions
A time string can be in any of the following formats:
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
我已经在我的 vb 项目中成功安装了 sqlite import,并且我能够从 .sqlite file.However 中获取数据,当我尝试从同一个文件中删除一行时,我没有任何错误,但也没有任何结果(它不起作用)。该文件不是只读的。所以,基本上我希望代码使用 vb 和 sqlite 导入从 sqlite 文件中删除一行。
我使用的代码:
Private Sub DelBtn_Click(sender As Object, e As EventArgs) Handles DelBtn.Click
Me.Delete("places", "date = '10/06/2016'")
End Sub
Public Function ExecuteNonQuery(sql As String) As Integer
Dim cnn As New SQLiteConnection("Data Source=C:\file.sqlite")
cnn.Open()
Dim mycommand As New SQLiteCommand(cnn)
mycommand.CommandText = sql
Dim rowsUpdated As Integer = mycommand.ExecuteNonQuery()
cnn.Dispose()
cnn.Close()
Return rowsUpdated
End Function
Public Function Delete(tableName As [String], where As [String]) As Boolean
Dim returnCode As [Boolean] = True
Try
Me.ExecuteNonQuery([String].Format("delete from {0} where {1};", tableName, where))
Catch fail As Exception
MessageBox.Show(fail.Message)
returnCode = False
End Try
Return returnCode
End Function
您认为查询相同的假设是错误的。
也就是说,这是一个使用您的 SQL 作为参考的参数化查询示例。
Public Function DeleteFromPlacesByDate(dt As DateTime) As Integer
Dim rowsUpdated As Integer = 0
Using connection As New SQLiteConnection("Data Source=C:\file.sqlite")
connection.Open()
'SQL with parameter included.
Dim sql As String = "DELETE FROM places WHERE date = @date"
'create command to execute sql query on connection
Dim command As New SQLiteCommand(sql, connection)
'add parameter and value
command.Parameters.AddWithValue("@date", dt)
'Call Prepare after setting the Commandtext and Parameters.
command.Prepare()
'execute command
rowsUpdated = command.ExecuteNonQuery()
connection.Close()
End Using
Return rowsUpdated
End Function
你也应该看看SQL As Understood By SQLite: Date And Time Functions
A time string can be in any of the following formats:
YYYY-MM-DD YYYY-MM-DD HH:MM YYYY-MM-DD HH:MM:SS YYYY-MM-DD HH:MM:SS.SSS YYYY-MM-DDTHH:MM YYYY-MM-DDTHH:MM:SS YYYY-MM-DDTHH:MM:SS.SSS HH:MM HH:MM:SS HH:MM:SS.SSS now DDDDDDDDDD