0

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.

Kaigo
  • 1,267
  • 2
  • 14
  • 33
  • Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this Also, please note that the code in your question is never getting to the `rawQuery()` call, as you never call `viewText()`. – CommonsWare Mar 23 '18 at 10:49
  • I forgot to say viewTest() is the click event on a button. I've updated the post. Okay so in log cat I get "Caused by: net.sqlcipher.database.SQLiteException: 't1' is not a function: , while compiling: SELECT * FROM t1(a, b)" which is strange because it's a valid sql query? – Kaigo Mar 23 '18 at 10:54

2 Answers2

1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

Complete solution for future references. (changed name of "t" to 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();
    }
}
Kaigo
  • 1,267
  • 2
  • 14
  • 33