使SQLDatabase受限

时间:2013-02-22 12:57:12

标签: android database sqlite

我正在制作一本可以记住用户单词的词典,但我想限制单词列表大约100个单词。我该如何限制这个单词列表?我试图找到一些信息,但似乎很难。请帮我! 这是我的create database方法:

    public class DataProvider extends SQLiteOpenHelper {


// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "contactsManager";

// Contacts table name
private static final String TABLE = "contacts";

public DataProvider(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Contacts Table Columns names
private static final String KEY_NAME = "name";
private static final String KEY_DECRIBED = "described";

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
   String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE + "("
         +KEY_NAME + " TEXT,"
        + KEY_DECRIBED + " TEXT" + ")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXISTS " + TABLE);

    // Create tables again
    onCreate(db);
}

public void addContact(Name contact) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, contact.getName()); // Contact Name
    values.put(KEY_DECRIBED, contact.getDescribed()); // Contact Described

    // Inserting Row
    db.insert(TABLE, null, values);
    db.close(); // Closing database connection
}

 // Getting single contact
public Name getContact(int id) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE, new String[] {
            KEY_NAME, KEY_DECRIBED },
            null, new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();

    Name contact = new Name(cursor.getString(0),
            cursor.getString(1));
    // return contact
    return contact;
}

public List<Name> getAllContacts() {
    List<Name> contactList = new ArrayList<Name>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE;

   SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
         do {
            Name contact = new Name();

            contact.SetName(cursor.getString(0));
            contact.SetDescribed(cursor.getString(1));
            // Adding contact to list
            contactList.add(contact);
        } while (cursor.moveToNext());
    }

    // return contact list
    return contactList;
}

// Getting contacts Count
public int getContactsCount() {
    String countQuery = "SELECT  * FROM " + TABLE;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    // return count
    return cursor.getCount();
}
}

1 个答案:

答案 0 :(得分:0)

你可以找到带有

的min rowid

SELECT MIN(ROWID)FROM contacts

表的

并取消它。然后你可以添加你的新单词。或者如果你很懒,只需在SQL中使用“LIMIT 100”子句