应用程序启动时以编程方式调用rawQuery

时间:2016-10-04 18:18:46

标签: android sqlite android-sqlite

我们有两个类MainActivity和一个DBHelper

DBHelper创建数据库和两个表一个表masterPW,它只有一行和两列,IDColPW。该表只包含一条记录,并插入数据。

应用启动时,列ColPW将为空或为null 第一次。

我们希望在应用启动时以编程方式调用rawQuery,并将ColPW列的值设置为String

如果字符串变量为null或为空,则显示MainActivity 两个EditText字段和SAVE按钮。如果字符串变量获得 来自ColPW列的数据。 MainActivity仅显示一个 EditText字段和ENTER按钮。

成为Android的新手我在这里不知所措如何制作 app以编程方式调用rawQuery。我在哪里放置rawQuery

如果查询结果在DBHelper类中,如何发送 结果是MainActivity? Intents,Global singleton Class,或 使用带有getter和setter以及Adapter Class的Model Class?

我用DerbyDB和JavaFX编写了这个,但我可以打开 并创建表查询表所有在同一个类

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

1 个答案:

答案 0 :(得分:1)

在您的MainActivity onCreate尝试执行此操作:

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
    }

}

在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;
}

如果您有任何疑问,请在评论中提问。