2011-10-16 13 views
0

我需要使用SQLite,但有一個與我的語法有問題(當然這就是唯一的例外是告訴我......),這裏是我的代碼:Mono.Data.Sqlite語法

using System; 
using System.Data; 
using Mono.Data.Sqlite; 

class MainClass 
{ 
    public static void Main (string[] args) 
    {   
     string connectionString = "URI=file:SqliteTest.db,version=3"; 

     SqliteConnection conn = new SqliteConnection(connectionString); 
     conn.Open(); 

     SqliteCommand dbcommand = new SqliteCommand(conn); 

     string sql_command = "CREATE TABLE transaction (" + 
      "id INTEGER PRIMARY KEY," + 
      "datetemps TEXT NOT NULL," + 
      "description TEXT NOT NULL);"; 

     Console.WriteLine(sql_command); 

     dbcommand.CommandText = sql_command; 
     dbcommand.ExecuteNonQuery(); 

     dbcommand.Dispose(); 
     conn.Close(); 
    } 
} 

這裏的異常我收到:

Unhandled Exception: Mono.Data.Sqlite.SqliteException: SQLite error 
near "transaction": syntax error 

我已經習慣了使用MySQL,這不是第一次,我與數據庫的工作,但它是我第一次得到了這樣的問題,我只是想不通出了什麼問題,爲什麼會有'語法問題'。

感謝您的提示!

回答

1

交易是SQLite中的保留關鍵字。要使用它作爲一個對象的名稱,應將其用單或雙引號,括號或反引號:

CREATE TABLE 'transaction' ... 
CREATE TABLE "transaction" ... 
CREATE TABLE [transaction] ... 
CREATE TABLE `transaction` ... 

注意括號和反引號不是標準的SQL,所以報價一般建議。

有關其他保留字的完整列表:http://www.sqlite.org/lang_keywords.html

+0

謝謝,我忘了該交易是一個關鍵字,並感謝您的關鍵字列表的鏈接:) – HexaGridBrain