仿真器可以訪問數據庫,但在Android設備不工作。 當我打開時,android設備將自動關閉此應用程序。 我正在使用Google API(Google Inc.) - API Level 10仿真器 package ict.mobile;Android的數據庫問題
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class DatabaseMemberQuery extends Activity implements OnClickListener {
SQLiteDatabase db;
String sql;
Cursor cursor = null;
String[] columns = { "mid", "name", "password", "age" };
TextView tvData;
String dataStrHeader = String.format("%4s %-12s %-9s %3s\n", "Mid", "Name", "Password", "Age");
String dataStr;
Button btnDBinitial, btnAction;
public void findView() {
tvData = (TextView) findViewById(R.id.data);
btnDBinitial = (Button) findViewById(R.id.btnDBinitial);
btnAction = (Button) findViewById(R.id.btnAction);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findView();
btnDBinitial.setOnClickListener(this);
btnAction.setOnClickListener(this);
initialDB();
}
public void onClick(View v) {
if (v.equals(btnDBinitial)) {
initialDB();
}
else if (v.equals(btnAction)) {
try {
db = SQLiteDatabase.openDatabase("/data/data/ict.mobile/MemberDB", null, SQLiteDatabase.OPEN_READONLY);
cursor = db.rawQuery("select * from Member where age >=30 order by mid", null);
int result = cursor.getCount();
Toast.makeText(this, "Count: " + result, Toast.LENGTH_SHORT).show();
dataStr = dataStrHeader;
while (cursor.moveToNext()) {
int mid = cursor.getInt(cursor.getColumnIndex("mid"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String password = cursor.getString(cursor.getColumnIndex("password"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
dataStr += String.format("%4d %-12s %-9s %3d\n", mid, name, password, age);
}
tvData.setText(dataStr);
db.close();
} catch (SQLiteException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
public void initialDB() {
try {
db = SQLiteDatabase.openDatabase("/data/data/ict.mobile/MemberDB", null, SQLiteDatabase.CREATE_IF_NECESSARY);
sql = "DROP TABLE if exists Member;";
db.execSQL(sql);
sql= "CREATE TABLE Member(" + "mid int PRIMARY KEY ," + "name text, "
+ "password text, "+ "age int); ";
db.execSQL(sql);
db.execSQL("INSERT INTO Member(mid, name, password, age) values"
+ "(1001, 'Amy Carl', '12345', 16); ");
db.execSQL("INSERT INTO Member(mid, name, password, age) values"
+ "(1002, 'Helen Leung', '88888', 25); ");
db.execSQL("INSERT INTO Member(mid, name, password, age) values"
+ "(1003, 'Robert Chan', 'iloveu', 61); ");
db.execSQL("INSERT INTO Member(mid, name, password, age) values"
+ "(1004, 'Carol Wong', 'peterpan', 33); ");
db.execSQL("INSERT INTO Member(mid, name, password, age) values"
+ "(1005, 'Carman Wong', 'pooh', 44); ");
db.execSQL("INSERT INTO Member(mid, name, password, age) values"
+ "(1006, 'John Chan', 'johnchan', 28); ");
db.execSQL("INSERT INTO Member(mid, name, password, age) values"
+ "(1007, 'Paul Lam', 'apple', 16); ");
cursor = db.rawQuery("select * from Member order by mid", null);
dataStr = dataStrHeader;
while (cursor.moveToNext()) {
int mid = cursor.getInt(cursor.getColumnIndex("mid"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String password = cursor.getString(cursor.getColumnIndex("password"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
dataStr += String.format("%4d %-12s %-9s %3d\n", mid, name, password, age);
}
tvData.setText(dataStr);
Toast.makeText(this, "Table Member is created and initialised.", Toast.LENGTH_SHORT).show();
db.close();
}
catch (SQLiteException e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
顯示的logcat輸出:http://developer.android.com/tools/help/logcat.html –