将用户输入与sqlite进行比较

时间:2014-04-03 08:37:37

标签: android android-activity android-sqlite

我对Android编程非常陌生(有一些以前使用php-mysql组合的经验)。通过一些在线教程,我已经为我的mainactivity类设置了一个sqlite适配器用于学习目的。

如果有人可以解释我是否能够获取每个用户输入/输入(例如,条目,日期)并使用它来从mainactivity类本身访问和更新sqlite数据库中的数据,我将不胜感激。

例如,给定用户选择的特定名称,年龄或位置(任何一个),如何使用此信息从主要活动(或托管交换的任何活动)中搜索数据库?

    >>   MainActivity.......
    public class MainActivity extends Activity {

DBAdapter myDb;

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

    myDb = new DBAdapter(this);
    myDb.open();
}
@Override
protected void onDestroy() {
    super.onDestroy();  
    myDb.close();
}


private void displayText(String message) {
    TextView textView = (TextView) findViewById(R.id.textDisplay);
    textView.setText(message);
}

public void onClick_AddRecord(View v) {
    displayText("Record added.");   
    long newId = myDb.insertRow("Kenneth", 17, "Indiana");

    Cursor cursor = myDb.getRow(newId);
    displayRecordSet(cursor);
}

public void onClick_ClearAll(View v) {
    displayText("Clicked clear all!");
    myDb.deleteAll();
}

public void onClick_DisplayRecords(View v) {
    displayText("Clicked display record!");

    Cursor cursor = myDb.getAllRows();
    displayRecordSet(cursor);
}


private void displayRecordSet(Cursor cursor) {
    String message = "";

    if (cursor.moveToFirst()) {
        do {

            int id = cursor.getInt(DBAdapter.COL_ROWID);
            String name = cursor.getString(DBAdapter.COL_NAME);
            int ageber = cursor.getInt(DBAdapter.COL_AGE);
            String location = cursor.getString(DBAdapter.COL_LOCATION);


            message += "id=" + id
                       +", name=" + name
                       +", age =" + age
                       +", location =" + location
                       +"\n";
        } while(cursor.moveToNext());
    }

    cursor.close();

    displayText(message);
}
  }

    >>> DBAdapter.....
    public class DBAdapter {

private static final String TAG = "DBAdapter";

public static final String KEY_ROWID = "_id";
public static final int COL_ROWID = 0;

public static final String KEY_NAME = "name";
public static final String KEY_AGE = "age";
public static final String KEY_LOCATION = "location";

public static final int COL_NAME = 1;
public static final int COL_AGE = 2;
public static final int COL_LOCATION = 3;


public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_NAME, KEY_AGE, 

    KEY_LOCATION};

public static final String DATABASE_NAME = "MyDb";
public static final String DATABASE_TABLE = "Table";

public static final int DATABASE_VERSION = 23;  

private static final String DATABASE_CREATE_SQL = 
        "create table " + DATABASE_TABLE 
        + " (" + KEY_ROWID + " integer primary key autoincrement, "
        + KEY_NAME + " text not null, "
        + KEY_AGE + " integer not null, "
        + KEY_LOCATION + " string null"
        + ");";


private final Context context;

private DatabaseHelper myDBHelper;
private SQLiteDatabase db;


public DBAdapter(Context ctx) {
    this.context = ctx;
    myDBHelper = new DatabaseHelper(context);
}

public DBAdapter open() {
    db = myDBHelper.getWritableDatabase();
    return this;
}

public void close() {
    myDBHelper.close();
}

public long insertRow(String name, int age, String location) {

    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_NAME, name);
    initialValues.put(KEY_AGE, age);
    initialValues.put(KEY_LOCATION, location);

    return db.insert(DATABASE_TABLE, null, initialValues);
}

public boolean deleteRow(long rowId) {
    String where = KEY_ROWID + "=" + rowId;
    return db.delete(DATABASE_TABLE, where, null) != 0;
}

public void deleteAll() {
    Cursor c = getAllRows();
    long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
    if (c.moveToFirst()) {
        do {
            deleteRow(c.getLong((int) rowId));              
        } while (c.moveToNext());
    }
    c.close();
}

public Cursor getAllRows() {
    String where = null;
    Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                        where, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

public Cursor getRow(long rowId) {
    String where = KEY_ROWID + "=" + rowId;
    Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                    where, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

public boolean updateRow(long rowId, String name, int age, String location) {
    String where = KEY_ROWID + "=" + rowId;

    ContentValues newValues = new ContentValues();
    newValues.put(KEY_NAME, name);
    newValues.put(KEY_AGE, age);
    newValues.put(KEY_LOCATION, location);

    return db.update(DATABASE_TABLE, newValues, where, null) != 0;
}


private static class DatabaseHelper extends SQLiteOpenHelper
{
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase _db) {
        _db.execSQL(DATABASE_CREATE_SQL);           
    }

    @Override
    public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {     
        _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);

        onCreate(_db);
    }
}
}        

对不起,如果这个问题对某些人来说太简单了,但我非常渴望学习。

1 个答案:

答案 0 :(得分:0)

如果您想要所有记录,请尝试以下代码:

将此代码放在DBAdapter类中:

public ArrayList<HashMap<String, String>> getAlldts() {
        ArrayList<HashMap<String, String>> todos = new ArrayList<HashMap<String,String>>();
        String selectQuery = "SELECT  * FROM " + DATABASE_TABLE;

        Log.e(LOG, selectQuery);

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (c.moveToFirst()) {
            do {
                HashMap<String, String> dtls = new HashMap<String, String>();

                dtls.put("nm", c.getString((c.getColumnIndex(KEY_NAME))));
                dtls.put("age", c.getString((c.getColumnIndex(KEY_AGE))));
                dtls.put("loc", c.getString((c.getColumnIndex(KEY_LOCATION))));
                todos.add(dtls);
            } while (c.moveToNext());
        }

        return todos;
    }

public ArrayList<HashMap<String, String>> getSomedts(int age) {
        ArrayList<HashMap<String, String>> todos = new ArrayList<HashMap<String,String>>();
        String selectQuery = "SELECT  * FROM " + DATABASE_TABLE+ " WHERE " + KEY_AGE + " = " + age+";

    Log.e(LOG, selectQuery);

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (c.moveToFirst()) {
        do {
            HashMap<String, String> dtls = new HashMap<String, String>();

            dtls.put("nm", c.getString((c.getColumnIndex(KEY_NAME))));
            dtls.put("age", c.getString((c.getColumnIndex(KEY_AGE))));
            dtls.put("loc", c.getString((c.getColumnIndex(KEY_LOCATION))));
            todos.add(dtls);
        } while (c.moveToNext());
    }

    return todos;
}

现在在您的活动类中使用此方法,如下所示:

    ArrayList<HashMap<String, String>> Mondtls = new ArrayList<HashMap<String, String>>();
        Mondtls = db.getAlldts(); // db.getSomedts(25);
                for (int i = 0; i < Mondtls.size(); i++) {
                        String nm= Mondtls.get(i).get("nm");
            String age = Mondtls.get(i).get("age");
                        String loc = Mondtls.get(i).get("loc");
}

现在根据您的需要使用它。