我需要使用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,这不是我第一次使用数据库,但这是我第一次遇到这种问题,我就是不知道是什么问题,为什么会有"语法问题"。
谢谢你的提示!
Transaction是SQLite中的保留关键字。若要将其用作对象名称,请将其用单引号或双引号、方括号或反引号括起来:
CREATE TABLE 'transaction' ...
CREATE TABLE "transaction" ...
CREATE TABLE [transaction] ...
CREATE TABLE `transaction` ...
请注意,括号和反引号不是标准的SQL,所以通常建议使用引号。
其他保留字的完整列表:http://www.sqlite.org/lang_keywords.html