How do I get around the "'" problem in sqlite and c#? How do I get around the "'" problem in sqlite and c#? sqlite sqlite

How do I get around the "'" problem in sqlite and c#?


The solution presented by Robert will work (i.e. replacing ' by '').

Alternatively you can use parameters as in:

DbCommand   cmd = new DbCommand();DbParameter param = cmd.CreateParameter();// ...// more code// ...cmd.CommandText = "Insert table (field) values (@param)";param.ParameterName = "param"param.DbType = DbType.String;param.Value  = @"This is a sample value with a single quote like this: '";cmd.Parameters.Add(param);cmd.ExecuteNonQuery();


Using parameters protects against sql injection, and makes the ' problems qo away.

It is also much faster because sqlite can reuse the execution plan of statements when you use parameters. It can't when you don't use parameters. In this example using a parameter makes the bulk insert action approximately 3 times faster.

private void TestInsertPerformance() {  const int limit = 100000;  using (SQLiteConnection conn = new SQLiteConnection(@"Data Source=c:\testperf.db")) {    conn.Open();    using (SQLiteCommand comm = new SQLiteCommand()) {      comm.Connection = conn;      comm.CommandText = " create table test (n integer) ";      comm.ExecuteNonQuery();      Stopwatch s = new Stopwatch();      s.Start();      using (SQLiteTransaction tran = conn.BeginTransaction()) {        for (int i = 0; i < limit; i++) {          comm.CommandText = "insert into test values (" + i.ToString() + ")";          comm.ExecuteNonQuery();        }        tran.Commit();      }      s.Stop();      MessageBox.Show("time without parm " + s.ElapsedMilliseconds.ToString());      SQLiteParameter parm = comm.CreateParameter();      comm.CommandText = "insert into test values (?)";      comm.Parameters.Add(parm);      s.Reset();      s.Start();      using (SQLiteTransaction tran = conn.BeginTransaction()) {        for (int i = 0; i < limit; i++) {          parm.Value = i;          comm.ExecuteNonQuery();        }        tran.Commit();      }      s.Stop();      MessageBox.Show("time with parm " + s.ElapsedMilliseconds.ToString());    }    conn.Close();  }}

Sqlite behaves similar to Oracle when it comes to the importance of using parameterised sql statements.