3

I get an error when I try to run this method:

public bool InsertLog(string logMsg, string type)
{
    bool result = false;

    string sql = "INSERT INTO `log`(`logmsg`, `type`) VALUES (?a, ?b)";


    using (SQLiteConnection conn = new SQLiteConnection(m_dbConnectionString))
    {
        conn.Open();
        using (var comm = conn.CreateCommand())
        {
            comm.CommandText = sql;
            comm.Parameters.AddWithValue("?a", logMsg);
            comm.Parameters.AddWithValue("?b", type);
            int res = comm.ExecuteNonQuery();
            result = (res == 1);

        }
        conn.Close();
    }
    return result;
}

On this database table:

CREATE TABLE log (
    id        INTEGER  PRIMARY KEY,
    createdAt DATETIME DEFAULT (datetime('now', 'localtime') ),
    logmsg    TEXT,
    type      VARCHAR
);

The error message is {"SQL logic error or missing database\r\nnear \"a\": syntax error"} (System.Data.SQLite.SQLiteExpcetion). Where is the syntax error in the insert query? I don't see it. Can't I use prepared statements this way?

1 Answer 1

4

Quotation marks in values (?, ?) are for positional parameters, so ?a is incorrect (you can, however, use something like ?123). If you want to use named parameters, change that to @a.

See Binding Values To Prepared Statements.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, in mysql I need to use question marks, so I thought it is okay. I will accept your answer when I can. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.