我正在开发一个使用非常简单的数据库的应用程序。我只需要保存一个描述(STRING),它是用户使用的应用程序的历史结果。
我创建了一个名为ScoreHelper
的辅助类。
package com.example.cobaandroid;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class ScoreHelper extends SQLiteOpenHelper {
private static final int SCHEMA_VERSION = 1;
public ScoreHelper(Context context)
{
super(context,"score.db",null,SCHEMA_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
//boolean dbExist = checkDataBase();
db.execSQL("CREATE TABLE score (_id INTEGER PRIMARY KEY AUTOINCREMENT,nama VARCHAR,akurasi VARCHAR,keterangan VARCHAR);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public Cursor getAll()
{
return(getReadableDatabase().rawQuery("SELECT _id,nama,akurasi,keterangan FROM score ORDER BY nama",null));
}
public void insert(String name,String akurasi,String keterangan)
{
SQLiteDatabase db =this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("nama", name);
cv.put("akurasi", akurasi);
cv.put("keterangan", keterangan);
db.insert("score", null, cv);
db.close();
}
}
这是我的主要名为Analyze.java
public class Analyze extends Activity {
Cursor model = null;
ScoreHelper scoreHelper = null;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_analyze);
scoreHelper = new ScoreHelper(this);
model = scoreHelper.getAll();
final double totalAkurasi = hitungAkurasi(knn.length, knn, nama,listFileDataTest.length);
insertScoreToDB(totalAkurasi);
Button scoreHistory = (Button)findViewById(R.id.scorebutton);
scoreHistory.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent scoreIntent = new Intent(Analyze.this,CamScoreHistory.class);
startActivity(scoreIntent);
}
});
}
private void insertScoreToDB(double akurasi)
{
String dateCaptured = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
String concatDateandImg = "Image captured at " + dateCaptured;
String keterangan = "";
if(akurasi >= 83.333)
{
keterangan = "YES";
}
else
{
keterangan = "NO";
}
if(model.moveToFirst())
{
do
{
scoreHelper.insert(concatDateandImg, String.valueOf(akurasi), keterangan);
}while(model.moveToNext());
}
}
这就是CamScoreHistory.java,我希望在列表中显示数据库中的数据的类
package com.example.cobaandroid;
import java.util.ArrayList;
import android.app.Activity;
import android.app.ListActivity;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class CamScoreHistory extends ListActivity {
Cursor model = null;
ScoreHelper scoreHelper = null;
String nama= "";
String akurasi="";
String keterangan="";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
scoreHelper = new ScoreHelper(this);
model = scoreHelper.getAll();
if(model.moveToFirst())
{
do
{
nama = model.getString(model.getColumnIndex("nama"));
akurasi = model.getString(model.getColumnIndex("akurasi"));
keterangan = model.getString(model.getColumnIndex("keterangan"));
}while(model.moveToNext());
}
ArrayList<String> results = new ArrayList<String>();
results.add(nama+ " Akurasi " + akurasi + " Keterangan "+keterangan);
this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
}
}
问题是,我能够将数据插入到我的数据库中,但它总是覆盖以前的数据。我想要的只是插入的新数据不会覆盖上一个数据。