James_Duh James_Duh - 2 months ago 6
Android Question

Programmatically call rawQuery when app starts

We have these two classes MainActivity and a DBHelper

The

DBHelper
creates the Database and two Tables one table,
masterPW
, which will only have one row and two columns,
ID
and
ColPW
. This table will only contain one record once created and data is inserted.

The column
ColPW
will be empty or null when the app starts
the first time.

We would like to call a
rawQuery
programmatically when the app starts and set the value of the
ColPW
column to a
String
.

If the string variable is null or empty the
MainActivity
displays
two
EditText
fields and the SAVE button. If the string variable obtains
data from the
ColPW
column. The MainActivity will only display one
EditText field and the ENTER button.

Being real new to Android I am at a loss here how to make the
app programmatically call the
rawQuery
. Where do I place the
rawQuery
?

If the result of the query is in the
DBHelper
Class, how to send the
result to the
MainActivity
? Intents, Global singleton Class, or
use a Model Class with getters and setters and Adapter Class?

I have written this with DerbyDB and JavaFX, but I could open
and create the table query the table all in the same Class

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

Button btnSave;
Button btnEnter;
TextView tvPW;
TextView tvCPW;
EditText etPW;
EditText etCPW;
int x = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
tvPW = (TextView) findViewById( R.id.tvPW );
tvCPW = (TextView) findViewById( R.id.tvCPW );
etPW = (EditText) findViewById( R.id.etPW );
etCPW = (EditText) findViewById( R.id.etCPW );

btnEnter = (Button) findViewById( R.id.btnEnter );
btnSave = (Button) findViewById( R.id.btnSave );
addListenerOnButtonSave();

DBHelper dbHelper = new DBHelper( this );
String str = dbHelper.getCol_PW();
if (str == null || str.isEmpty()) {
System.out.println( "I am null" );
} else {
System.out.println( "I am NOT NULL" );
}
}

private void addListenerOnButtonSave() {
btnSave.setOnClickListener( new View.OnClickListener() {

@Override
public void onClick(View v) {

if (x == 1) {
btnSave.setVisibility( View.INVISIBLE );
btnEnter.setVisibility( View.VISIBLE );
tvCPW.setVisibility( View.INVISIBLE );
etCPW.setVisibility( View.INVISIBLE );
etPW.requestFocus();
setTitle( "Enter Password" );

}

}
} );
}
}
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

public static final String DB_NAME = "PassWord";
public static final Integer DB_VERSION = 1;

public static final String TABLE_NAME = "masterPW";
public static final String Col_ID = "ID";
public static final String Col_MPW = "mpw";

private static final String MAKE_TABLE = "CREATE TABLE " + TABLE_NAME +
"(" + Col_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + Col_MPW + " TEXT" + ")";

SQLiteDatabase db;


public DBHelper(Context context){
super(context,DB_NAME,null,DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL( MAKE_TABLE );
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
db.execSQL(MAKE_TABLE);
}

public String getCol_PW(){
String str = null;
String q = "SELECT mpw FROM masterPW";
Cursor cursor = db.rawQuery(q,null);
if(cursor.moveToFirst()){
str = cursor.getString(cursor.getColumnIndex(Col_MPW));
}
return str;
}
}

Answer

In your MainActivity onCreate try to do this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DBHelper dbHelper = new DBHelper(this);
    String str = dbHelper.getColPW();
    if(str == null || str.isEmpty()){
        //display 2 edittext
    } else {
        //display 1 edittext
    }

}

In DBHelper.java:

private SQLiteDatabase myDataBase;
public String getColPW(){
    String str = null;
    myDataBase = getWritableDatabase();
    Cursor cursor = myDataBase.rawQuery("select " + COL_MPW + " from " + TABLE_NAME, null);
    if(cursor.moveToFirst()){
        str = cursor.getString(cursor.getColumnIndex(COL_MPW));
    }
    return str;
}

If you have questions, please ask in comments.