我有一个关于在SQLite中使用WHERE子句的一般问题。我曾经和SQLite合作过,并且知道我的方法。但是,我遇到了WHERE子句的问题。
我有一个Android应用程序,我需要使用SQLite WHERE子句进行一些简单的操作。然而,尽管整天都在试验WHERE条款,但我还是空了。我正在尝试执行非常简单的SQLite命令(例如SELECT name FROM tablename WHERE _id=2
或DELETE FROM tablename WHERE name='somename'
)。但是,每次我使用where子句时,我都会返回0行(在SELECT的情况下)或删除0行。
表名是正确的。列名是正确的。 (只要我没有指定WHERE子句,我就没有选择或插入任何问题。)我确保查询/语句格式正确。我已经尝试了两个原始查询/语句以及方法(我确保使用SELECT和DELETE的正确方法{eg rawQuery()
或query()
for SELECT}){{1在Android中有类,但没有有效。
我需要的是能够使用WHERE子句执行简单的查询/语句。有没有人对发生的事情有任何见解?
以下是创建我正在使用的表的代码:
SQLiteDatabase
示例查询
public static final String TABLE_WORKOUTS = "workouts"
public static final String W_ID = "_id";
public static final String W_WORKOUT_NAME = "workout_name";
public static final String W_EXERICSE_NAME = "exercise_name";
public static final String W_EXERCISE_SETS = "exercise_sets";
public static final String W_EXERCISE_FIRST_ATTRIBUTE = "first_attribute";
public static final String W_EXERCISE_SECOND_ATTRIBUTE = "second_attribute";
public static final String W_EXERCISE_TYPE = "exercise_type";
private static final String createTableWorkouts = "CREATE TABLE " + TABLE_WORKOUTS + " (" + W_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
W_WORKOUT_NAME + " TEXT, " + W_EXERICSE_NAME + " TEXT, " + W_EXERCISE_SETS + " TEXT, " +
W_EXERCISE_FIRST_ATTRIBUTE + " TEXT, " + W_EXERCISE_SECOND_ATTRIBUTE + " TEXT, " + W_EXERCISE_TYPE + " TEXT);";
示例记录(_id,exercise_name,exercise_name,sets,first_attribute,second_attribute,exercise_type): 23,上半身,卧推,5,160,5,重量
答案 0 :(得分:1)
exercise_name
列中的值为Bench Press
,但您尝试将其与Some Workout
匹配。
答案 1 :(得分:1)
这是一个可行的源代码,它将使用where条件从SQLite数据库中检索值列表。通过使用游标,检索值并添加到POJO类中。我认为这段代码可以帮到你。
public List<RationNamesPOJO> fetchRationMembers(String rationNumber) {
ArrayList<RationNamesPOJO> RationDet = new ArrayList<RationNamesPOJO>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor cur = db.rawQuery("SELECT * FROM " + TABLE_NAME + " where " + PEOPLE_RATION_NUMBER + "='" + rationNumber + "'", null);
if (cur.moveToFirst()) {
do {
RationNamesPOJO rationPojo = new RationNamesPOJO();
rationPojo.setPeople_rationNumber(cur.getString(cur.getColumnIndex(PEOPLE_RATION_NUMBER)));
rationPojo.setPeople_aadhaarNumber(cur.getString(cur.getColumnIndex(PEOPLE_AADHAAR_NUMBER)));
rationPojo.setPeopleName(cur.getString(cur.getColumnIndex(PEOPLE_NAME)));
rationPojo.setPeopleBiometric(cur.getString(cur.getColumnIndex(PEOPLE_FINGER_IMAGE)));
RationDet.add(rationPojo);
} while (cur.moveToNext());
}
return RationDet;
}
答案 2 :(得分:0)
实施1
String sqlQuery = "select * from Appointment where start_date='"
+ startDate + "' order by start_date,time(start_date) ASC";
cursor = getReadableDatabase().rawQuery(sqlQuery, null);
// Log.e("Appointment ->","Total rows fetched for date "+
// startDate+" "+cursor.getCount());
if (cursor != null && cursor.moveToFirst()) {
do {
} while (cursor.moveToNext());
}
实施2
private boolean isRequestExists(int requestId) {
Cursor cursor = null;
boolean result = false;
try {
String[] args = { "" + requestId };
StringBuffer sbQuery = new StringBuffer("SELECT * from ").append(
TABLE_NAME).append(" where _id=?");
cursor = getReadableDatabase().rawQuery(sbQuery.toString(), args);
if (cursor != null && cursor.moveToFirst()) {
result = true;
}
} catch (Exception e) {
Log.e("Requestdbhelper", e.toString());
}
return result;
}
答案 3 :(得分:0)
您好,您可以尝试使用帮助程序类
Cursor cur1 = null, cur2 = null;
if (mySQLiteAdapter == null || !mySQLiteAdapter.isOpen()) {
mySQLiteAdapter = new SqliteAdapter(
LoginActivity.this);
}
try {
cur1 = mySQLiteAdapter
.executeSQLQuery(
"select ifnull(max(login_id),0) as userCount from table_login",
null);
for (cur1.moveToFirst(); !(cur1.isAfterLast()); cur1
.moveToNext()) {
userCount = cur1.getString(cur1
.getColumnIndex("userCount"));
}
} catch (Exception ex) {
Log.w("error while retrieving cursor values", ex.toString());
} finally {
cur1.close();
if (mySQLiteAdapter != null || mySQLiteAdapter.isOpen()) {
mySQLiteAdapter.close();
}
}
==========================================
public Cursor executeSQLQuery(String sql, String[] selectionArgs) {
Cursor cursor = null;
try {
if (database != null) {
cursor = database.rawQuery(sql, selectionArgs);
}
} catch (SQLiteException sqle) {
Toast.makeText(context,
"Unable to execute sql query \n" + sqle.getMessage(),
Toast.LENGTH_SHORT).show();
}
return cursor;
}