Reputation: 15
I am creating an application that has a default table that stores what the other tables are so that when your trying to put your own input into the app it will store the tables but when you try to add a table to the default table it works perfectly unless there is a space "test" would work perfect "test 1" would crash the app. This is my code retrieving the String that is inputed by the user:
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();
This is my createNew and 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);
}
my createNew() that is executed to make new tables
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);"
);
}
last but not lease, my 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)
Upvotes: 1
Views: 2451
Reputation: 4227
You need to put quotes around your table name so its "test 1" if you really want spaces. However this is generally a bad idea, I would replace the spaces with underscores so it becomes test_1.
Upvotes: 4