gpt4 book ai didi

android - 无法从 SQLite 数据库中提取数据并显示在 Activity 中

转载 作者:行者123 更新时间:2023-11-29 21:53:30 25 4
gpt4 key购买 nike

我有一个 SQLiteHelper 源文件,我用它来创建数据库等等。我有另一个源文件需要实例化数据库、提取数据并显示到 UI。但是,当我在模拟器上启动应用程序时,应用程序崩溃了。我不确定出了什么问题。源文件中没有给出错误。

餐厅数据库源文件

package com.demostudio.restaurants;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class RestaurantDB extends SQLiteOpenHelper {

static final String dbName = "RestaurantDB";
static final Integer dbVersion = 1;

static final String restaurantListTable = "RestaurantList";
static final String colRestaurantID = "RestaurantID";
static final String colRestaurantName = "RestaurantName";
static final String colRestaurantStore = "StoreNo";

public RestaurantDB(Context context) {
super(context, dbName, null, dbVersion);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub

String newTableSQL =
"CREATE TABLE "+
restaurantListTable+
" ("+colRestaurantID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
colRestaurantName+" TEXT NOT NULL, "+
colRestaurantStore+" TEXT NOT NULL "+
");";
db.execSQL(newTableSQL);

ContentValues cv = new ContentValues();
cv.put(colRestaurantName, "Restaurant AAA");
cv.put(colRestaurantStore, "1");
db.insert(restaurantListTable, null, cv);

cv.put(colRestaurantName, "Restaurant BBB");
cv.put(colRestaurantStore, "2");
db.insert(restaurantListTable, null, cv);

db.close();

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}
}

AllRestaurants源文件

package com.demostudio.restaurants;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;

public class AllRestaurants extends ListActivity {
RestaurantDB db = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
db=new RestaurantDB(AllRestaurants.this);
onPostExecute( ) ;
}

private Cursor doQuery() {
return(db.getReadableDatabase().rawQuery("SELECT colRestaurantID AS _id, colRestaurantName, colRestaurantStore "
+ "FROM RestaurantListTable ORDER BY colRestaurantName", null));

}

public void onPostExecute( ) {
SimpleCursorAdapter adapter;

if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
adapter=new SimpleCursorAdapter(this, R.layout.activity_all_restaurants,
doQuery(), new String[] {
RestaurantDB.colRestaurantName,
RestaurantDB.colRestaurantStore },
new int[] { R.id.title, R.id.value },
0);
}
else {
adapter=new SimpleCursorAdapter(this, R.layout.activity_all_restaurants,
doQuery(), new String[] {
RestaurantDB.colRestaurantName,
RestaurantDB.colRestaurantStore },
new int[] { R.id.title, R.id.value });
}


setListAdapter(adapter);
}
}

Activity ListView XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ListView
android:id="@android:id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>

activity_all_restaurants XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textSize="20sp"
android:textStyle="bold"/>

<TextView
android:id="@+id/value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textSize="20sp"
android:textStyle="bold"/>

</RelativeLayout>

日志文件

12-13 14:08:52.805: W/Trace(3861): Unexpected value from nativeGetEnabledTags: 0
12-13 14:08:52.805: W/Trace(3861): Unexpected value from nativeGetEnabledTags: 0
12-13 14:08:54.795: D/dalvikvm(3861): GC_FOR_ALLOC freed 68K, 7% free 2670K/2860K, paused 139ms, total 143ms
12-13 14:08:54.826: I/dalvikvm-heap(3861): Grow heap (frag case) to 3.796MB for 1127536-byte allocation
12-13 14:08:54.946: D/dalvikvm(3861): GC_FOR_ALLOC freed 1K, 5% free 3770K/3964K, paused 112ms, total 112ms
12-13 14:08:55.145: D/dalvikvm(3861): GC_CONCURRENT freed <1K, 5% free 3785K/3964K, paused 12ms+27ms, total 203ms
12-13 14:08:55.618: D/AndroidRuntime(3861): Shutting down VM
12-13 14:08:55.618: W/dalvikvm(3861): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
12-13 14:08:55.715: E/AndroidRuntime(3861): FATAL EXCEPTION: main
12-13 14:08:55.715: E/AndroidRuntime(3861): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.demostudio.restaurants/com.demostudio.restaurants.AllRestaurants}: java.lang.IllegalArgumentException: column '_id' does not exist
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.os.Looper.loop(Looper.java:137)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.app.ActivityThread.main(ActivityThread.java:5039)
12-13 14:08:55.715: E/AndroidRuntime(3861): at java.lang.reflect.Method.invokeNative(Native Method)
12-13 14:08:55.715: E/AndroidRuntime(3861): at java.lang.reflect.Method.invoke(Method.java:511)
12-13 14:08:55.715: E/AndroidRuntime(3861): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-13 14:08:55.715: E/AndroidRuntime(3861): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-13 14:08:55.715: E/AndroidRuntime(3861): at dalvik.system.NativeStart.main(Native Method)
12-13 14:08:55.715: E/AndroidRuntime(3861): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.support.v4.widget.CursorAdapter.init(CursorAdapter.java:174)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.support.v4.widget.CursorAdapter.(CursorAdapter.java:151)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.support.v4.widget.ResourceCursorAdapter.(ResourceCursorAdapter.java:93)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.support.v4.widget.SimpleCursorAdapter.(SimpleCursorAdapter.java:89)
12-13 14:08:55.715: E/AndroidRuntime(3861): at com.demostudio.restaurants.AllRestaurants.onPostExecute(AllRestaurants.java:27)
12-13 14:08:55.715: E/AndroidRuntime(3861): at com.demostudio.restaurants.AllRestaurants.onCreate(AllRestaurants.java:18)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.app.Activity.performCreate(Activity.java:5104)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-13 14:08:55.715: E/AndroidRuntime(3861): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-13 14:08:55.715: E/AndroidRuntime(3861): ... 11 more

我之前将 listview 和两个 textview 放在同一个 XML 文件中,但是没有用。所以现在我把它放在一个单独的 XML 文件中,但它仍然不起作用。

最佳答案

在你的助手的 onCreate() 中,你有:

String newTableSQL = 
"CREATE TABLE "+
`+
" ("+colRestaurantID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
colRestaurantName+" TEXT NOT NULL, "+
colRestaurantStore+" TEXT NOT NULL "+
");";

注意您是如何连接字符串来创建完整语句的。生成的 SQL 将是:

CREATE TABLE RestaurantList (RestaurantID INTEGER PRIMARY KEY AUTOINCREMENT, RestaurantName TEXT NOT NULL, RestaurantStore TEXT NOT NULL );

基于您对 restaurantListTablecolRestaurantIDcolRestaurantNamecolRestaurantStore 的值。

但是,在您的 rawQuery() 调用中,您的 SQL 是这样的:

SELECT colRestaurantID AS _id, colRestaurantName, colRestaurantStore FROM RestaurantListTable ORDER BY  colRestaurantName

请注意,您在 rawQuery() SQL 中的列名前面有 col,而在您的 CREATE TABLE。这行不通,因为您需要列名保持一致。

是更改CREATE TABLE 语句还是更改SELECT 语句由您决定。

关于android - 无法从 SQLite 数据库中提取数据并显示在 Activity 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13843126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com