2013-10-29 61 views
0

背景+问题:无法识别的令牌: “#” C#对于SQLite

(初学者在这里)我要填充的SQLite数据库从列表中我有我的C#代码值。下面是从finisarsqlite网站采取的sqlite的代码:我修改了它有点通过创建像“序列#”等

我自己的列名,但我发现了以下错误:“无法识别的记号#”

也许我的语法关闭了?

代码:

   // [snip] - As C# is purely object-oriented the following lines must be put  into a class: 

     // We use these three SQLite objects: 
     SQLiteConnection sqlite_conn; 
     SQLiteCommand sqlite_cmd; 
     SQLiteDataReader sqlite_datareader; 

     // create a new database connection: 
     sqlite_conn = new SQLiteConnection("Data Source=database.db;Version=3;New=True;Compress=True;"); 

     // open the connection: 
     sqlite_conn.Open(); 

     // create a new SQL command: 
     sqlite_cmd = sqlite_conn.CreateCommand(); 

     // Let the SQLiteCommand object know our SQL-Query: 
     sqlite_cmd.CommandText = "CREATE TABLE table1 (Seq# integer primary key, Field integer primary key, Description integer primary key);"; 

     // Now lets execute the SQL ;D                     
     sqlite_cmd.ExecuteNonQuery(); <<< ---- This is where Error Occurs ! 

     // Lets insert something into our new table: 
     sqlite_cmd.CommandText = "INSERT INTO table1 (Seq#, Field, Description) VALUES (list[0], list[1], list[2]);"; 

     // And execute this again ;D 
     sqlite_cmd.ExecuteNonQuery(); 


     // We are ready, now lets cleanup and close our connection: 
     sqlite_conn.Close(); 
    } 
+1

试着围绕你的列名换行,看看是否能解决问题? – Recipe

+1

尝试在'CommandText'前添加一个@ – Tico

+1

修复第一个错误后,下一个错误将出现在INSERT INTO table1(Seq#,Field,Description)VALUES(list [0],list [1],list [ 2]);“'... –

回答

4

在SQL中,你不能在一个普通的标识符使用#

您可以使用的是带引号的标识符。 在SQL中,这是用双引号("Seq#")完成的。 MySQL使用单引号('Seq#')或反引号(`Seq#`);或者使用单引号('Seq#')或反引号(`Seq#`)。 SQL Server使用括号([Seq#]); all these are supported in SQLite for compatibility

如果您不想在使用该名称时引用该名称,请删除#

+2

我强烈建议不要在变量名,表名,字段名等中使用像#这样的特殊字符的习惯 – jeremy

+0

我会尽量实施这些建议,再次在应用程序上工作,并会检查正确的答案 - thx! – user2788405

相关问题