在创建Android上的SQLite数据库表

在创建Android上的SQLite数据库表

本文介绍了在创建Android上的SQLite数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@覆盖
        公共无效的onCreate(SQLiteDatabase DB)
        {
            尝试{
                db.execSQL(CREATE TABLE+ NotificationManager.getUserStatic(上下文)+日志(+
                KEY_TIME +INTEGER主键,+ KEY_TEXT +TEXT NOT NULL););

            }
            赶上(的SQLException E){
                e.printStackTrace();
            }
        }

        / **
         *当应用程序被打开称为的OnOpen方法。
         * /

        @覆盖
        公共无效的OnOpen(SQLiteDatabase DB)
        {
            尝试{
                的System.out.println(鼎好);
                db.execSQL(创建表,如果不存在+ DATABASE_NAME +。+ NotificationManager.getUserStatic(上下文)+日志(+
                KEY_TIME +INTEGER主键,+ KEY_TEXT +TEXT NOT NULL););
                的System.out.println(downhere);
            }
            赶上(的SQLException E){
                e.printStackTrace();
            }
        }

我们有这个$ C $下一个应用程序创建一个数据库。问题是,据我们所知道的,它不会创建表,所以当我们尝试插入到表中并从中读取,它崩溃。我们已经尝试了一切,在的System.out.println的在那里看到它失败。我们得到的鼎好出来,但它从来没有获取到日志中downhere一部分,所以大家都在猜测什么导致它出现故障。我们已经检查了DDMS数据库是在正确的文件夹,所以数据库应该有,但由于某些原因,它不能找到它(日志中的2号线和3)。

有什么想法?

这是错误日志:

10月4日至二十七号:45:46.768:我/的System.out(6441):鼎好
10月4日至二十七号:45:46.772:I / SqliteDatabaseCpp(6441):源码返回:错误code = 1,MSG =未知数据库NOPO,DB = /数据/数据​​/ dmri.nopo /数据库/ NOPO
10月4日至二十七号:45:46.772:W / System.err的(6441):android.database.sqlite.SQLiteException:未知数据库NOPO:在编制:如果不存在NOPO.log创建表(时间INTEGER主键,文本文本NOT NULL);
10月4日至二十七号:45:46.792:W / System.err的(6441):在android.database.sqlite.SQLiteCompiledSql.native_compile(本机方法)
10月4日至二十七号:45:46.792:W / System.err的(6441):在android.database.sqlite.SQLiteCompiledSql(SQLiteCompiledSql.java:68)。
10月4日至二十七号:45:46.811:W / System.err的(6441):在android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:134)
10月4日至二十七号:45:46.811:W / System.err的(6441):在android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
10月4日至二十七号:45:46.811:W / System.err的(6441):在android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
10月4日至二十七号:45:46.811:W / System.err的(6441):在android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:84)
10月4日至二十七号:45:46.811:W / System.err的(6441):在android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1899)
10月4日至二十七号:45:46.823:W / System.err的(6441):在android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1839)
10月4日至二十七号:45:46.823:W / System.err的(6441):在dmri.nopo.DBAdapter $ DatabaseHelper.onOpen(DBAdapter.java:67)
10月4日至二十七号:45:46.823:W / System.err的(6441):在android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:180)
10月4日至二十七号:45:46.902:W / System.err的(6441):在dmri.nopo.DBAdapter.open(DBAdapter.java:86)
10月4日至二十七号:45:46.912:W / System.err的(6441):在dmri.nopo.LogManager.readLogFile(LogManager.java:32)
10月4日至二十七号:45:46.912:W / System.err的(6441):在dmri.nopo.LogActivity.onCreate(LogActivity.java:25)
10月4日至二十七号:45:46.921:W / System.err的(6441):在android.app.Activity.performCreate(Activity.java:4465)
10月4日至二十七号:45:46.921:W / System.err的(6441):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10月4日至二十七号:45:46.931:W / System.err的(6441):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
10月4日至二十七号:45:46.931:W / System.err的(6441):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10月4日至二十七号:45:46.941:W / System.err的(6441):在android.app.ActivityThread.access $ 600(ActivityThread.java:123)
10月4日至二十七号:45:46.941:W / System.err的(6441):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147)
10月4日至二十七号:45:46.953:W / System.err的(6441):在android.os.Handler.dispatchMessage(Handler.java:99)
10月4日至二十七号:45:46.953:W / System.err的(6441):在android.os.Looper.loop(Looper.java:137)
10月4日至二十七号:45:46.972:W / System.err的(6441):在android.app.ActivityThread.main(ActivityThread.java:4424)
10月4日至二十七号:45:46.972:W / System.err的(6441):在java.lang.reflect.Method.invokeNative(本机方法)
10月4日至二十七号:45:46.972:W / System.err的(6441):在java.lang.reflect.Method.invoke(Method.java:511)
10月4日至二十七号:45:46.982:W / System.err的(6441):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
10月4日至二十七号:45:46.982:W / System.err的(6441):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10月4日至二十七号:45:46.992:W / System.err的(6441):在dalvik.system.NativeStart.main(本机方法)
10月4日至二十七号:45:47.002:I / SqliteDatabaseCpp(6441):源码返回:错误code = 1,味精=没有这样的表格:登录,DB = /数据/数据​​/ dmri.nopo /数据库/ NOPO
10月4日至二十七号:45:47.012:D / AndroidRuntime(6441):关闭虚拟机
10月4日至二十七号:45:47.012:W / dalvikvm(6441):主题ID = 1:螺纹退出与未捕获的异常(组= 0x409c01f8)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):致命异常:主要
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):java.lang.RuntimeException的:无法启动的活动ComponentInfo {dmri.nopo / dmri.nopo.LogActivity}:android.database.sqlite.SQLiteException:没有这样的表:登录:在编制:选择时间,从文本日志
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在android.app.ActivityThread.access $ 600(ActivityThread.java:123)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1147)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在android.os.Handler.dispatchMessage(Handler.java:99)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在android.os.Looper.loop(Looper.java:137)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在android.app.ActivityThread.main(ActivityThread.java:4424)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在java.lang.reflect.Method.invokeNative(本机方法)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在java.lang.reflect.Method.invoke(Method.java:511)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在dalvik.system.NativeStart.main(本机方法)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):android.database.sqlite.SQLiteException:没有这样的表:记录:在编制:选择时间,从文本日志产生的原因
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在android.database.sqlite.SQLiteCompiledSql.native_compile(本机方法)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在android.database.sqlite.SQLiteCompiledSql(SQLiteCompiledSql.java:68)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在android.database.sqlite.SQLiteProgram(SQLiteProgram.java:127)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在android.database.sqlite.SQLiteProgram(SQLiteProgram.java:94)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在android.database.sqlite.SQLiteQuery(SQLiteQuery.java:53)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1449)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1485)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在dmri.nopo.DBAdapter.getAllSMS(DBAdapter.java:116)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在dmri.nopo.LogManager.readLogFile(LogManager.java:34)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在dmri.nopo.LogActivity.onCreate(LogActivity.java:25)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在android.app.Activity.performCreate(Activity.java:4465)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
10月4日至二十七号:45:47.181:E / AndroidRuntime(6441):11 ...更多

解决方案

首先,我会建议使用android.util.Log在Android的日志记录异常。

二 - 我怀疑你有创建错误的名表。你的错误表示查询无法找到日志,但我看到你做的创建语句一些串联。这可能是这个原因。

您可以检查什么是真正为您创建。通过查看创建的sqlite的基础。

您可以试试:

  1. 亚行外壳
  2. CD /data/data/<your.package.name>/databases
  3. sqlite3的&LT; yourdbname&GT;
  4. .tables

@Override
        public void onCreate(SQLiteDatabase db)
        {
            try{
                db.execSQL("create table " +          NotificationManager.getUserStatic(context) + "log ("+
                KEY_TIME +" INTEGER primary key, "+ KEY_TEXT +" TEXT not null);");

            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }

        /**
         * onOpen method called when app is opening.
         */

        @Override
        public void onOpen(SQLiteDatabase db)
        {
            try{
                System.out.println("tophere");
                db.execSQL("create table if not exists "+DATABASE_NAME+"." + NotificationManager.getUserStatic(context) + "log ("+
                KEY_TIME +" INTEGER primary key, "+ KEY_TEXT +" TEXT not null);");
                System.out.println("downhere");
            }
            catch (SQLException e){
                e.printStackTrace();
            }
        }

We have this code for creating a database for an app. problem is, as far as we can tell, it doesn't create the tables, so when we try to insert into the tables and read from them, it crashes. we have tried everything, the System.out.println's are there to see where it fails. we get the tophere out, but it never gets to the downhere part in the log, so we are guessing something is causing it to fail there. we have checked with DDMS that the database is in the correct folder, so the database should be there, but for some reason it cant find it(line 2 and 3 in the log).

Any thoughts?

this is the error log:

04-27 10:45:46.768: I/System.out(6441): tophere
04-27 10:45:46.772: I/SqliteDatabaseCpp(6441): sqlite returned: error code = 1, msg = unknown database NOPO, db=/data/data/dmri.nopo/databases/NOPO
04-27 10:45:46.772: W/System.err(6441): android.database.sqlite.SQLiteException: unknown database NOPO: , while compiling: create table if not exists NOPO.log (time INTEGER primary key, text TEXT not null);
04-27 10:45:46.792: W/System.err(6441):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
04-27 10:45:46.792: W/System.err(6441):     at android.database.sqlite.SQLiteCompiledSql.(SQLiteCompiledSql.java:68)
04-27 10:45:46.811: W/System.err(6441):     at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:134)
04-27 10:45:46.811: W/System.err(6441):     at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
04-27 10:45:46.811: W/System.err(6441):     at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
04-27 10:45:46.811: W/System.err(6441):     at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:84)
04-27 10:45:46.811: W/System.err(6441):     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1899)
04-27 10:45:46.823: W/System.err(6441):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1839)
04-27 10:45:46.823: W/System.err(6441):     at dmri.nopo.DBAdapter$DatabaseHelper.onOpen(DBAdapter.java:67)
04-27 10:45:46.823: W/System.err(6441):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:180)
04-27 10:45:46.902: W/System.err(6441):     at dmri.nopo.DBAdapter.open(DBAdapter.java:86)
04-27 10:45:46.912: W/System.err(6441):     at dmri.nopo.LogManager.readLogFile(LogManager.java:32)
04-27 10:45:46.912: W/System.err(6441):     at dmri.nopo.LogActivity.onCreate(LogActivity.java:25)
04-27 10:45:46.921: W/System.err(6441):     at android.app.Activity.performCreate(Activity.java:4465)
04-27 10:45:46.921: W/System.err(6441):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-27 10:45:46.931: W/System.err(6441):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-27 10:45:46.931: W/System.err(6441):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-27 10:45:46.941: W/System.err(6441):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-27 10:45:46.941: W/System.err(6441):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-27 10:45:46.953: W/System.err(6441):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-27 10:45:46.953: W/System.err(6441):     at android.os.Looper.loop(Looper.java:137)
04-27 10:45:46.972: W/System.err(6441):     at android.app.ActivityThread.main(ActivityThread.java:4424)
04-27 10:45:46.972: W/System.err(6441):     at java.lang.reflect.Method.invokeNative(Native Method)
04-27 10:45:46.972: W/System.err(6441):     at java.lang.reflect.Method.invoke(Method.java:511)
04-27 10:45:46.982: W/System.err(6441):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-27 10:45:46.982: W/System.err(6441):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-27 10:45:46.992: W/System.err(6441):     at dalvik.system.NativeStart.main(Native Method)
04-27 10:45:47.002: I/SqliteDatabaseCpp(6441): sqlite returned: error code = 1, msg = no such table: log, db=/data/data/dmri.nopo/databases/NOPO
04-27 10:45:47.012: D/AndroidRuntime(6441): Shutting down VM
04-27 10:45:47.012: W/dalvikvm(6441): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-27 10:45:47.181: E/AndroidRuntime(6441): FATAL EXCEPTION: main
04-27 10:45:47.181: E/AndroidRuntime(6441): java.lang.RuntimeException: Unable to start activity ComponentInfo{dmri.nopo/dmri.nopo.LogActivity}: android.database.sqlite.SQLiteException: no such table: log: , while compiling: SELECT time, text FROM log
04-27 10:45:47.181: E/AndroidRuntime(6441):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at android.os.Looper.loop(Looper.java:137)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at android.app.ActivityThread.main(ActivityThread.java:4424)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at java.lang.reflect.Method.invokeNative(Native Method)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at java.lang.reflect.Method.invoke(Method.java:511)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at dalvik.system.NativeStart.main(Native Method)
04-27 10:45:47.181: E/AndroidRuntime(6441): Caused by: android.database.sqlite.SQLiteException: no such table: log: , while compiling: SELECT time, text FROM log
04-27 10:45:47.181: E/AndroidRuntime(6441):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at android.database.sqlite.SQLiteCompiledSql.(SQLiteCompiledSql.java:68)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:127)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:94)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:53)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1449)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1485)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at dmri.nopo.DBAdapter.getAllSMS(DBAdapter.java:116)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at dmri.nopo.LogManager.readLogFile(LogManager.java:34)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at dmri.nopo.LogActivity.onCreate(LogActivity.java:25)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at android.app.Activity.performCreate(Activity.java:4465)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-27 10:45:47.181: E/AndroidRuntime(6441):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-27 10:45:47.181: E/AndroidRuntime(6441):     ... 11 more
解决方案

First of all I would recommend using android.util.Log for logging exceptions in Android.

Second - I suspect you have tables with wrong names created. Your error says query can't find "log", but I see you make some concatenation in "CREATE" statement. That may be the reason.

You can check what is actually created for you. By viewing the sqlite base created.

You can try:

  1. adb shell
  2. cd /data/data/<your.package.name>/databases
  3. sqlite3 <yourdbname>
  4. .tables

这篇关于在创建Android上的SQLite数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:24