2015-04-22 89 views
0

我想创建数据库。这里是DBHelperAndroid DB - 语法错误

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

回答

0

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

+0

是的,非常感谢。现在一切正常 – SergaRUS

+0

不客气。然后,请将我的答案标记为已接受。 – cygery

0

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

static String DB_COLUMN_FROM_NAME = "`from`";