Android SQLite按字段排序
我是SQLite的新手,不知道如何按字段排序。我试图使用“订购方式”,但我做不好。我有一个类来实现SQLite函数,如下所示:Android SQLite按字段排序,android,sqlite,Android,Sqlite,我是SQLite的新手,不知道如何按字段排序。我试图使用“订购方式”,但我做不好。我有一个类来实现SQLite函数,如下所示: public class PLSQLiteOpenHelper extends SQLiteOpenHelper{ public String TableNames[]; public String FieldNames[][]; public String FieldTypes[][]; public static String NO_CREATE_TABLES =
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();
}
}
创建助手类的对象,然后以这种方式调用该方法
Cursor cursor = pLSQLiteOpenHelper.select("table_name", new String[] { "f_id", "f_service", "f_user", "f_pwd", "f_lanuch" }, null, null, null, null, "f_service");
希望这有帮助。您似乎把MySQL和sqlite混淆了。这里不涉及MySQL。
Cursor cursor = pLSQLiteOpenHelper.select("table_name", new String[] { "f_id", "f_service", "f_user", "f_pwd", "f_lanuch" }, null, null, null, null, "f_service");