2015-04-22 124 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`";