As the topic imply i’ve a few problems creating a new table in sqlite. what ever i do with my code i can’t create new table in the db. if i look at the db there’s just the android_metadata tb and nothing else. the code to create a new one is:
...
this.createTable(R.string.nametable, "_id INT PRIMARY KEY, " + "parentID INT, " + "name TEXT");
...
public void createTable(int id, String sql) {
String table = this.getStringById(id);
String _sql = "CREATE TABLE " + table + "(" + sql + ")";
Log.d("Controller.createTable", _sql);
this.mCurrentConnection.rawQuery(_sql, null);
try {
Cursor c = this.Select(id, "*", null);
c.close();
Log.d("Controller.createTable", "create finished");
} catch (Exception e) {
Log.d("Controller.createTable", "can't create table: " + table);
}
}
i open or create the db in the contructor of my app
if(this.mCurrentConnection == null || !this.mCurrentConnection.isOpen()) {
this.mCurrentConnection = this.mContext.openOrCreateDatabase(this.mDB, Context.MODE_PRIVATE , null);
}
I’ve tried everything: changing names of db / tb, reinstall app, placing “;” or not at the end of the command, tried it with a new clean vm.
i did not found any helpful existing post/answer that fix my prob.
if i execute the _sql statement
CREATE TABLE name_table(_id INT PRIMARY KEY, parentID INT, name TEXT)
in phpmyadmin (MySQL) everything works fine.
I’de be happy if you could help me.
regards Alex
EDIT: (answer for a comment below)
@VikramBodicherla: that’s the prob. the create statement itself did not cause any exeption. the select (check) statement does throw an
04-30 07:34:14.834: E/Database(305): Error inserting parentid=-1
name=blabla
04-30 07:34:14.834: E/Database(305): android.database.sqlite.SQLiteException:no such table: name_table: , while compiling: INSERT INTO
name_table(parentid, name) VALUES(?, ?); 04-30 07:34:14.834:
E/Database(305): at
android.database.sqlite.SQLiteProgram.native_compile(Native Method)
04-30 07:34:14.834: E/Database(305): at
android.database.sqlite.SQLiteProgram.compile(SQLiteProgram.java:110)
04-30 07:34:14.834: E/Database(305): at
android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:59)
04-30 07:34:14.834: E/Database(305): at
android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:41)
04-30 07:34:14.834: E/Database(305): at
android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1027)
04-30 07:34:14.834: E/Database(305): at
android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1413)
04-30 07:34:14.834: E/Database(305): at
android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1286)
04-30 07:34:14.834: E/Database(305): at
unicorn.Heurazio.Controller.addName(Controller.java:156) 04-30
07:34:14.834: E/Database(305): at
unicorn.Heurazio.Controller.install(Controller.java:135) 04-30
07:34:14.834: E/Database(305): at
unicorn.Heurazio.StartUpActivity.onCreate(StartUpActivity.java:22)
04-30 07:34:14.834: E/Database(305): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-30 07:34:14.834: E/Database(305): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
04-30 07:34:14.834: E/Database(305): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-30 07:34:14.834: E/Database(305): at
android.app.ActivityThread.access$2200(ActivityThread.java:119) 04-30
07:34:14.834: E/Database(305): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-30 07:34:14.834: E/Database(305): at
android.os.Handler.dispatchMessage(Handler.java:99) 04-30
07:34:14.834: E/Database(305): at
android.os.Looper.loop(Looper.java:123) 04-30 07:34:14.834:
E/Database(305): at
android.app.ActivityThread.main(ActivityThread.java:4363) 04-30
07:34:14.834: E/Database(305): at
java.lang.reflect.Method.invokeNative(Native Method) 04-30
07:34:14.834: E/Database(305): at
java.lang.reflect.Method.invoke(Method.java:521) 04-30 07:34:14.834:
E/Database(305): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.runZygoteInit.java:860)
04-30 07:34:14.834: E/Database(305): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 04-30
07:34:14.834: E/Database(305): at
dalvik.system.NativeStart.main(Native Method)
You really really really want to use the SQLiteOpenHelper class and its associated pattern: it is not just for upgrades, it handles cleanly creating the table(s) as well.
Also, your create statement should probably look like this:
EDIT: from your trace, your CREATE TABLE is never run (hence the table doesn’t exist). use the SQLiteOpenHelper and the
execSQLfunction to create the table and you should be all set.