0

我有一个关于在 SQLite 中使用 WHERE 子句的一般性问题。我已经使用过一些 SQLite 并且知道我的方式。但是,我在使用 WHERE 子句时遇到了问题。

我有一个 Android 应用程序,我需要在其中使用 SQLite WHERE 子句执行一些简单的操作。然而,尽管整天都在试验 WHERE 子句,但我还是一无所获。我正在尝试执行非常简单的 SQLite 命令(例如SELECT name FROM tablename WHERE _id=2or DELETE FROM tablename WHERE name='somename')。但是,每次我使用 where 子句时,我要么返回 0 行(在 SELECT 的情况下),要么删除 0 行。

表名是正确的。列名是正确的。(只要我没有指定 WHERE 子句,我在选择或插入时就没有任何问题。)我确保查询/语句的格式正确。我已经尝试了原始查询/语句以及Android 中的类提供的方法(我确保对 SELECT 和 DELETE {例如rawQuery()query()用于 SELECT} 使用正确的方法) ,但没有任何效果。SQLiteDatabase

我所需要的只是能够使用 WHERE 子句执行简单的查询/语句。有人对正在发生的事情有任何见解吗?

这是创建我正在使用的表的代码:

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);";

示例查询: String workoutName = "Some Workout"; Cursor cursor = datasource.executeRawQuery("SELECT * FROM " + WorkoutDatabase.TABLE_WORKOUTS + " WHERE " + WorkoutDatabase.W_EXERICSE_NAME + "='" + workoutName + "'", null);

示例记录(_id、锻炼名称、锻炼名称、套数、第一属性、第二属性、锻炼类型):23、上身、卧推、5、160、5、体重

4

4 回答 4

2

这是一个可行的来源,它将使用 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;

}
于 2017-07-19T09:55:10.353 回答
1

exercise_name列中的值为Bench Press,但您正试图将其与 匹配Some Workout

于 2013-05-30T18:51:40.087 回答
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;
}
于 2013-05-30T05:21:27.660 回答
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;
}
于 2013-05-30T05:32:20.863 回答