我想从比较3行的表中选择*并且select返回一个id,我创建了这个方法:
setTintColor
已在我的数据库上创建nickname = q,chapter = Q和password = qqqqqq
这些是我调用其功能的方法
f.setTintColor(0x7fccff99);
我使用另一种列出所有用户的方法进行了检查,并获得了我曾经创建过的所有用户。
结果如下:
//UserDAO class
private SQLiteDatabase mDatabase;
private DatabaseHelper mDbHelper;
// irelative code
public Long selectUserLogin(String nickname, String chapter, String password) {
long id;
String selectQuery = "SELECT * FROM " +
DatabaseHelper.TABLE_USERS + " WHERE " +
DatabaseHelper.COLUMN_USERS_NICKNAME + " = " + nickname + " AND " +
DatabaseHelper.COLUMN_USERS_CHAPTER + " = " + chapter + " AND " +
DatabaseHelper.COLUMN_USERS_PASSWORD + " = " + password;
Log.e(TAG, selectQuery);
try {
Cursor cursor = mDatabase.rawQuery(selectQuery, null);
if (cursor != null)
cursor.moveToFirst();
id = cursor.getLong(cursor.getColumnIndex(DatabaseHelper.COLUMN_USERS_ID));
return id;
} catch (Exception e){
e.printStackTrace();
}
return null;
}
当我试图在Android设备监视器上看到我的数据文件夹时,该文件夹是空的,所以如果我无法访问数据库或者我没有正确插入,我有点困惑。
当我尝试调用方法 selectUserLogin 时,我的部分logcat:
10-09 00:46:01.198 7745-7745 / com.org.feedme.cisvmeeting.activities E / UserDAO:SELECT * FROM users WHERE nickname = q AND chapter = Q AND 密码= qqqqqq
10-09 00:46:01.208 7745-7745 / com.org.feedme.cisvmeeting.activities E / SQLiteLog:(1)没有这样的列:q
10-09 00:46:01.208 7745-7745 / com.org.feedme.cisvmeeting.activities W / System.err:android.database.sqlite.SQLiteException:没有这样的列: q(代码1):,编译时:SELECT * FROM users WHERE nickname = q AND chapter = Q AND password = qqqqqq
编辑:我只在我的Android设备上运行我的应用程序!
EDIT_2:我测试了另一种类似方法,第二种方法工作正常!
userDAO.selectUserLogin("q", "Q", "qqqqqq");
答案 0 :(得分:2)
您需要用引号括起字符串参数,因为您没有使用selectionArgs(rawQuery
中的第二个参数)。
String selectQuery = "SELECT * FROM " +
DatabaseHelper.TABLE_USERS + " WHERE " +
DatabaseHelper.COLUMN_USERS_NICKNAME + " = '" + nickname + "' AND " +
DatabaseHelper.COLUMN_USERS_CHAPTER + " = '" + chapter + "' AND " +
DatabaseHelper.COLUMN_USERS_PASSWORD + " = '" + password + "' ";
Log.e(TAG, selectQuery);
try {
Cursor cursor = mDatabase.rawQuery(selectQuery, null);
或者你可以这样做,这将为你处理字符串。
String selectQuery = "SELECT * FROM " +
DatabaseHelper.TABLE_USERS + " WHERE " +
DatabaseHelper.COLUMN_USERS_NICKNAME + "=? AND " +
DatabaseHelper.COLUMN_USERS_CHAPTER + "=? AND " +
DatabaseHelper.COLUMN_USERS_PASSWORD + "=? ";
String[] selectionArgs = new String[] { nickname, chapter, password };
Log.e(TAG, selectQuery);
try {
Cursor cursor = mDatabase.rawQuery(selectQuery, selectionArgs);
答案 1 :(得分:0)
首先,您删除设备上的应用程序,然后重新安装应用程序,现在插入您需要的数据,现在无论您输入的数据是什么,都要记住并在查询参数上应用这些数据以供选择。