我在 Android 中创建了一个简单的数据库。它有 3 列 RowID、UPCCode 和 Title。我已使用 youtube 上的 NewBoston 教程成功地将数据输入数据库。现在我想要将我的数据库限制为 10 条记录(例如最后 10 次搜索),并且我想在新活动中将它们显示到 ListView 中。
我在如何实现这一点方面遇到问题,找不到好的帮助.. 代码发布在下面...
TenSearches.java
package com.rhuf.brandscan;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class TenSearches {
public static final String KEY_ROWID="ID";
public static final String KEY_UPC="upccode";
public static final String KEY_TITLE="title";
public static final String DATABASE_NAME="BrandscanDB";
public static final String DATABASE_TABLE="ScanTable";
public static final int DATABASE_VERSION=1;
private DBHelper helper;
private final Context context;
private SQLiteDatabase BrandScanDB;
public TenSearches(Context c)
{
context = c;
}
public TenSearches open() throws SQLException
{
helper = new DBHelper(context);
BrandScanDB = helper.getWritableDatabase();
return this;
}
public void close()
{
helper.close();
}
public long createEntry(String upc, String title)
{
ContentValues cv = new ContentValues();
cv.put(KEY_UPC, upc);
cv.put(KEY_TITLE, title);
return BrandScanDB.insert(DATABASE_TABLE, null, cv);
}
public String getDataUPC()
{
String columns[]=new String[]{KEY_ROWID,KEY_UPC,KEY_TITLE};
Cursor c = BrandScanDB.query(DATABASE_TABLE, columns, null, null, null, null, KEY_ROWID);
String result;
int iRow = c.getColumnIndex(KEY_ROWID);
int iUPC = c.getColumnIndex(KEY_UPC);
int iTitle = c.getColumnIndex(KEY_TITLE);
//Make a listArray or Object Array from the Database entries
return null;
}
}
DBHelper.java
package com.rhuf.brandscan;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.rhuf.brandscan.TenSearches;
public class DBHelper extends SQLiteOpenHelper{
public DBHelper(Context context) {
super(context,TenSearches.DATABASE_NAME,null,TenSearches.DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " +TenSearches.DATABASE_TABLE +" ("+
TenSearches.KEY_ROWID +" INTEGER PRIMARY KEY AUTOINCREMENT, " +
TenSearches.KEY_UPC +" TEXT NOT NULL, " +
TenSearches.KEY_TITLE + " TEXT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TenSearches.DATABASE_TABLE);
onCreate(db);
}
}