【发布时间】:2015-04-27 13:47:32
【问题描述】:
我正在使用 SQLiteHelper 创建一个数据库,当应用程序尝试运行时,我不断收到异常。
这是我收到的错误消息: 引起:android.database.sqlite.SQLiteException:在“Trip”附近:语法错误(代码 1):,编译时:创建表 Business Trip Expenditure(desc text 主键,餐饮整数 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);
}
}
如果有人可以帮助我了解创建表的语法,我将不胜感激,我知道这种形式的代码在语法方面很挑剔。
【问题讨论】:
-
表名之间不能有
space。将 ot 替换为_之类的字符。 -
@PrerakSola 其实你可以 (
public static final String TABLE_NAME = "[Business Trip Expenditure]";)。但不鼓励这样做。 -
感谢他更新@DerGolem。我不知道。
标签: android sqlite exception syntax