SQLite错误 - 参数替换

时间:2013-06-11 16:18:15

标签: vb.net sqlite parameter-passing

我在此代码中收到错误,完全让我感到困惑,任何帮助,想法,建议都表示赞赏。

我有以下代码:

   Sub addauthorsearch(ByVal objconn As SQLiteConnection, ByVal author As String, ByVal searchname As String)
        Dim myTrans As SQLiteTransaction
        Dim objCommand As SQLiteCommand
        Dim authorid As Integer
        myTrans = objconn.BeginTransaction
<< snipped  authorid is retrieved in here>>
       objCommand = objconn.CreateCommand()
        'objCommand.CommandText = "INSERT INTO [authorsearch] ([searchid],[authorid] ) VALUES ('" & searchname & "', '" & authorid  & "');"
        objCommand.CommandText = "INSERT INTO [authorsearch] ([searchid],[authorid] ) VALUES (@name, @id);"
        objCommand.Parameters.Clear()
        objCommand.Parameters.Add(New SQLiteParameter("@name", searchname))  ' * error occurs here
        objCommand.Parameters.Add(New SQLiteParameter("@id", authorid))

        Try
            objCommand.ExecuteNonQuery()
        Catch ex As InvalidOperationException
            MessageBox.Show("An error has occurred: " & ex.Message)
        Catch ex As SQLiteException
            MessageBox.Show("An error has occurred: " & ex.Message)

        End Try
        myTrans.Commit()

数据库定义为(在创建中)为

CREATE TABLE authorsearch 
    (searchid VARCHAR (150) NOT NULL UNIQUE, 
     authorid INTEGER REFERENCES author (authorid))

返回的错误是InvalidCastException,特别是从字符串“a-itmatov,chingiz”到类型Integer的转换无效。 searchID定义为vchar,我认为没有理由将任何内容转换为整数。 非参数化语句有效,但是由于显而易见的原因,它在包含单引号的名称(例如O'Neil)上失败。

任何建议都将不胜感激。

在一个相关的问题上,显然翻译C#代码有帮助,并且有几个讨论sqlite和c#的论坛,有没有人知道VB.NET和sqlite有一个区域的论坛。

0 个答案:

没有答案