无法从SQLite数据库中提取数据并在活动中显示

时间:2012-12-12 15:32:32

标签: android sqlite

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

RestaurantDB源文件

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):来自nativeGetEnabledTags的意外值:0
  12-13 14:08:52.805:W / Trace(3861):来自nativeGetEnabledTags的意外值:0
  12-13 14:08:54.795:D / dalvikvm(3861):GC_FOR_ALLOC释放68K,7%免费2670K / 2860K,暂停139ms,总计143ms
  12-13 14:08:54.826:I / dalvikvm-heap(3861):将堆(frag case)增长到3.796MB,用于1127536字节分配
  12-13 14:08:54.946:D / dalvikvm(3861):GC_FOR_ALLOC释放1K,5%释放3770K / 3964K,暂停112ms,总计112ms
  12-13 14:08:55.145:D / dalvikvm(3861):GC_CONCURRENT释放&lt; 1K,5%free 3785K / 3964K,暂停12ms + 27ms,总计203ms
  12-13 14:08:55.618:D / AndroidRuntime(3861):关闭VM
  12-13 14:08:55.618:W / dalvikvm(3861):threadid = 1:线程退出未捕获异常(组= 0x40a70930)
  12-13 14:08:55.715:E / AndroidRuntime(3861):致命异常:主要
  12-13 14:08:55.715:E / AndroidRuntime(3861):java.lang.RuntimeException:无法启动活动ComponentInfo {com.demostudio.restaurants / com.demostudio.restaurants.AllRestaurants}:java.lang.IllegalArgumentException:column '_id'不存在
  12-13 14:08:55.715:E / AndroidRuntime(3861):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
  12-13 14:08:55.715:E / AndroidRuntime(3861):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
  12-13 14:08:55.715:E / AndroidRuntime(3861):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
  12-13 14:08:55.715:E / AndroidRuntime(3861):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)
  12-13 14:08:55.715:E / AndroidRuntime(3861):在android.os.Handler.dispatchMessage(Handler.java:99)
  12-13 14:08:55.715:E / AndroidRuntime(3861):在android.os.Looper.loop(Looper.java:137)
  12-13 14:08:55.715:E / AndroidRuntime(3861):在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):引起:java.lang.IllegalArgumentException:列'_id'不存在
  12-13 14:08:55.715:E / AndroidRuntime(3861):在android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
  12-13 14:08:55.715:E / AndroidRuntime(3861):在android.support.v4.widget.CursorAdapter.init(CursorAdapter.java:174)
  12-13 14:08:55.715:E / AndroidRuntime(3861):在android.support.v4.widget.CursorAdapter。(CursorAdapter.java:151)
  12-13 14:08:55.715:E / AndroidRuntime(3861):在android.support.v4.widget.ResourceCursorAdapter。(ResourceCursorAdapter.java:93)
  12-13 14:08:55.715:E / AndroidRuntime(3861):在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):在android.app.Activity.performCreate(Activity.java:5104)
  12-13 14:08:55.715:E / AndroidRuntime(3861):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
  12-13 14:08:55.715:E / AndroidRuntime(3861):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
  12-13 14:08:55.715:E / AndroidRuntime(3861):... 11更多

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

3 个答案:

答案 0 :(得分:2)

在你帮助者的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中的列名称前面有colCREATE TABLE中没有。这不起作用,因为您需要列名一致。

您是否更改了CREATE TABLE声明或SELECT声明取决于您。

答案 1 :(得分:0)

你又做了。如果您需要数据库实例化字段,请将RestaurantDB db=new RestaurantDB(AllRestaurants.this);更改为db=new RestaurantDB(AllRestaurants.this);

答案 2 :(得分:0)

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

我应该使用以下代码:

private Cursor doQuery() {
return(db.getReadableDatabase().rawQuery("SELECT RestaurantID AS _id, RestaurantName, StoreNo "
+ "FROM RestaurantList ORDER BY RestaurantName", null));
}