2 markdowneide markdowneide 于 2013.05.06 17:26 提问

运行 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
yongyong_21   2013.05.07 13: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 与申明不符。

baimy1985
baimy1985   2013.05.06 21:06
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

这里不是提示表不存在吗?

cj6585256
cj6585256 对啊,log不是直接有提示吗? no such table: mycontacts 没有这个Table
4 年多之前 回复
u010589110
u010589110   2013.05.06 17:45

有可能你的 符号出错了 做程序是需要很小心的

suannai0314
suannai0314 您的这种回答更适合使用评论功能,以后您可以在问题下边进行添加评论
4 年多之前 回复
u010591434
u010591434   2013.05.06 19:51

崩溃了?报错?????????

markdowneide
markdowneide 我贴出 logcat 的输出了呀。
4 年多之前 回复
suannai0314
suannai0314 您的这种回答更适合使用评论功能,以后您可以在问题下边进行添加评论
4 年多之前 回复
poweraeb
poweraeb   2013.05.07 13:15

应该是符号错了。去掉try吧,多加些条件判断。

yezisuifengbiao
yezisuifengbiao   2013.12.13 11:40

你创建表的时候,那些列名的类型有没给少?如果给少了一个,本身就没有类型,插不进数据,怎么查呢?

Csdn user default icon
上传中...
上传图片
插入图片