我正在创建一个应用程序,只要新用户注册,就会在sqlite数据库中生成一个表,但我只能在sqliteOpenHelper类的onCreate()方法中创建表.
我正在使用这个用户定义的函数动态创建表:
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列时得到错误:
02-23 18:17:35.499 7292-7292/? E/sqliteLog: (1) no such table: Bundle02-23 18:17:35.500 7292-7292/? D/AndroIDRuntime: Shutting down VM02-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.access0(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.access0(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()方法创建新表?
解决方法:
错误显示您的表未创建,因为您在查询结尾处放置了分号(;),因此请在下面进行正确编写,并在调用此函数时创建运行时.
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中动态创建表?全部内容,希望文章能够帮你解决如何在Android中的SQLite中动态创建表?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)