markdowneide 2013-05-06 09:26 采纳率: 0%
浏览 9408
已采纳

运行 SQLite 数据库后程序崩溃

我设计的是查询数据库后下面的程序段返回一个数组。但是现在运行下面的程序时,奔溃了

//info 是类型数据中对象的名字
    info.open();
    String[] data = info.queryAll();
    info.close();

数据库代码部分,我想检索某一列中所有数据库的行

public String[] queryAll() {
    String[] columns = new String[] { KEY_NAME };
    Cursor cursor = ourDatabase.query(DATABASE_TABLE, columns, null, null,
            null, null, null);
    if (cursor != null) {
        try {
            final int nameColumnIndex =                     cursor.getColumnIndex(KEY_NAME);
            List<String> names = new ArrayList<String>();
            while (cursor.moveToNext()) {
                names.add(cursor.getString(nameColumnIndex));
            }
            return names.toArray(new String[names.size()]);
        } finally {
            cursor.close();
        }
    }
    return null;
}

是因为数据库刚开始是空的吗?

LOGCAT

04-23 22:26:47.780: E/AndroidRuntime(2825): FATAL EXCEPTION: main
04-23 22:26:47.780: E/AndroidRuntime(2825): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.contactlist/com.example.contactlist.Contacts}: android.database.sqlite.SQLiteException: no such table: mycontacts (code 1): , while compiling: SELECT Contact_name FROM mycontacts
04-23 22:26:47.780: E/AndroidRuntime(2825):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at android.os.Looper.loop(Looper.java:137)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at android.app.ActivityThread.main(ActivityThread.java:4745)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at java.lang.reflect.Method.invokeNative(Native Method)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at java.lang.reflect.Method.invoke(Method.java:511)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at dalvik.system.NativeStart.main(Native Method)
04-23 22:26:47.780: E/AndroidRuntime(2825): Caused by: android.database.sqlite.SQLiteException: no such table: mycontacts (code 1): , while compiling: SELECT Contact_name FROM mycontacts
04-23 22:26:47.780: E/AndroidRuntime(2825):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1200)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at com.example.contactlist.DBContact.queryAll(DBContact.java:97)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at com.example.contactlist.Contacts.onCreate(Contacts.java:38)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at android.app.Activity.performCreate(Activity.java:5008)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-23 22:26:47.780: E/AndroidRuntime(2825):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-23 22:26:47.780: E/AndroidRuntime(2825):     ... 11 more
  • 写回答

6条回答 默认 最新

  • yongyong_21 2013-05-07 05:33
    关注
     private static final String DATABASE_CREATE = "create table contacts (_id integer primary key autoincrement, "
                    + "Contact_name text not null, Personal_Phonenumber text not null, Home_Phonenumber text not null, Office_Phone text not null); ";
    

    上面代码中的 Office_Phone 与申明不符。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 请问如何在matlab里使用raven工具?
  • ¥100 关于ios手游充值到一定金额,再点充值提示公众号的问题。
  • ¥15 求一个stm32串口控制程序
  • ¥20 Windows 驱动开发版本疑问相关
  • ¥15 MAC 未能打开磁盘映像
  • ¥15 fastcap使用,二维导体输入问题
  • ¥15 hosts修改后不能访问
  • ¥15 关于化学反应速率C++编译问题/FLUENT
  • ¥20 Yolov5训练报错
  • ¥15 Unity发布gzip压缩的webgl之后让浏览器可以正常显示画面