我无法使用vb.net将整数插入到sqlite表中

时间:2012-08-15 18:19:56

标签: vb.net sqlite

在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

但是我想放一个Integer变量而不是插入数字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') "

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

如何将count变量插入表中?

提前致谢。

2 个答案:

答案 0 :(得分: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.

答案 1 :(得分:1)

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

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

应该是

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

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