2012-06-13 160 views
0

我是SQLite的新手,我不知道如何按字段排序。我搜查使用「ORDER BY」,但我做得不好。我有一個類來實現SQLite的功能如下: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" 
+0

你似乎混淆MySQL與sqlite。 MySQL沒有涉及到這裏。 – Gryphius

回答

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

希望這會有所幫助。

+0

對不起,它工作。它對我來說有一個小的(愚蠢的)錯誤。 –