0

sqlcipher 入门页面工作,我无法显示数据库中的数据并在 textview 中查看

我已经初始化了数据库并在点击事件中查询了数据库,但它在原始查询事件中崩溃了。

package com.example.keystoretest;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import net.sqlcipher.Cursor;
import net.sqlcipher.database.SQLiteDatabase;

import java.io.File;

public class HelloSQLCipherActivity extends Activity {

    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello_sqlcipher);
        InitializeSQLCipher();
    }

    private void InitializeSQLCipher() {
        SQLiteDatabase.loadLibs(this);
        File databaseFile = getDatabasePath("demo.db");
        databaseFile.mkdirs();
        databaseFile.delete();
        db = SQLiteDatabase.openOrCreateDatabase(databaseFile, "test123", null);
        db.execSQL("create table t1(a, b)");
        db.execSQL("insert into t1(a, b) values(?, ?)", new Object[]{"one for the money",
                "two for the show"});
    }

    public void viewText(View view) { //click event on button

        String query = "SELECT * FROM t1(a, b)";

        Cursor data = db.rawQuery(query, null);

        final TextView mTextView = (TextView) findViewById(R.id.textView);
        mTextView.setText(data.toString());
    }
}

任何人都可以帮助我,因为我一直在尝试遵循sql-lite 的示例,但它们似乎不适用于密码。

4

2 回答 2

1

这很奇怪,因为它是一个有效的 sql 查询?

我不认为这是一个有效的 SQL 查询,至少对于SQLite 而言。使用SELECT * FROM t1SELECT a,b FROM t1

于 2018-03-23T10:59:02.950 回答
0

完整的解决方案供将来参考。(将“t”的名称更改为表)

public class HelloSQLCipherActivity extends Activity {

    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello_sqlcipher);
        InitializeSQLCipher();
    }

    private void InitializeSQLCipher() {
        SQLiteDatabase.loadLibs(this);
        File databaseFile = getDatabasePath("demo.db");
        databaseFile.mkdirs();
        databaseFile.delete();
        db = SQLiteDatabase.openOrCreateDatabase(databaseFile, "test123", null);
        db.execSQL("create table table1(a, b)");
        db.execSQL("insert into table1(a, b) values(?, ?)", new Object[]{"one for the money",
                "two for the show"});
    }

    public void viewText(View view) {
        String query = "SELECT a FROM table1";

        Cursor data = db.rawQuery(query, null);

        final TextView mTextView = (TextView) findViewById(R.id.textView);

        data.moveToFirst();

        mTextView.setText(data.getString(data.getColumnIndex("a")));

        data.close();

        db.close();
    }
}
于 2018-03-23T12:42:57.443 回答