Return 来自 android 上的 sqlcipher 数据库的数据并放入 textview

Return data from sqlcipher database on android and put in textview

使用 sqlcipher getting started 页面,我无法显示数据库中的数据和 在文本视图中查看.

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

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 的 ,但他们似乎无法使用密码。

which is strange because it's a valid sql query?

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

完整的解决方案以备将来参考。 (将 "t" 的名称更改为 table)

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