Android SQLite异常语法错误(代码1)

时间:2015-04-27 13:47:32

标签: android sqlite exception syntax

我正在使用SQLiteHelper创建数据库,并且在应用程序尝试运行时我一直收到异常。

这是我收到的错误消息:引起:android.database.sqlite.SQLiteException:near“Trip”:语法错误(代码1):,同时编译:create table Business Trip Expenditure(desc text primary key,用餐整数NOT NULL,饮料整数NOT NULL,旅行整数NOT NULL,住宿整数NOT NULL,其他整数NOT NULL,Receipt_Retained?real NOT NULL,Created_at text NOT NULL);

public class MySQLiteHelper extends SQLiteOpenHelper {

    public static final String TABLE_NAME = "Business Trip Expenditure";
    //Column Names
    public static final String COLUMN_NAME = "desc";
    public static final String MEAL_COST ="Meals";
    public static final String DRINK_COST = "Drinks";
    public static final String TRAVEL_COST = "Travel";
    public static final String ACCO_COST = "Accommodation";
    public static final String OTHER_COST = "Other";
    public static final String HAS_RECEIPT = "Receipt_Retained?";
    public static final String CREATED_AT = "Created_at";

    private static final String DATABASE_NAME = "task.db";
    private static final int DATABASE_VERSION = 1;

    // Database creation sql statement
    private static final String DATABASE_CREATE = "create table "
            + TABLE_NAME + "( " + COLUMN_NAME + " text primary key , " + MEAL_COST + " integer NOT NULL , " + DRINK_COST + " integer NOT NULL , " + TRAVEL_COST + " integer NOT NULL , " + ACCO_COST + " integer NOT NULL , " + OTHER_COST + " integer NOT NULL , " + HAS_RECEIPT + " real NOT NULL , " + CREATED_AT + " text NOT NULL );";

    public MySQLiteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(MySQLiteHelper.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}

如果有人可以帮助我创建表的语法,我将非常感激,我知道这种形式的代码在语法方面是挑剔的。

1 个答案:

答案 0 :(得分:3)

改变:

import sqlite3
conn2 = sqlite3.connect('datatest.db')  # start the data base
databa2 = conn2.cursor()

try:
    databa2.execute("CREATE TABLE datatest(date blob, rd blob)")
    if conn2:
        conn2.commit()
except:
    databa2.execute("DROP TABLE datatest ")
    databa2.execute("CREATE TABLE datatest (date blob, rd blob)")
    if conn2:
        conn2.commit()
a=1
b=2
print (b)
databa2.execute("INSERT INTO datatest VALUES (?,?)", (a,b))
databa2.execute("INSERT INTO datatest VALUES (?,?)", (b,a))
databa2.execute("INSERT INTO datatest VALUES (?,?)", (a,a))
if conn2:
    conn2.commit()

for row in conn2.execute("SELECT date , rd "):
    print(row)
if conn2:
    conn2.commit()

public static final String TABLE_NAME = "Business Trip Expenditure";