Reputation: 306
I am creating an application where a table gets generated in SQLite Database as soon as a new user registers, but I can create tables only in the onCreate() method in the SQLiteOpenHelper class. I am using this user-defined function to create tables dynamically :
public void createUserTable(DatabaseOperations d,String user){
String USER_Q = "CREATE TABLE " + user + "(" + UserInfo.NOTES + " text);";
SQLiteDatabase dp = d.getWritableDatabase();
dp.execSQL(USER_Q);
}
but it does not work as I get the error while retreiving the notes column from the user table:
02-23 18:17:35.499 7292-7292/? E/SQLiteLog: (1) no such table: Bundle
02-23 18:17:35.500 7292-7292/? D/AndroidRuntime: Shutting down VM
02-23 18:17:35.507 7292-7292/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.himanshu.sqlregistration, PID: 7292
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.himanshu.sqlregistration/com.example.himanshu.sqlregistration.secretMessage}: android.database.sqlite.SQLiteException: no such table: Bundle (code 1): , while compiling: SELECT notes FROM Bundle[mParcelledData.dataSize=72]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
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:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: android.database.sqlite.SQLiteException: no such table: Bundle (code 1): , while compiling: SELECT notes FROM Bundle[mParcelledData.dataSize=72]
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.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
at com.example.himanshu.sqlregistration.DatabaseOperations.extractNotesFromUser(DatabaseOperations.java:44)
at com.example.himanshu.sqlregistration.secretMessage.onCreate(secretMessage.java:24)
at android.app.Activity.performCreate(Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
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:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Code for retreiving columns from table:
public Cursor extractNotesFromUser(DatabaseOperations d, String user) {
SQLiteDatabase dp = d.getReadableDatabase();
String column[] = {UserInfo.NOTES};
Cursor c = dp.query(user, column, null, null, null, null, null);
return c;
}
How can I create new tables with or without using the onCreate() method?
Upvotes: 2
Views: 9516
Reputation: 657
Error show that your table is not created, because you have put semicolon (;)
at the end of query so write proper like below, its create runtime when you call this function.
public void createUserTable(DatabaseOperations d, String user) {
final SQLiteDatabase db = getWritableDatabase();
String CREATE_TABLE_NEW_USER = "CREATE TABLE " + user + " (" + UserInfo.NOTES + " TEXT)";
db.execSQL(CREATE_TABLE_NEW_USER);
db.close();
}
Upvotes: 3