0

我是 SQLite 的新手,我不知道如何按字段排序。我搜索使用“ORDER BY”,但我做得不好。我有一个类来实现 SQLite 函数,如下所示:

public class PLSQLiteOpenHelper extends SQLiteOpenHelper{
public String TableNames[];
public String FieldNames[][];
public String FieldTypes[][];
public static String NO_CREATE_TABLES = "no tables";
private String message = "";

public PLSQLiteOpenHelper(Context context, String name, CursorFactory factory, int version, String tableNames[], String fieldNames[][], String fieldTypes[][]) {
    super(context, name, factory, version);
    TableNames = tableNames;
    FieldNames = fieldNames;
    FieldTypes = fieldTypes;
}

@Override
public void onCreate(SQLiteDatabase db) {
    if(TableNames == null){
        message = NO_CREATE_TABLES;
        return;
    }

    for(int i = 0; i < TableNames.length; i++){
        String sql = "CREATE TABLE " + TableNames[i] + "(";
        for(int j = 0; j < FieldNames[i].length; j++)
            sql += FieldNames[i][j] + " " + FieldTypes[i][j] + ",";
        sql = sql.substring(0, sql.length() - 1);
        sql += ")";
        db.execSQL(sql);
    }
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    for(int i = 0; i < TableNames[i].length(); i++){
        String sql = "DROP TABLE IF EXISTS " + TableNames[i];
        db.execSQL(sql);
    }
    onCreate(db);
}

public void execSQL(String sql) throws java.sql.SQLException{
    SQLiteDatabase db = this.getWritableDatabase();
    db.execSQL(sql);
}

public Cursor select(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
    return cursor;
}

public long insert(String table, String fields[], String values[]){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    for(int i = 0; i < fields.length; i++)
        cv.put(fields[i], values[i]);
    return db.insert(table, null, cv);
}

public int sort(String table, String updateFields[], String orderBy){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(table, updateFields, null, null, null, null, orderBy);

    ContentValues cv = new ContentValues();
    int i = 0;
    while(cursor.moveToNext()){
        cv.putNull(updateFields[i]);
        i++;
    }
    return db.update(table, cv, null, null);
}

public int delete(String table, String where, String[] whereValue){
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete(table, where, whereValue);
}

public int update(String table, String updateFields[], String updateValues[], String where, String[] whereValue){
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues cv = new ContentValues();
    for(int i = 0; i < updateFields.length; i++)
        cv.put(updateFields[i], updateValues[i]);
    return db.update(table, cv, where, whereValue);
}

public String getMessage(){
    return message;
}

@Override
public synchronized void close(){
    super.close();
}
}

我知道我需要一起使用“选择”和“更新”功能,但我不知道如何调用它们以便按字段名称对表格进行排序。任何人都可以通过以下信息告诉我函数调用顺序吗?

Table Names: "t_passwordlist"
Field Names: "f_id", "f_service", "f_user", "f_pwd", "f_lanuch"
Field Types: "INTEGER PRIMARY KEY AUTOINCREMENT", "text", "text", "text", "text"
Order By: "f_service"
4

1 回答 1

0

创建您的帮助类的对象,然后以这种方式调用该方法。

Cursor cursor = pLSQLiteOpenHelper.select("table_name", new String[] { "f_id", "f_service", "f_user", "f_pwd", "f_lanuch" }, null, null, null, null, "f_service");

希望这可以帮助。

于 2012-06-13T07:15:57.257 回答