我正在Android设备上做一些非常初学的数据库编程。在下面的代码中,我试图用一些过滤器查询数据库。如果我选择根据行的ID进行过滤,则它会正确返回,但如果我使用不同的列进行过滤,则无法在数据库中找到任何条目。谁能发现我犯了什么样的错误?
*请注意,我已就我询问的具体部分发表评论。
import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBaseManager {
Context context;
private SQLiteDatabase db;
private final String DB_NAME = "database_name";
private final int DB_VERSION = 1;
private final String TABLE_NAME = "database_table";
private final String TABLE_COLUMN_ID = "id";
private final String TABLE_COLUMN_ONE = "user_name";
private final String TABLE_COLUMN_TWO = "pass_word";
public DataBaseManager(Context context) {
this.context = context;
CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context);
this.db = helper.getWritableDatabase();
}
private class CustomSQLiteOpenHelper extends SQLiteOpenHelper {
public CustomSQLiteOpenHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
public void onCreate(SQLiteDatabase db) {
String newTableQueryString =
"create table " +
TABLE_NAME +
" (" +
TABLE_COLUMN_ID + " integer primary key autoincrement not null," +
TABLE_COLUMN_ONE + " text," +
TABLE_COLUMN_TWO + " text" +
");";
db.execSQL(newTableQueryString);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public void addRow(String username, String password) {
ContentValues values = new ContentValues();
values.put(TABLE_COLUMN_ONE, username);
values.put(TABLE_COLUMN_TWO, password);
try {
db.insert(TABLE_NAME, null, values);
} catch (Exception e) {
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
public void deleteRow(long rowID) {
try {
db.delete(TABLE_NAME, TABLE_COLUMN_ID + "=" + rowID, null);
} catch (Exception e) {
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
public void updateRow(long rowID, String username, String password) {
ContentValues values = new ContentValues();
values.put(TABLE_COLUMN_ONE, username);
values.put(TABLE_COLUMN_TWO, password);
try {
db.update(TABLE_NAME, values, TABLE_COLUMN_ID + "=" + rowID, null);
} catch (Exception e) {
Log.e("DB Error", e.toString());
e.printStackTrace();
}
}
public ArrayList<Object> getRowAsArray(long rowID) {
ArrayList<Object> rowArray = new ArrayList<Object>();
Cursor cursor = null;
try {
cursor = db.query(
TABLE_NAME,
new String[] { TABLE_COLUMN_ID, TABLE_COLUMN_ONE, TABLE_COLUMN_TWO },
TABLE_COLUMN_ID + "=" + rowID,
null, null, null, null, null
);
cursor.moveToFirst();
if (!cursor.isAfterLast()) {
do {
ArrayList<Object> dataList = new ArrayList<Object>();
dataList.add(cursor.getLong(0));
dataList.add(cursor.getString(1));
dataList.add(cursor.getString(2));
rowArray.add(dataList);
} while(cursor.moveToNext());
}
cursor.close();
} catch (Exception e) {
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
return rowArray;
}
//THIS IS THE METHOD I AM ASKING ABOUT
//
//
public ArrayList<ArrayList<Object>> getAllRowsAsArrays() {
ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
Cursor cursor;
try {
/* If I have TABLE_COLUMN_ID + "=" + 1, for example, the sql query will return
the correct value, but this returns nothing, even though i am 100% sure
that "teharris" is in the database
*/
cursor = db.query(
TABLE_NAME,
new String[]{TABLE_COLUMN_ID, TABLE_COLUMN_ONE, TABLE_COLUMN_TWO},
TABLE_COLUMN_ONE + "=" + "teharris", null, null, null, null
);
cursor.moveToFirst();
if (!cursor.isAfterLast()) {
do {
ArrayList<Object> dataList = new ArrayList<Object>();
dataList.add(cursor.getLong(0));
dataList.add(cursor.getString(1));
dataList.add(cursor.getString(2));
dataArrays.add(dataList);
} while (cursor.moveToNext());
}
}
catch (Exception e) {
Log.e("DB Error", e.toString());
e.printStackTrace();
}
// return the ArrayList that holds the data collected from
// the database.
return dataArrays;
}
public boolean loginVerify(String username, String password) {
Cursor cursor = null;
boolean matches = false;
try {
cursor = db.query(
TABLE_NAME,
new String[] { TABLE_COLUMN_ID, TABLE_COLUMN_ONE, TABLE_COLUMN_TWO },
TABLE_COLUMN_ONE + "=" + username,
null, null, null, null, null
);
cursor.moveToFirst();
if (!cursor.isAfterLast()) {
do {
if (username.equals(cursor.getString(1).toString()) &&
password.equals(cursor.getString(2).toString())) {
matches = true;
}
} while(cursor.moveToNext() && !matches);
}
cursor.close();
} catch (Exception e) {
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
return matches;
}
}
答案 0 :(得分:2)
您需要引用String
文字:
cursor = db.query(
TABLE_NAME,
new String[]{TABLE_COLUMN_ID, TABLE_COLUMN_ONE, TABLE_COLUMN_TWO},
TABLE_COLUMN_ONE + "=" + "'teharris'", null, null, null, null
);