C# SQLite简单插入查询

C# SQLite简单插入查询,c#,.net,sqlite,C#,.net,Sqlite,我正在尝试使用SQLite作为我的存储。我已经使用nuget和语句添加了引用dll 我有 private void SetConnection() { sql_con = new SQLiteConnection ("Data Source=c:\\Dev\\MYApp.sqlite;Version=3;New=False;Compress=True;"); } private void ExecuteQuery(string txtQu

我正在尝试使用SQLite作为我的存储。我已经使用nuget和语句添加了引用dll

我有

private void SetConnection()
{
            sql_con = new SQLiteConnection
                ("Data Source=c:\\Dev\\MYApp.sqlite;Version=3;New=False;Compress=True;");
}

private void ExecuteQuery(string txtQuery)
{
            SetConnection();
            sql_con.Open();
            sql_cmd = sql_con.CreateCommand();
            sql_cmd.CommandText = txtQuery;
            sql_cmd.ExecuteNonQuery();
            sql_con.Close(); 
}
我像这样发送查询文本

public void Create(Book book)
{
            string txtSqlQuery  = "INSERT INTO Book (Id, Title, Language, PublicationDate, Publisher, Edition, OfficialUrl, Description, EBookFormat) ";
            txtSqlQuery += string.Format("VALUES (@{0},@{1},@{2},@{3},@{4},@{5},@{6},@{7},{8})", 
                        book.Id, book.Title, book.Language, book.PublicationDate, book.Publisher, book.Edition, book.OfficialUrl, book.Description, book.EBookFormat);
                   try
                   {
                       ExecuteQuery(txtSqlQuery);
                   }
                   catch (Exception ex )
                   {
                       throw new Exception(ex.Message);
                   }    
}
“我的数据库”已正确创建,并且已传递具有有效数据的book实例,该实例正常。但在这一行代码上执行查询时总是引发异常:

sql_cmd.ExecuteNonQuery();
我显然做错了什么,但我看不见

更新:引发的异常消息为

SQL逻辑错误或缺少数据库

无法识别的令牌:“22cf”

其中,此
22cf
是传递的
book.Id
guid字符串的一部分。

将字符串发送到INSERT语句时应使用(') 值(@{0}、@{1}、'@{2}、'@{3}、'@{4}、'@{5}、'@{6}、'@{7}、{8}) 您还应该捕获SQLExeprion

使用准备好的语句和绑定参数:

public void Create(Book book) {
    SQLiteCommand insertSQL = new SQLiteCommand("INSERT INTO Book (Id, Title, Language, PublicationDate, Publisher, Edition, OfficialUrl, Description, EBookFormat) VALUES (?,?,?,?,?,?,?,?,?)", sql_con);
    insertSQL.Parameters.Add(book.Id);
    insertSQL.Parameters.Add(book.Title);
    insertSQL.Parameters.Add(book.Language);
    insertSQL.Parameters.Add(book.PublicationDate);
    insertSQL.Parameters.Add(book.Publisher);
    insertSQL.Parameters.Add(book.Edition);
    insertSQL.Parameters.Add(book.OfficialUrl);
    insertSQL.Parameters.Add(book.Description);
    insertSQL.Parameters.Add(book.EBookFormat);
    try {
        insertSQL.ExecuteNonQuery();
    }
    catch (Exception ex) {
        throw new Exception(ex.Message);
    }    
}

将记录插入SqliteDB:

使用System.Data.SQLite;
私有无效BTN插入\单击(对象发送方,路由目标)
{
字符串连接=@“数据源=C:\Folder\SampleDB.db;版本=3;新建=False;压缩=True;”;
SQLiteConnection sqlite_conn=新SQLiteConnection(连接);
string stringQuery=“INSERT INTO_StudyInfo”+“(Param,Val)”+“Values('Name','“+snbox.Text+”)”;//将StudyInfo插入数据库
sqlite_conn.Open();//打开sqlite连接
var SqliteCmd=new SQLiteCommand();//初始化SQLiteCommand
SqliteCmd=sqlite_conn.CreateCommand();//创建SqliteCmd命令
SqliteCmd.CommandText=stringQuery;//将查询分配给CommandText
SqliteCmd.ExecuteNonQuery();//执行SqliteCmd命令
sqlite_conn.Close();//关闭sqlite连接
}

引发了什么异常?@Alireza更新了questioI,我注意到您没有在SQL命令中使用正式参数。这是不太可取的,因为您将不得不担心将数据呈现到SQL语句中(即格式化日期、小数分隔符等)。答案很好,我可能会使用此处建议的命名参数tho为什么?因为在我的一些sql语句中,您可能多次使用同一参数。如果参数不是字符串类型,则此示例不起作用。这不正确:接受对象值。当然,实现会将.NET类型转换为适当的数据库类型。SQLite实现接受字符串类型,但也接受数字类型,null,…这是SQLiteParameterCollection.Add(),但您是对的--它确实接受对象。我一定是在测试的时候出错了。在任何情况下,IntelliSense都不会在您键入左括号后显示该版本的Add()(它仅在您键入.before Add时显示)。如果对象很复杂,如何进行嵌套或递归添加?这似乎无效。您会收到一个强制转换错误,抱怨您的对象不是
SQLiteParameter
类型。