0

在 vb.net 中,下面的代码有效

cmd.CommandText = "CREATE TABLE Notes ( num INTEGER, name TEXT)"
Try
      cmd.ExecuteNonQuery()
Catch ex As Exception
      MsgBox("Error during save operation: " & ex.Message)
End Try

cmd.CommandText = "insert into Notes(num , name ) VALUES( 3, 'Bob') "
Try
    Console.WriteLine(cmd.ExecuteNonQuery())
Catch ex As Exception
    MsgBox("Error during save operation: " & ex.Message)
End Try

但我想放置一个整数变量而不是插入数字 3。我试过这个:

Dim count As Integer = 1
cmd.CommandText = "insert into Notes(num , name ) VALUES(" + count " + , 'Bob') "

Try
     Console.WriteLine(cmd.ExecuteNonQuery())
Catch ex As Exception
      MsgBox("Error during save operation: " & ex.Message)
End Try

但后来我有一个例外:

Conversion from string "insert into Notes(isFirst" to type 'Double' is not valid.

我知道这条线:

"insert into Notes(num , name ) VALUES(" + count + ", 'Bob') "

似乎完全错误,但我找不到任何其他解决方案,因为我都尝试了。

如何将计数变量插入表中?

提前致谢。

4

2 回答 2

1

您应该参数化查询。我不确定 SQLite 的确切语法,因为我使用的是 Oracle,但这肯定会让你走上正轨。

Dim count As Integer
'Whatever code you need to get the count from the user.

cmd.CommandText = "insert into Notes(num , name ) VALUES( @count, 'Bob')"

Dim myParam As New SQLiteParameter("@count")
cmd.Parameters.Add(myParam)

'Continue with executing the query, storing the result, etc.
于 2012-08-15T18:44:12.627 回答
1

您在双引号的错误一侧有“+”号:

cmd.CommandText = "insert into Notes(num , name ) VALUES(" + count " + , 'Bob') "

应该

cmd.CommandText = "insert into Notes(num , name ) VALUES(" & count & ", 'Bob') "

正如 ChrisStauffer 提到的,使用参数。总是。

于 2012-08-15T18:48:22.690 回答