tags:

views:

93

answers:

1

Hello.

The following code works perfectly:

  var oDb = new SQLiteConnection();
  oDb.ConnectionString = String.Format(
      "Data Source={0};"
    + "Synchronous=Full;",
    "test.db" );
  oDb.Open();
  SQLiteCommand oCmd = new SQLiteCommand( oDb );
  oCmd.CommandText =  "create table tests ( ";
  oCmd.CommandText += "  id guid primary key not null";
  oCmd.CommandText += ", name text default 'none' )";
  oCmd.ExecuteNonQuery();

But if i try to use a query builder (to automatically escape strings):

  var oDb = new SQLiteConnection();
  oDb.ConnectionString = String.Format(
      "Data Source={0};"
    + "Synchronous=Full;",
    "test.db" );
  oDb.Open();
  SQLiteCommand oCmd = new SQLiteCommand( oDb );
  oCmd.CommandText =  "create table tests ( ";
  oCmd.CommandText += "  id guid primary key not null";
  oCmd.CommandText += ", name text default @param )";
  var oParam = new SQLiteParameter( "@param", "none" );
  oCmd.Parameters.Add( oParam );
  oCmd.ExecuteNonQuery();

Exception is raised:

SQLite error
near "@param": syntax error

Can anyone please spare a bit of knowledge and hint me what i'm doing wrong?

+2  A: 

Although I haven't tested I, this should work: oCmd.Parameters.AddWithValue("@param", "none"); Edit: it works if you add () around the @param:

Edit2: Well, the ExecuteNonQuery() does work, but the result is not the expected result. @param doesn't get replaced with the acutal param. Do not use this code! (now a community wiki. maybe somebody is able to fix it?)

SQLiteCommand oCmd = new SQLiteCommand( oDb ); 
oCmd.CommandText =  "create table tests ( ";
oCmd.CommandText += "  id guid primary key not null";
oCmd.CommandText += ", name text default (@param) )";
var oParam = new SQLiteParameter("@param", "none");
oCmd.Parameters.Add(oParam);
oCmd.ExecuteNonQuery();
tobsen
Exactly same exception :(
Eye of Hell
I changed my answer to a working version. However I do not really know why those () have to be present: http://www.sqlite.org/syntaxdiagrams.html#column-constraint states that there can be also a literal without the brackets...
tobsen
I'm suprised that it works at all. I didn't know you could use parameter binding with data definition language statements.
tuinstoel
tuinstoel seems to be right: the query is executed correctly but @param won't be substituted by "none" :-( Too bad, my fault for not testing it properly. I'll edit my answer. Thanks tuinstoel.
tobsen