我有一个Sqlite数据库,我将其限制为最多10行最新条目。逻辑是在插入任何新条目时我检查行数。如果已有10行,则会删除最早输入的行并添加新行。但我收到错误SQLiteException: near "BYid": syntax error (code 1): , while compiling: DELETE FROM profORDER BYid LIMIT 1
。这是因为任何空间不匹配或查询错误吗?
public class SQLiteHandler extends SQLiteOpenHelper {
private static final String TAG = SQLiteHandler.class.getSimpleName();
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "android_api";
// Profile Settings table name
private static final String TABLE_PROF = "prof";
// Profile Settings information names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_MOBILE = "mobile";
public SQLiteHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_PROF_TABLE = "CREATE TABLE " + TABLE_PROF + "("+KEY_ID+" INTEGER PRIMARY KEY, "+KEY_NAME+" TEXT, "+KEY_MOBILE+" TEXT" + ")";
db.execSQL(CREATE_PROF_TABLE);
Log.d(TAG, "Database tables created");
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROF);
// Create tables again
onCreate(db);
}
/**
* Storing Prof_settings details in database
* */
public void addProfile(String name, String mobile){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, name);
values.put(KEY_MOBILE, mobile);
int count = getProfilesCount();
if(count == 10) {
String DELETE_ROW = "DELETE FROM " +TABLE_PROF+ "ORDER BY" +KEY_ID+" LIMIT 1";
db.execSQL(DELETE_ROW); // will delete oldest record
}
long id = db.insert(TABLE_PROF, null, values); // insert to 1st row
db.close(); // Closing database connection
Log.d(TAG, "New profile settings inserted into sqlite: " + id);
}
/**
* Getting Profile Settings data from database
* */
public ArrayList<HashMap<String, String>> getProfDetails()
{
ArrayList<HashMap<String, String>> array_list = new ArrayList<HashMap<String, String>>();
SQLiteDatabase db = this.getReadableDatabase();
//Cursor res = db.rawQuery("SELECT * " + " FROM " + TABLE_PROF + " GROUP BY " + KEY_NAME + " ORDER BY " + KEY_MOBILE + " COLLATE NOCASE;", null);
String selectQuery = "SELECT * FROM " + TABLE_PROF;
Cursor res = db.rawQuery(selectQuery, null);
res.moveToFirst();
while (res.isAfterLast() == false)
{
HashMap<String, String> hashmap= new HashMap<String, String>();
hashmap.put("id", res.getString(res.getColumnIndex(KEY_ID)));
hashmap.put("name", res.getString(res.getColumnIndex(KEY_NAME)));
hashmap.put("mobile", res.getString(res.getColumnIndex(KEY_MOBILE)));
array_list.add(hashmap);
res.moveToNext();
}
return array_list;
}
public int getProfilesCount() {
String countQuery = "SELECT * FROM " + TABLE_PROF;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int cnt = cursor.getCount();
cursor.close();
return cnt;
}
/**
* Re crate database Delete all tables and create them again
* */
public void deleteUsers() {
SQLiteDatabase db = this.getWritableDatabase();
// Delete All Rows
db.delete(TABLE_PROF, null, null);
db.close();
Log.d(TAG, "Deleted all profile info from sqlite");
}
}
答案 0 :(得分:1)
更改此
"DELETE FROM " +TABLE_PROF+ "ORDER BY" +KEY_ID+" LIMIT 1";
到
"DELETE FROM " +TABLE_PROF+ " ORDER BY " +KEY_ID+" LIMIT 1";
只需在ORDER BY
之前和之后放置空格。
正如@TimBiegeleisen指出的那样,LIMIT
子句不适用于DELETE
。
这是一个解决方法 -
"DELETE FROM " + TABLE_PROF
+ " WHERE someColumn in ( "
+ " SELECT someColumn FROM " + TABLE_PROF + " ORDER BY " + KEY_ID + " LIMIT 1"
+ ")";
答案 1 :(得分:0)
String DELETE_ROW = "DELETE FROM " +TABLE_PROF+ "ORDER BY" +KEY_ID+" LIMIT 1";
这应该是
String DELETE_ROW = "DELETE FROM " +TABLE_PROF+ " ORDER BY " +KEY_ID+" LIMIT 1";
像这样。
注意ORDER之前和BY之后的空格。