如何在Android中的SQLite中动态创建表? [英] How Do I Create Tables Dynamically in SQLite in Android?

查看:31
本文介绍了如何在Android中的SQLite中动态创建表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,只要新用户注册,就会在 SQLite 数据库中生成一个表,但我只能在 SQLiteOpenHelper 类的 onCreate() 方法中创建表.我正在使用这个用户定义的函数动态创建表:

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);
}

但它不起作用,因为我在从用户表中检索 notes 列时收到错误:

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) 

从表中检索列的代码:

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;
}

如何使用或不使用 onCreate() 方法创建新表?

How can I create new tables with or without using the onCreate() method?

推荐答案

Error show that your table is not created, because you have put 分号 (;)查询结束,所以写得像下面一样,当你调用这个函数时它会创建运行时.

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();
}

这篇关于如何在Android中的SQLite中动态创建表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆