17

我正在尝试使用 SQLite 作为我的存储。我还使用 nuget 和 using 语句添加了引用 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(); 
}

我正在发送这样的查询 txt

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);
                   }    
}

我的数据库已正确创建并通过有效数据传递书籍实例是可以的。但是在这行代码上执行查询时总是抛出异常:

sql_cmd.ExecuteNonQuery();

我显然在这里做错了什么,但我看不到。

更新:抛出的异常消息是

SQL 逻辑错误或缺少数据库

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

22cf是传递的book.Idguid 字符串的一部分。

4

4 回答 4

54

永远不要在语句中插入数据!

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

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);
    }    
}
于 2013-10-21T08:40:44.097 回答
0

要将记录插入 SqliteDB:

using System.Data.SQLite;

    private void btnInsert_Click(object sender, RoutedEventArgs e)
    {
     string connection = @"Data Source=C:\Folder\SampleDB.db;Version=3;New=False;Compress=True;";
     SQLiteConnection sqlite_conn = new SQLiteConnection(connection);
     string stringQuery ="INSERT INTO _StudyInfo"+"(Param,Val)"+"Values('Name','" + snbox.Text + "')";//insert the studyinfo into Db
     sqlite_conn.Open();//Open the SqliteConnection
     var SqliteCmd = new SQLiteCommand();//Initialize the SqliteCommand
     SqliteCmd = sqlite_conn.CreateCommand();//Create the SqliteCommand
     SqliteCmd.CommandText = stringQuery;//Assigning the query to CommandText
     SqliteCmd.ExecuteNonQuery();//Execute the SqliteCommand
     sqlite_conn.Close();//Close the SqliteConnection
    }

于 2020-02-24T07:06:59.197 回答
0

使用 Dapper (Dapper.2.0.90) 和 SQLite (System.Data.SQLite.Core.1.0.114.2) 更新最新版本

using System.Data.SQLite;
using Dapper;

连接字符串(数据库名称 - 'Store.db')

connectionString="Data Source=.\Store.db;Version=3;"

我的应用程序中的人员保存方法

    public static void SavePerson(PersonModel person)
    {
        using (IDbConnection cnn = new SQLiteConnection(connectionString))
        {
            cnn.Execute("insert into Person (FirstName, LastName) values (@FirstName, @LastName)", new { person.FirstName, person.LastName} );
        }
    }

人物模型

public class PersonModel
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public string FullName
    {
        get
        {
            return $"{ FirstName } { LastName }";
        }
    }
}

这个答案包括我在我的应用程序中使用和使用的代码方法。

于 2021-12-27T04:46:52.603 回答
-4

将字符串发送到 INSERT 语句 VALUES (@{0},'@{1}','@{2}','@{3}','@{4}','@{ 5}','@{6}','@{7}',{8}) 你也应该捕获 SQLExeprion

于 2013-10-20T15:32:22.050 回答