私は 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"