SQLite异常:android.database.sqlite.SQLiteException

时间:2017-11-08 20:31:28

标签: java android sqlite android-sqlite

我想创建一个显示已接来电的所有电话号码的应用程序。 使用sqlite将这些数字保存在表中。 这些是我所做的课程。

logcat的

Repeater {
    id: rep
    model: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110]

    delegate: Item {
        height: 100
        anchors.bottom: parent.verticalCenter
        anchors.left: center.horizontalCenter
        transformOrigin: Item.Bottom
        rotation: 360 / rep.model.length * index
        Text {
            text: modelData
            anchors.horizontalCenter: parent.horizontalCenter
            rotation: -parent.rotation // If you want to have them upright
        }
    }
}

我为表名来电号

分别开设了课程
  

DbContract.class

DbHelper.class

11-09 01:18:04.002 3027-3027/nischayvaish.com.test1 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: nischayvaish.com.test1, PID: 3027
                                                                      java.lang.RuntimeException: Unable to start activity ComponentInfo{nischayvaish.com.test1/nischayvaish.com.test1.MainActivity}: android.database.sqlite.SQLiteException: near "TABLEincoming_info": syntax error (code 1): , while compiling: CREATE TABLEincoming_info(ID INTEGER PRIMARY KEY AUTOINCREMENT,incoming_number,TEXT)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2696)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2757)
                                                                          at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1504)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                          at android.os.Looper.loop(Looper.java:163)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6237)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                       Caused by: android.database.sqlite.SQLiteException: near "TABLEincoming_info": syntax error (code 1): , while compiling: CREATE TABLEincoming_info(ID INTEGER PRIMARY KEY AUTOINCREMENT,incoming_number,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:1675)
                                                                          at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1606)
                                                                          at nischayvaish.com.test1.DbHelper.onCreate(DbHelper.java:28)
                                                                          at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
                                                                          at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                          at nischayvaish.com.test1.MainActivity.readFromDb(MainActivity.java:52)
                                                                          at nischayvaish.com.test1.MainActivity.onCreate(MainActivity.java:39)
                                                                          at android.app.Activity.performCreate(Activity.java:6847)
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2649)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2757) 
                                                                          at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1504) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                          at android.os.Looper.loop(Looper.java:163) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6237) 
                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

teInit.java:877) 
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

DbContract.class

public class DbHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "numberDb";
    private static final int DATABASE_VERSION = 1;
    private static final String CREATE_TABLE_INFO = "CREATE TABLE" + DbContract.TABLE_NAME  +"(ID INTEGER PRIMARY KEY AUTOINCREMENT," + DbContract.INCOMING_NUMBER + ",TEXT);";
    private static final String DROP_TABLE = "drop table if exists" + DbContract.TABLE_NAME;

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

}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_TABLE_INFO);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(DROP_TABLE);
    onCreate(db);
}

public void saveNumber(String number, SQLiteDatabase database) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(DbContract.INCOMING_NUMBER, number);
    database.insert(DbContract.TABLE_NAME, null, contentValues);
}

public Cursor readNumber(SQLiteDatabase database) {
    String[] projection = {"id", DbContract.INCOMING_NUMBER};
    return (database.query(DbContract.TABLE_NAME, projection, null, null, null, null, null));
}

错误来自DbContract.class或DbHelper.class

  

引起:android.database.sqlite.SQLiteException:near   &#34; TABLEincoming_info&#34 ;:语法错误(代码1):,编译时:CREATE   TABLEincoming_info(ID INTEGER PRIMARY KEY   AUTOINCREMENT,incoming_number,TEXT)

1 个答案:

答案 0 :(得分:0)

尝试在CREATE TABLE之后添加空格字符。

private static final String CREATE_TABLE_INFO = "CREATE TABLE " + DbContract.TABLE_NAME +"(ID INTEGER PRIMARY KEY AUTOINCREMENT," + DbContract.INCOMING_NUMBER + ",TEXT);";