android - SQLite rawQuery导致问题

时间:2012-01-21 02:26:53

标签: android sqlite

下面发布的是我遇到问题的代码。我已将问题缩小到cursor = db.rawQuery()声明。

为什么声明不正确?数据库的设置似乎正确,但我似乎无法查询它。我尝试了db.query()db.rawQuery()

因为应用程序在物理设备(我用于测试的摩托罗拉Atrix)上运行时只是崩溃了,有可能一些模型没有附带SQLite吗?

public class Database {
    private static final String ID_DATE = "id_date";
    private static final String ROUND = "round";
    private static final String CATEGORY = "category";
    private static final String VALUE = "value";
    private static final String ANSWER = "answer";
    private static final String QUESTION = "question";
    private static final String DB_NAME = "database.db";
    private static final String DB_TABLE = "clues";
    private static final String DB_PATH = "/data/data/es.foo.bar/databases/" + DB_NAME;
    private static final int DB_VERSION = 1;
    private static class DatabaseHelper extends SQLiteOpenHelper {
        private Context c = null;
        DatabaseHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
            this.c = context;
        }
        public void onCreate(SQLiteDatabase db) {
            try {
                InputStream is = c.getAssets().open(DB_NAME);
                OutputStream os = new FileOutputStream(DB_PATH);
                byte[] buffer = new byte[1024];
                while (is.read(buffer) > 0) {
                    os.write(buffer);
                }
                os.flush();
                os.close();
                is.close();
            } catch (java.io.IOException ex) {  }
        }
        public void onUpgrade(SQLiteDatabase db, int old_v, int new_v) {
        }
    }
    private final Context context;
    private DatabaseHelper helpme;
    private SQLiteDatabase db = null;
    public Database(Context c) {
        this.context = c;
    }
    public Database open() throws SQLException {
        helpme = new DatabaseHelper(context);
        db = helpme.getWritableDatabase();
        return this;
    }
    public void close() { helpme.close(); }
    public boolean askfordata() {
        Cursor cursor = null;
        cursor = db.rawQuery("select * from clues where question match \'foo\'", null);
        if (cursor != null) {
            return true;
        }
        return false;
    }
}

以及该应用的主要(也是唯一)活动:

public class Main extends Activity {
    Database db = null;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView hw = (TextView) findViewById(R.id.helloworld);
        db = new Database(this);
        db.open();
        if (db != null) {
            if (db.askfordata()) hw.setText("Worked.");
        }
    }
    public void onDestroy() {
        db.close();
        super.onDestroy();
    }
}

ADB logcat输出:

01-20 23:35:27.183 22155 22155 E AndroidRuntime: FATAL EXCEPTION: main 01-20 23:35:27.183 22155 22155 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{es.foo.bar/es.foo.bar.Main}: android.database.sqlite.SQLiteException: no such table: clues: , while compiling: select * from clues where question match 'foo'

这是否意味着我的桌子从未创建过?我将表格从/assets/文件夹移动到onCreate方法中的正确位置。我可以使用sqlite3查看计算机上的.db文件,并在其中列出表格。这显示了我的所有表格:

clues clues_content clues_segdir clues_segments

注意:我遗漏了import以节省空间。提前感谢任何见解。

2 个答案:

答案 0 :(得分:1)

请参阅http://www.sqlite.org/lang_createtable.html

    Cursor cursor = null;
    cursor = db.rawQuery("CREATE TABLE IF NOT EXISTS clues (question TEXT, answer TEXT)", null);
    if (cursor != null) {
        return true;
    }
    return false;

需要实际创建表

答案 1 :(得分:0)

您是否检查过资产文件夹中的数据库以确保它已正确创建?您可以使用Sqlite plugin for Firefox

等工具快速查看

<强>更新

修改此代码:

InputStream is = c.getAssets().open(DB_NAME);
OutputStream os = new FileOutputStream(DB_PATH);

是:

InputStream is = c.getAssets().open(DB_NAME);
this.getReadableDatabase();
OutputStream os = new FileOutputStream(DB_PATH);

我认为您的副本失败了,您只是在日志中错过了它。在重试之前卸载您的应用。