尝试输入带空格的String时,SQLite崩溃

时间:2012-11-26 03:03:31

标签: java android sqlite android-sqlite

我正在创建一个具有默认表的应用程序,该表存储其他表的内容,以便当您尝试将自己的输入放入应用程序时,它将存储表,但是当您尝试将表添加到默认表时除非有一个空间“测试”将完美“测试1”将使应用程序崩溃,它的工作完美。这是我的代码检索用户输入的字符串:

t = new Table(this);
t.open();
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        rListName = input.getText().toString();
        t.createNew(rListName + "DB");
        t.createList(rListName, rListName + "DB");
        t.close();
    }
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        dialog.cancel();
    }
});
alert.show();

这是我的createNew和createList:

public void createNew(String TABLE_NAME){
if(DATABASE_STATUS == "new"){
    ourHelper.createNew(ourDatabase, TABLE_NAME);
    System.out.println("Creating " + TABLE_NAME);
}else{
    System.out.print("Already Created");
    }
}


public long createList(String listName, String listDB){
    ContentValues cv = new ContentValues();
    cv.put(KEY_LISTNAME, listName);
    cv.put(KEY_LISTDB, listDB);
    return ourDatabase.insert(DEFAULT_TABLE, null, cv);
}

为执行新表而执行的createNew()

public void createNew(SQLiteDatabase db, String TABLE_NAME){
    db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + 
    KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
    KEY_QUESTION + " TEXT NOT NULL, " +
    KEY_PATH + " TEXT NOT NULL);"
    );
}

最后但不是租约,我的logcat:

11-25 22:02:30.418: I/SqliteDatabaseCpp(24726): sqlite returned: error code = 1, msg = near "helloDB": syntax error
11-25 22:02:30.428: D/AndroidRuntime(24726): Shutting down VM
11-25 22:02:30.428: W/dalvikvm(24726): threadid=1: thread exiting with uncaught exception (group=0x40ab9228)
11-25 22:02:30.438: E/AndroidRuntime(24726): FATAL EXCEPTION: main
11-25 22:02:30.438: E/AndroidRuntime(24726): android.database.sqlite.SQLiteException: near "helloDB": syntax error: , while compiling: CREATE TABLE hello helloDB (_id INTEGER PRIMARY KEY AUTOINCREMENT, question TEXT NOT NULL, path TEXT NOT NULL);
11-25 22:02:30.438: E/AndroidRuntime(24726):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
11-25 22:02:30.438: E/AndroidRuntime(24726):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
11-25 22:02:30.438: E/AndroidRuntime(24726):    at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:141)
11-25 22:02:30.438: E/AndroidRuntime(24726):    at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:368)
11-25 22:02:30.438: E/AndroidRuntime(24726):    at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:272)
11-25 22:02:30.438: E/AndroidRuntime(24726):    at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:84)
11-25 22:02:30.438: E/AndroidRuntime(24726):    at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:2031)
11-25 22:02:30.438: E/AndroidRuntime(24726):    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1971)
11-25 22:02:30.438: E/AndroidRuntime(24726):    at com.mitterederStudios.Quiz.Table$DbHelper.createNew(Table.java:49)
11-25 22:02:30.438: E/AndroidRuntime(24726):    at com.mitterederStudios.Quiz.Table.createNew(Table.java:94)
11-25 22:02:30.438: E/AndroidRuntime(24726):    at com.mitterederStudios.Quiz.Menu$1.onClick(Menu.java:56)
11-25 22:02:30.438: E/AndroidRuntime(24726):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:174)
11-25 22:02:30.438: E/AndroidRuntime(24726):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 22:02:30.438: E/AndroidRuntime(24726):    at android.os.Looper.loop(Looper.java:154)
11-25 22:02:30.438: E/AndroidRuntime(24726):    at android.app.ActivityThread.main(ActivityThread.java:4945)
11-25 22:02:30.438: E/AndroidRuntime(24726):    at java.lang.reflect.Method.invokeNative(Native Method)
11-25 22:02:30.438: E/AndroidRuntime(24726):    at java.lang.reflect.Method.invoke(Method.java:511)
11-25 22:02:30.438: E/AndroidRuntime(24726):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-25 22:02:30.438: E/AndroidRuntime(24726):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-25 22:02:30.438: E/AndroidRuntime(24726):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:4)

如果你真的想要空格,你需要在表名周围加上引号,使其为“test 1”。然而,这通常是一个坏主意,我会用下划线替换空格,因此它变为test_1。