更新sqlite查询在Visual Basic 2013中不起作用

时间:2015-01-03 12:02:41

标签: vb.net visual-studio-2010 visual-studio visual-studio-2012 visual-studio-2013

我正在尝试插入行和更新行(如果行已存在),如下所示 -

Dim con As SQLiteConnection
Dim sql As String
Dim cmd As New SQLiteCommand
Dim da As SQLiteDataAdapter
Dim ds As New DataSet
Dim NumberOfRows As Integer
    Public Sub UpdateUserStatistics(ByVal UserId As Integer, ByVal QId As Integer, _
                                    ByVal KeyName As String, ByVal KeyValue As Integer)
        con = New SQLiteConnection("Data Source = " + AppDomain.CurrentDomain.BaseDirectory + "/user_statistics.db;Version=3;")
        con.Open()
        sql = "SELECT * FROM med_user_meta where user_id=" & UserId & " And qid=" & QId
        da = New SQLiteDataAdapter(sql, con)
        da.Fill(ds, "UserMeta")
        NumberOfRows = ds.Tables("UserMeta").Rows.Count
        ds.Clear()
        If (NumberOfRows = 0) Then
            Dim InsertQuery As SQLiteCommand = con.CreateCommand
            InsertQuery.CommandText = "INSERT INTO med_user_meta " _
                                 & "(user_id, qid, " _
                                 & KeyName _
                                 & ", timestamp) VALUES(?, ?, ?, datetime('now'))"

            InsertQuery.Parameters.AddWithValue("user_id", UserId)
            InsertQuery.Parameters.AddWithValue("qid", QId)
            InsertQuery.Parameters.AddWithValue(KeyName, KeyValue)
            RowInserted = InsertQuery.ExecuteNonQuery()
        ElseIf (NumberOfRows = 1) Then
            Dim UpdateQuery As SQLiteCommand = con.CreateCommand
            UpdateQuery.CommandText = "UPDATE med_user_meta SET " _
                & KeyName _
                & " = ?, timestamp = datetime('now') Where qid = ?"
            'UpdateQuery.Parameters.AddWithValue("user_id", UserId)
            UpdateQuery.Parameters.AddWithValue("qid", QId)
            UpdateQuery.Parameters.AddWithValue(KeyName, KeyValue)
            RowUpdated = UpdateQuery.ExecuteNonQuery()
        End If
        con.Close()
    End Sub

我在模块中使用此代码,并在需要时调用UpdateUserStatistics 我的问题是,插入查询工作正常,update查询不起作用。

已经超过2个小时,但我无法找到,我在哪里犯错误?

此外,有没有办法在添加参数后将最终更新查询添加到消息框中,以便我可以检查我的最终更新查询是否正确?

修改 - 1

我忘了提及,RowUpdated的值返回0,即ExecuteNonQuery未更新行。

我还确认了与更新查询对应的行存在,ElseIf (NumberOfRows = 1) Then块也在运行,但它没有更新行。

0 个答案:

没有答案