0

模拟器可以访问数据库,但不适用于安卓设备。当我打开时,android 设备会自动关闭这个应用程序。我正在使用 Google API (Google Inc.) - API Level 10 模拟器包 ict.mobile;

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();
    }
}

}

4

1 回答 1

0

您的数据库的路径应该是

/data/data/ict.mobile/databases/MemberDB

根据文档

要使用 sqlite3,请在模拟器实例上输入远程 shell,如上所述,然后使用 sqlite3 命令调用该工具。或者,在调用 sqlite3 时,您可以指定要探索的数据库的完整路径。模拟器/设备实例将 SQLite3 数据库存储在文件夹/data/data/< package_name >/databases/中。

这是一个例子:

adb -s emulator-5554 shell
# sqlite3 /data/data/com.example.google.rss.rssexample/databases/rssitems.db`

在使用新路径执行应用程序之前,请从模拟器/设备中删除您的应用程序。

于 2013-06-23T13:53:36.577 回答