我正在尝试学习一些android sql数据库的基础知识。所以,我在网上发现了一个例子(我相信尖叫企鹅),它说明了一个列,一个表sql数据库,它使用textveiw来显示它的值。所以我试图通过添加两个列并为它们提供值来扩展这个数据库。我成功地建立了实际的数据库(我从DDMS中取出它并查看它......很好)但我的应用程序一直在我的模拟器中崩溃,我无法弄清楚我做错了什么。下面我有一个活动和一个名为DataHelper和我的主xml文件的附加类。
CartoonDb2 Activity:
package your.cartoon2.namespace;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.util.List;
public class CartoonDb2Activity extends Activity {
private TextView output;
private DataHelper dh;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.output = (TextView) this.findViewById(R.id.out_text);
this.dh = new DataHelper(this);
this.dh.deleteAll();
this.dh.insert("Porky Pig","DaffyDuck", "wolverine");/////this too////////////////
this.dh.insert("Foghorn Leghorn","superman","green lantern");///////////////////
this.dh.insert("Yosemite Sam","bugs bunny","mighty mouse");//////////////////////
List<String> names = this.dh.selectAll();
StringBuilder sb = new StringBuilder();
sb.append("Names in database:\n");
for (String name : names) {
sb.append(name + "\n");
}
Log.d("EXAMPLE", "names size - " + names.size());
this.output.setText(sb.toString());
}
}
DataHelper类:
package your.cartoon2.namespace;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class DataHelper {
private static final String DATABASE_NAME = "example.db";
private static final int DATABASE_VERSION = 2;
private static final String TABLE_NAME = "table1";
private Context context;
private SQLiteDatabase db;
private SQLiteStatement insertStmt;
private static final String INSERT = "insert into " + TABLE_NAME
+ "(name1,name2,name3)values (?,?,?)";
public DataHelper(Context context) {
this.context = context;
OpenHelper openHelper = new OpenHelper(this.context);
this.db = openHelper.getWritableDatabase();
this.insertStmt = this.db.compileStatement(INSERT);
}
public long insert(String name1,String name2, String name3) {
this.insertStmt.bindString(1, name1);
this.insertStmt.bindString(2, name2);
this.insertStmt.bindString(3, name3);
return this.insertStmt.executeInsert();
}
public void deleteAll() {
this.db.delete(TABLE_NAME, null, null);
}
public List<String> selectAll() {
List<String> list = new ArrayList<String>();
Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name1","name2","name3" },
null, null, null, null, "name desc");
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0));
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return list;
}
private static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME
+ " (id INTEGER PRIMARY KEY, name1 text,name2 text,name3 text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("Example",
"Upgrading database, this will drop tables and recreate.");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
}
主xml文件:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:id="@+id/out_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
</ScrollView>
非常感谢任何协助。