我正在尝试对数据库中的数字进行排序,以打印出游戏的五个高分。问题是,当我尝试排序时,我要么按第一个数字排序,要么将两位数字打印到我的高分。混乱是在deleteProduct()方法中。
DBHandler类
public class MyDBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 4;
private static final String DATABASE_NAME = "products.db";
public static final String TABLE_PRODUCTS = "products";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_PRODUCTNAME = "productname";
public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE " + TABLE_PRODUCTS + "(" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT " +
");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
onCreate(db);
}
//add a new row to the database
public void addProduct(Products product){
SQLiteDatabase db = this.getReadableDatabase();
if(DatabaseUtils.queryNumEntries(db, TABLE_PRODUCTS) <= 4){
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCTNAME, product.get_productname());
db.insert(TABLE_PRODUCTS, null, values);
db.close();
}
else{
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCTNAME, product.get_productname());
db.insert(TABLE_PRODUCTS, null, values);
deleteProduct();
db.close();
}
}
//code coming from MainActivity
public void addButtonClicked(String highscore1){
Products product = new Products(highscore1);
addProduct(product);
updateDatabase();
}
//code coming from MainActivity
public void updateDatabase(){
String dbString = databaseToString();
MainActivity.productText.setText(dbString);
}
//delete a product from the database
public void deleteProduct(){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM products WHERE _id NOT IN"+
//only reading first digit of double digit numbers
//" (SELECT _id FROM products ORDER BY productname DESC LIMIT 5)");
//this runs but isnt putting double digit scores into the high score textView
" (SELECT _id FROM products ORDER BY length(productname), productname DESC LIMIT 5)");
}
//print out the database as a string
public String databaseToString(){
String dbString = "";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_PRODUCTS + " WHERE 1";
//Cursor point to location in your results
Cursor c = db.query(TABLE_PRODUCTS, null, null, null, null, null, COLUMN_PRODUCTNAME +" DESC");
//Move to the first row in your results
c.moveToFirst();
while(!c.isAfterLast()){
if(c.getString(c.getColumnIndex("productname")) != null){
dbString += c.getString(c.getColumnIndex("productname"));
dbString += "\n";
}
c.moveToNext();
}
db.close();
return dbString;
}
答案 0 :(得分:0)
SELECT _id FROM products ORDER BY productname DESC LIMIT 5
此查询按productname
列中的值排序。如果这些值是字符串,则排序顺序由第一个字符定义。
您应该存储数字。
如果您无法更改数据库,请在将值用于排序之前将值转换为数字:
SELECT _id FROM products ORDER BY CAST(productname AS NUMBER) DESC LIMIT 5