我有一个包含多列的SQL数据库,我将它与simpleCursorAdapter一起使用以在列表视图中显示它。我想在整数列COL3
上对数据库进行排序。我知道应该怎么做,但是,我的尝试没有对数据进行排序。我不确定是否放置有误,但我将非常感谢您的帮助。
DatabaseHelper.java
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL2 +" TEXT, " + COL3 + " INTEGER, '" + COL4 + "' TEXT, '" + COL5 + "'INTEGER, '" + COL6 + "'INTEGER, '" + COL7 + "'INTEGER, '" + COL8 + "'INTEGER)";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addData(String item, String desc, int pri, int strat, int person, int urgen) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item);
contentValues.put(COL4, desc);
contentValues.put(COL3, pri);
contentValues.put(COL6, strat);
contentValues.put(COL7, person);
contentValues.put(COL8, urgen);
String query = "SELECT * FROM " + TABLE_NAME + " ORDER BY " + COL3 + " DESC";
db.rawQuery(query, null);
Log.d(TAG, "addData: Adding " + item + " to " + TABLE_NAME);
long result = db.insert(TABLE_NAME, null, contentValues);
//if data as inserted incorrectly it will return -1
if (result == -1) {
return false;
} else {
return true;
}
}
public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
}
public Cursor getItemID(String name){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT " + COL1 + " FROM " + TABLE_NAME +
" WHERE " + COL2 + " = '" + name + "'";
Cursor data = db.rawQuery(query, null);
return data;
}
public String[] databaseToStringArray() {
String[] fromColumns = new String[]{COL2, COL4};
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = db.rawQuery(" SELECT * FROM " + TABLE_NAME + " WHERE 1 ", null);
if (cursor != null && cursor.getCount()>0) {
Log.d("Event", "Records do exist");
}
else {
Log.d("Event", "Records do not exist");
}
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
cursor.moveToNext();
}
db.close();
return fromColumns;
}
}
如果您希望我详细说明或提供更多信息,我们将很高兴为您提供帮助。预先感谢
答案 0 :(得分:0)
您的代码:-
String query = "SELECT * FROM " + TABLE_NAME + " ORDER BY " + COL3 + " DESC";
db.rawQuery(query, null);
将数据提取到按指定顺序排序的游标中。但是,不使用游标。这将不会对存储可用空间的表本身产生影响。
您需要做的是在查询中包含排序(ORDER BY COL3),该查询将返回游标(此方法显示为 getData 方法),该游标用作ListView的源。 / p>
如果以上假设正确,则 getData 方法可能是:-
public Cursor getData(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME + " ORDER BY " + COL3 + " DESC";
return db.rawQuery(query, null);
}
以上几行在 addData 方法内没有任何用处,应将其删除。因此, addData 方法可以是:-
public boolean addData(String item, String desc, int pri, int strat, int person, int urgen) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, item);
contentValues.put(COL4, desc);
contentValues.put(COL3, pri);
contentValues.put(COL6, strat);
contentValues.put(COL7, person);
contentValues.put(COL8, urgen);
Log.d(TAG, "addData: Adding " + item + " to " + TABLE_NAME);
return (db.insert(TABLE_NAME, null, contentValues) > 0);
}