Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法从SQLite数据库中提取数据并在活动中显示_Android_Sqlite - Fatal编程技术网

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

Android 无法从SQLite数据库中提取数据并在活动中显示,android,sqlite,Android,Sqlite,我有一个SQLiteHelper源文件,我用它来创建数据库和所有这些。我有另一个源文件,它需要实例化数据库、提取数据并显示到UI。但是,当我在emulator上启动应用程序时,应用程序崩溃。我不知道怎么了。源文件中未给出任何错误 RestaurantDB源文件 package com.demostudio.restaurants; import android.content.ContentValues; import android.content.Context; import andro

我有一个SQLiteHelper源文件,我用它来创建数据库和所有这些。我有另一个源文件,它需要实例化数据库、提取数据并显示到UI。但是,当我在emulator上启动应用程序时,应用程序崩溃。我不知道怎么了。源文件中未给出任何错误

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

}
}
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);
}
}
所有源文件

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

}
}
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);
}
}
活动列表视图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>

活动所有餐厅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/跟踪(3861):来自nativeGetEnabledTags的意外值:0
12-13 14:08:52.805:W/跟踪(3861):来自nativeGetEnabledTags的意外值:0
12-13 14:08:54.795:D/dalvikvm(3861):释放68K的所有元素的GC,7%的自由元素2670K/2860K,暂停139ms,总143ms
12-13 14:08:54.826:I/dalvikvm堆(3861):为1127536字节分配将堆(frag案例)增长到3.796MB
12-13 14:08:54.946:D/dalvikvm(3861):释放1K的所有,5%的释放3770K/3964K,暂停112ms,总计112ms

12-13 14:08:55.145:D/dalvikvm(3861):你又做了一次。如果需要DB实例化a字段,请更改
RestaurantDB=newrestaurantdb(AllRestaurants.this)
to
db=newrestaurantdb(AllRestaurants.this)

我应该使用以下选项:

private Cursor doQuery() {
return(db.getReadableDatabase().rawQuery("SELECT RestaurantID AS _id, RestaurantName, StoreNo "
+ "FROM RestaurantList ORDER BY RestaurantName", null));
}
在助手的
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 );
基于您的
restaurantListTable
colRestaurantID
colRestaurantName
colRestaurantStore
的值

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

SELECT colRestaurantID AS _id, colRestaurantName, colRestaurantStore FROM RestaurantListTable ORDER BY  colRestaurantName
请注意,
rawQuery()
SQL中的列名前面有
col
,这在
CREATE表中是不存在的。这将不起作用,因为您需要列名称保持一致


您是更改
CREATE TABLE
语句还是更改
SELECT
语句取决于您。

调用
onPostExecute(Void arg0)
方法的位置可能重复?是的,但我没有得到任何有助于解决此问题的答案。@JYLim您调用onPostExecute(Void arg0)的位置方法?我遵循示例代码。我不知道用什么来代替那种方法。这就是应用程序不能工作的原因吗?事实上,我在最新的源文件中更改了它,但它不能工作。谢谢。这很有效。但是,我有db.close();在数据库的onCreate中。如何在活动中打开数据库?显然getReadableDatabase无法打开数据库。我也不能放入db.open()。@JYLim:“显然getReadableDatabase无法打开数据库”-
getReadableDatabase()
是打开数据库进行读取操作的方式。如果需要另外执行写入操作,请使用
getWritableDatabase()