2013-10-29 42 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

相關問題