androidsqlciphersqlcipher-android

Return data from sqlcipher database on android and put in textview


Working from sqlcipher getting started page, I can't display data from the database and view in a textview.

I have initialised the db and queried the db in the click event, but it's crashing during the raw query event.

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

Can anyone help me out, as I've been trying to follow examples of sql-lite but they don't seem to work with cipher.


Solution

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

    I do not think it is a valid SQL query, at least for SQLite. Use either SELECT * FROM t1 or SELECT a,b FROM t1.