Android DB - 语法错误

时间:2015-04-22 16:44:00

标签: android sqlite syntax-error

我想创建数据库。这是DBHelper

public class DBHelper extends SQLiteOpenHelper {

String TAG = Const.TAG_DB_HELPER;

public DBHelper(Context context) {
    super(context, Const.DB_TABLE_NAME, null, 1);
}

public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + Const.DB_TABLE_NAME + " ("
            + Const.DB_COLUMN_ID_NAME + " integer primary key autoincrement,"
            + Const.DB_COLUMN_NAME_NAME + " text,"
            + Const.DB_COLUMN_FROM_NAME + " text,"
            + Const.DB_COLUMN_TO_NAME + " text,"
            + Const.DB_COLUMN_DAYS_NAME + " text,"
            + Const.DB_COLUMN_SOUND_NAME + " text,"
            + Const.DB_COLUMN_INDICATOR_NAME + " text);");

    Log.w(TAG, "Database '" + Const.DB_TABLE_NAME + "' created successfully!");
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

以下是vars

static String DB_TABLE_NAME = "timetables";
static String DB_COLUMN_ID_NAME = "id";
static String DB_COLUMN_NAME_NAME = "name";
static String DB_COLUMN_DAYS_NAME = "days";
static String DB_COLUMN_FROM_NAME = "from";
static String DB_COLUMN_TO_NAME = "to";
static String DB_COLUMN_SOUND_NAME = "sound";
static String DB_COLUMN_INDICATOR_NAME = "indicator";

但是到了创建数据库的时候,我就得到了错误。这是:

android.database.sqlite.SQLiteException: near "from": syntax error (code 1): , while compiling: create table timetables (id integer primary key autoincrement,name text,from text,to text,days text,sound text,indicator text);
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
        at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
        at ru.sergey.timetable.DBHelper.onCreate(DBHelper.java:21)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
        at ru.sergey.timetable.Utils.addTimetableToDB(Utils.java:35)
        at ru.sergey.timetable.AddTimetableActivity.onClick(AddTimetableActivity.java:132)
        at android.view.View.performClick(View.java:4780)
        at android.view.View$PerformClick.run(View.java:19866)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

根据我的理解,DBHelper有问题,它靠近'from'列,对吧?但我已经检查了每一个符号。有什么想法吗?

提前致谢SergaRUS

2 个答案:

答案 0 :(得分:0)

fromto是保留关键字。请为这些列选择其他名称。

答案 1 :(得分:0)

from是一个SQL关键字。您应该更改列名称(这是最佳做法) 或者你可以尝试

static String DB_COLUMN_FROM_NAME = "`from`";