0

我正在制作一个可以记住用户单词的字典,但我想将单词列表限制在 100 个单词左右。我怎样才能限制这个单词列表?我试图找到一些信息来做到这一点,但似乎很难。请帮我!这是我的创建数据库方法:

    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();
}
}
4

1 回答 1

0

您可以使用 a 找到最小 rowid

从联系人中选择最小值(ROWID)

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

于 2013-02-22T13:12:17.277 回答