我正在尝试在Android应用程序中创建一个数据库。它工作正常,没有任何错误,但每次我运行应用程序(如果我们不止一次使用该应用程序)。它会以某种方式将相同的数据添加到现有数据中。我的意思是它继续插入值,它在第一个表上是10行,在第二个表上是15行。当我再次运行时,它将在第1行和第二行30行,并且每次重新运行应用程序时都会不断增加。如何将这些值设置为数据库中存在的默认值?(进一步我想在每次在线数据库中有更改时更新)并使其可以更改?
工作代码:
databasehelper code
public class DatabaseHelper extends SQLiteOpenHelper {
// Logcat tag
private static final String LOG = "DatabaseHelper";
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "Rates";
// Table Names
public static final String KEY_ER_ID = "er_id";
public static final String KEY_ER_USEDORNEW = "Used_or_New";
public static final String KEY_ER_TENOR = "ER_tenor";
public static final String KEY_ER_RATE = "ER_rate";
// Asuransi rate
public static final String KEY_AS_ID = "as_id";
public static final String KEY_AS_REGIONAL = "regional";
public static final String KEY_AS_TENOR = "AS_tenor";
public static final String KEY_AS_TLO = "TLO";
public static final String KEY_AS_COMPREHENSIVE = "Comprehensive";
public static final String KEY_AS_COMBINE = "Combine";
public static final String TABLE_EFFECTIVE_RATE = "effective_rate";
public static final String TABLE_ASURANSI_RATE = "asuransi_rate";
// Common column names
private static final String KEY_ID = "id";
private static final String KEY_CREATED_AT = "created_at";
// Table Create Statements
public static final String CREATE_TABLE_ASURANSI_RATE = "CREATE TABLE " + TABLE_ASURANSI_RATE + " (" + KEY_AS_ID
+ " INTEGER, " + KEY_AS_REGIONAL
+ " INTEGER, " + KEY_AS_TENOR + " INTEGER," + KEY_AS_TLO
+ " REAL," + KEY_AS_COMPREHENSIVE + " REAL,"
+ KEY_AS_COMBINE + " REAL" +")";
public static final String CREATE_TABLE_EFFECTIVE_RATE = "CREATE TABLE " + TABLE_EFFECTIVE_RATE + " ("
+ KEY_ER_ID + " INTEGER, "
+ KEY_ER_USEDORNEW + " TEXT NOT NULL, " + KEY_ER_TENOR
+ " INTEGER," + KEY_ER_RATE + " REAL"+ ")";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// creating required tables
db.execSQL(CREATE_TABLE_EFFECTIVE_RATE);
db.execSQL(CREATE_TABLE_ASURANSI_RATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// on upgrade drop older tables
db.execSQL("DROP TABLE IF EXISTS " + TABLE_EFFECTIVE_RATE);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ASURANSI_RATE);
// create new tables
onCreate(db);
}
/////////////////////////////////////////////////////////////////////////////////////
//ER methods
public long createEntryEffectiveRate(EntryEffectiveRate EER/*, long[] as_ids*/) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues valuesER = new ContentValues();
valuesER.put(KEY_ER_ID, EER.getERId());
valuesER.put(KEY_ER_USEDORNEW, EER.getERKondisi());
valuesER.put(KEY_ER_TENOR, EER.getERTenor());
valuesER.put(KEY_ER_RATE, EER.getERrate());
//values.put(KEY_CREATED_AT, getDateTime());
// insert row
long er_id = db.insert(TABLE_EFFECTIVE_RATE, null, valuesER);
return er_id;
}
//get er
public EntryEffectiveRate getEntryEffectiveRate(long er_id) {
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_EFFECTIVE_RATE + " WHERE "
+ KEY_ER_ID + " = " + er_id;
Log.e(LOG, selectQuery);
Cursor c = db.rawQuery(selectQuery, null);
if (c != null)
c.moveToFirst();
EntryEffectiveRate ergt = new EntryEffectiveRate();
ergt.setERId(c.getInt(c.getColumnIndex(KEY_ER_ID)));
ergt.setERKondisi(c.getString(c.getColumnIndex(KEY_ER_USEDORNEW)));
ergt.setERTenor(c.getInt(c.getColumnIndex(KEY_ER_TENOR)));
ergt.setERRate(c.getDouble(c.getColumnIndex(KEY_ER_RATE)));
//ergt.setCreatedAt(c.getString(c.getColumnIndex(KEY_CREATED_AT)));
c.close();
return ergt;
}
//getting all ER
public List<EntryEffectiveRate> getAllEffectiveRates() {
List<EntryEffectiveRate> EffectiveRates = new ArrayList<EntryEffectiveRate>();
String selectQuery = "SELECT * FROM " + TABLE_EFFECTIVE_RATE;
Log.e(LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
EntryEffectiveRate ergt = new EntryEffectiveRate();
ergt.setERId(c.getInt(c.getColumnIndex(KEY_ER_ID)));
ergt.setERKondisi(c.getString(c.getColumnIndex(KEY_ER_USEDORNEW)));
ergt.setERTenor(c.getInt(c.getColumnIndex(KEY_ER_TENOR)));
ergt.setERRate(c.getDouble(c.getColumnIndex(KEY_ER_RATE)));
// add
EffectiveRates.add(ergt);
} while (c.moveToNext());
}
c.close();
return EffectiveRates;
}
//get er count
public int getEntryEffectiveRateCount() {
String countQuery = "SELECT * FROM " + TABLE_EFFECTIVE_RATE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int count = cursor.getCount();
cursor.close();
// return count
return count;
}
/*
* Updating a todo
*/
public int updateEntryEffectiveRate(EntryEffectiveRate er) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ER_ID, er.getERId());
values.put(KEY_ER_USEDORNEW, er.getERKondisi());
values.put(KEY_ER_TENOR, er.getERTenor());
values.put(KEY_ER_RATE, er.getERrate());
//values.put(KEY_CREATED_AT, getDateTime());
// updating row
return db.update(TABLE_EFFECTIVE_RATE, values, KEY_ER_ID + " = ?",
new String[] { String.valueOf(er.getERId()) });
}
//
public void deleteEntryEffectiveRate(long er_id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_EFFECTIVE_RATE, KEY_ER_ID + " = ?",
new String[] { String.valueOf(er_id) });
}
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
public long createEntryAsuransiRate(EntryAsuransiRate EAR/*, long[] as_ids*/) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues valuesAS = new ContentValues();
valuesAS.put(KEY_AS_ID, EAR.getASId());
valuesAS.put(KEY_AS_REGIONAL, EAR.getASzona());
valuesAS.put(KEY_AS_TENOR, EAR.getAStenor());
valuesAS.put(KEY_AS_TLO, EAR.getAStlo());
valuesAS.put(KEY_AS_COMPREHENSIVE, EAR.getAScomp());
valuesAS.put(KEY_AS_COMBINE, EAR.getAScomb());
//values.put(KEY_CREATED_AT, getDateTime());
// insert row
long er_id = db.insert(TABLE_ASURANSI_RATE, null, valuesAS);
return er_id;
}
//get er
public EntryAsuransiRate getEntryAsuransiRate(long as_id) {
SQLiteDatabase db = this.getReadableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_ASURANSI_RATE + " WHERE "
+ KEY_AS_ID + " = " + as_id;
Log.e(LOG, selectQuery);
Cursor c = db.rawQuery(selectQuery, null);
if (c != null)
c.moveToFirst();
EntryAsuransiRate asgt = new EntryAsuransiRate();
asgt.setASId(c.getInt(c.getColumnIndex(KEY_AS_ID)));
asgt.setASzona(c.getInt(c.getColumnIndex(KEY_AS_REGIONAL)));
asgt.setAStenor(c.getInt(c.getColumnIndex(KEY_AS_TENOR)));
asgt.setAStlo(c.getDouble(c.getColumnIndex(KEY_AS_TLO)));
asgt.setAScomp(c.getDouble(c.getColumnIndex(KEY_AS_COMPREHENSIVE)));
asgt.setAScomb(c.getDouble(c.getColumnIndex(KEY_AS_COMBINE)));
//ergt.setCreatedAt(c.getString(c.getColumnIndex(KEY_CREATED_AT)));
c.close();
return asgt;
}
//getting all ER
public List<EntryAsuransiRate> getAllAsuransiRates() {
List<EntryAsuransiRate> AsuransiRates = new ArrayList<EntryAsuransiRate>();
String selectQuery = "SELECT * FROM " + TABLE_ASURANSI_RATE;
Log.e(LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
EntryAsuransiRate asgt = new EntryAsuransiRate();
asgt.setASId(c.getInt(c.getColumnIndex(KEY_AS_ID)));
asgt.setASzona(c.getInt(c.getColumnIndex(KEY_AS_REGIONAL)));
asgt.setAStenor(c.getInt(c.getColumnIndex(KEY_AS_TENOR)));
asgt.setAStlo(c.getDouble(c.getColumnIndex(KEY_AS_TLO)));
asgt.setAScomp(c.getDouble(c.getColumnIndex(KEY_AS_COMPREHENSIVE)));
asgt.setAScomb(c.getDouble(c.getColumnIndex(KEY_AS_COMBINE)));
// add
AsuransiRates.add(asgt);
} while (c.moveToNext());
}
c.close();
return AsuransiRates;
}
//get er count
public int getEntryAsuransiRateCount() {
String countQuery = "SELECT * FROM " + TABLE_ASURANSI_RATE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int count = cursor.getCount();
cursor.close();
// return count
return count;
}
/*
* Updating a todo
*/
public int updateEntryAsuransiRate(EntryAsuransiRate EAR) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues valuesAS = new ContentValues();
valuesAS.put(KEY_AS_ID, EAR.getASId());
valuesAS.put(KEY_AS_REGIONAL, EAR.getASzona());
valuesAS.put(KEY_AS_TENOR, EAR.getAStenor());
valuesAS.put(KEY_AS_TLO, EAR.getAStlo());
valuesAS.put(KEY_AS_COMPREHENSIVE, EAR.getAScomp());
valuesAS.put(KEY_AS_COMBINE, EAR.getAScomb());
//values.put(KEY_CREATED_AT, getDateTime());
// updating row
return db.update(TABLE_ASURANSI_RATE, valuesAS, KEY_AS_ID + " = ?",
new String[] { String.valueOf(EAR.getASId()) });
}
//
public void deleteEntryAsuransiRate(long as_id) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_ASURANSI_RATE, KEY_AS_ID + " = ?",
new String[] { String.valueOf(as_id) });
}
//////////////////////////////////////////////////////////////////////////////////
// closing database
public void closeDB() {
SQLiteDatabase db = this.getReadableDatabase();
if (db != null && db.isOpen())
db.close();
}
/**
* get datetime
* */
private String getDateTime() {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.getDefault());
Date date = new Date();
return dateFormat.format(date);
}
}
将插入值的主要活动:
public class MainActivity extends Activity {
// Database Helper
DatabaseHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DatabaseHelper(getApplicationContext());
EntryEffectiveRate er_id = new EntryEffectiveRate(1, "Baru", 12, 12.1);
EntryEffectiveRate er_id1 = new EntryEffectiveRate(2, "Baru", 24, 12.2);
EntryEffectiveRate er_id2 = new EntryEffectiveRate(3, "Baru", 36, 12.3);
EntryEffectiveRate er_id3 = new EntryEffectiveRate(4, "Baru", 48, 12.4);
EntryEffectiveRate er_id4 = new EntryEffectiveRate(5, "Baru", 60, 12.5);
EntryEffectiveRate er_id5 = new EntryEffectiveRate(6, "Bekas", 12,
12.6);
EntryEffectiveRate er_id6 = new EntryEffectiveRate(7, "Bekas", 24,
12.7);
EntryEffectiveRate er_id7 = new EntryEffectiveRate(8, "Bekas", 36,
12.8);
EntryEffectiveRate er_id8 = new EntryEffectiveRate(9, "Bekas", 48,
12.9);
EntryEffectiveRate er_id9 = new EntryEffectiveRate(10, "Bekas", 60,
12.10);
long er = db.createEntryEffectiveRate(er_id);
long er1 = db.createEntryEffectiveRate(er_id1);
long er2 = db.createEntryEffectiveRate(er_id2);
long er3 = db.createEntryEffectiveRate(er_id3);
long er4 = db.createEntryEffectiveRate(er_id4);
long er5 = db.createEntryEffectiveRate(er_id5);
long er6 = db.createEntryEffectiveRate(er_id6);
long er7 = db.createEntryEffectiveRate(er_id7);
long er8 = db.createEntryEffectiveRate(er_id8);
long er9 = db.createEntryEffectiveRate(er_id9);
Log.d("Effectiverate Count", " EffectiverateCount: "
+ db.getAllEffectiveRates().size());
EntryAsuransiRate as = new EntryAsuransiRate(1, 1, 12, 0.07, 2.07,
2.14);
EntryAsuransiRate as1 = new EntryAsuransiRate(2, 1, 24, 0.08, 2.06,
2.15);
EntryAsuransiRate as2 = new EntryAsuransiRate(3, 1, 36, 0.09, 2.05,
2.16);
EntryAsuransiRate as3 = new EntryAsuransiRate(4, 1, 48, 0.10, 2.04,
2.17);
EntryAsuransiRate as4 = new EntryAsuransiRate(5, 1, 60, 0.11, 2.03,
2.18);
EntryAsuransiRate as5 = new EntryAsuransiRate(6, 2, 12, 0.12, 2.02,
2.19);
EntryAsuransiRate as6 = new EntryAsuransiRate(7, 2, 24, 0.13, 2.01,
2.20);
EntryAsuransiRate as7 = new EntryAsuransiRate(8, 2, 36, 0.14, 2.00,
2.21);
EntryAsuransiRate as8 = new EntryAsuransiRate(9, 2, 48, 0.15, 1.99,
2.22);
EntryAsuransiRate as9 = new EntryAsuransiRate(10, 2, 60, 0.16, 1.98,
2.23);
EntryAsuransiRate as10 = new EntryAsuransiRate(11, 3, 12, 0.17, 1.97,
2.24);
EntryAsuransiRate as11 = new EntryAsuransiRate(12, 3, 24, 0.18, 1.96,
2.25);
EntryAsuransiRate as12 = new EntryAsuransiRate(13, 3, 36, 0.19, 1.95,
2.26);
EntryAsuransiRate as13 = new EntryAsuransiRate(14, 3, 48, 0.20, 1.94,
2.27);
EntryAsuransiRate as14 = new EntryAsuransiRate(15, 3, 60, 0.21, 1.93,
2.28);
long as_id = db.createEntryAsuransiRate(as);
long as_id1 = db.createEntryAsuransiRate(as1);
long as_id2 = db.createEntryAsuransiRate(as2);
long as_id3 = db.createEntryAsuransiRate(as3);
long as_id4 = db.createEntryAsuransiRate(as4);
long as_id5 = db.createEntryAsuransiRate(as5);
long as_id6 = db.createEntryAsuransiRate(as6);
long as_id7 = db.createEntryAsuransiRate(as7);
long as_id8 = db.createEntryAsuransiRate(as8);
long as_id9 = db.createEntryAsuransiRate(as9);
long as_id10 = db.createEntryAsuransiRate(as10);
long as_id11 = db.createEntryAsuransiRate(as11);
long as_id12 = db.createEntryAsuransiRate(as12);
long as_id13 = db.createEntryAsuransiRate(as13);
long as_id14 = db.createEntryAsuransiRate(as14);
Log.d("Asuransirate Count", " AsuransirateCount: "
+ db.getAllAsuransiRates().size());
// getting all ER
Log.d("Get ER", "GEtting All ER");
List<EntryEffectiveRate> allEffectiveRate = db.getAllEffectiveRates();
for (EntryEffectiveRate ER : allEffectiveRate) {
Log.d("ER_ids", String.valueOf(ER.getERId()));
Log.d("ER_rates", ER.getERKondisi());
Log.d("ER_tenors", String.valueOf(ER.getERTenor()));
Log.d("ER_rates", String.valueOf(ER.getERrate()));
}
// getting all AS
Log.d("Get AS", "GEtting All AS");
List<EntryAsuransiRate> allAsuransiRate = db.getAllAsuransiRates();
for (EntryAsuransiRate AS : allAsuransiRate) {
Log.d("AS_ids", String.valueOf(AS.getASId()));
Log.d("AS_tenors", String.valueOf(AS.getAStenor()));
Log.d("AS_tlos", String.valueOf(AS.getAStlo()));
Log.d("AS_comps", String.valueOf(AS.getAScomp()));
Log.d("AS_combs", String.valueOf(AS.getAScomb()));
}
// Don't forget to close database connection
db.closeDB();
}
}
和模型类:
public class EntryAsuransiRate {
int as_id;
int ASzona;
int AStenor;
double AStlo;
double AScomp;
double AScomb;
// String created_at;
// constructors
public EntryAsuransiRate() {
}
public EntryAsuransiRate(int ASzona, int AStenor, double AStlo,
double AScomp, double AScomb) {
this.ASzona = ASzona;
this.AStenor = AStenor;
this.AStlo = AStlo;
this.AScomp = AScomp;
this.AScomb = AScomb;
}
public EntryAsuransiRate(int as_id, int ASzona, int AStenor, double AStlo,
double AScomp, double AScomb) {
this.as_id = as_id;
this.ASzona = ASzona;
this.AStenor = AStenor;
this.AStlo = AStlo;
this.AScomp = AScomp;
this.AScomb = AScomb;
}
// setters
public void setASId(int as_id) {
this.as_id = as_id;
}
public void setASzona(int ASzona) {
this.ASzona = ASzona;
}
public void setAStenor(int AStenor) {
this.AStenor = AStenor;
}
public void setAStlo(double AStlo) {
this.AStlo = AStlo;
}
public void setAScomp(double AScomp) {
this.AScomp = AScomp;
}
public void setAScomb(double AScomb) {
this.AScomb = AScomb;
}
// public void setCreatedAt(String created_at){
// this.created_at = created_at;
// }
// getters
public long getASId() {
return this.as_id;
}
public int getASzona() {
return this.ASzona;
}
public int getAStenor() {
return this.AStenor;
}
public double getAStlo() {
return this.AStlo;
}
public double getAScomp() {
return this.AScomp;
}
public double getAScomb() {
return this.AScomb;
}
}
另一种模式:
public class EntryEffectiveRate {
int er_id;
String ERkondisi;
int ERtenor;
double ERrate;
//String created_at;
// constructors
public EntryEffectiveRate() {
}
public EntryEffectiveRate(String ERkondisi, int ERtenor, double ERrate) {
this.ERkondisi = ERkondisi;
this.ERtenor = ERtenor;
this.ERrate = ERrate;
}
public EntryEffectiveRate(int er_id, String ERkondisi, int ERtenor, double ERrate) {
this.er_id = er_id;
this.ERkondisi = ERkondisi;
this.ERtenor = ERtenor;
this.ERrate = ERrate;
}
// setters
public void setERId(int er_id) {
this.er_id = er_id;
}
public void setERKondisi(String ERkondisi) {
this.ERkondisi = ERkondisi;
}
public void setERTenor(int ERtenor) {
this.ERtenor = ERtenor;
}
public void setERRate(double ERrate){
this.ERrate = ERrate;
}
//public void setCreatedAt(String created_at){
// this.created_at = created_at;
//}
// getters
public long getERId() {
return this.er_id;
}
public String getERKondisi() {
return this.ERkondisi;
}
public int getERTenor() {
return this.ERtenor;
}
public double getERrate(){
return this.ERrate;
}
}
结果将出现在logcat中,如下所示:
04-02 13:00:19.758: E/DatabaseHelper(360): SELECT * FROM effective_rate
04-02 13:00:19.779: D/Effectiverate Count(360): EffectiverateCount: 20
04-02 13:00:21.068: E/DatabaseHelper(360): SELECT * FROM asuransi_rate
04-02 13:00:21.079: D/Asuransirate Count(360): AsuransirateCount: 30
04-02 13:00:21.079: D/Get ER(360): GEtting All ER
04-02 13:00:21.089: E/DatabaseHelper(360): SELECT * FROM effective_rate
04-02 13:00:21.099: D/ER_ids(360): 1
04-02 13:00:21.099: D/ER_rates(360): Baru
04-02 13:00:21.099: D/ER_tenors(360): 12
04-02 13:00:21.099: D/ER_rates(360): 12.1
04-02 13:00:21.099: D/ER_ids(360): 2
04-02 13:00:21.099: D/ER_rates(360): Baru
04-02 13:00:21.099: D/ER_tenors(360): 24
04-02 13:00:21.099: D/ER_rates(360): 12.2
04-02 13:00:21.099: D/ER_ids(360): 3
04-02 13:00:21.099: D/ER_rates(360): Baru
04-02 13:00:21.099: D/ER_tenors(360): 36
04-02 13:00:21.099: D/ER_rates(360): 12.3
04-02 13:00:21.099: D/ER_ids(360): 4
04-02 13:00:21.099: D/ER_rates(360): Baru
04-02 13:00:21.099: D/ER_tenors(360): 48
04-02 13:00:21.099: D/ER_rates(360): 12.4
04-02 13:00:21.099: D/ER_ids(360): 5
04-02 13:00:21.109: D/ER_rates(360): Baru
04-02 13:00:21.109: D/ER_tenors(360): 60
04-02 13:00:21.119: D/ER_rates(360): 12.5
04-02 13:00:21.119: D/ER_ids(360): 6
04-02 13:00:21.119: D/ER_rates(360): Bekas
04-02 13:00:21.119: D/ER_tenors(360): 12
04-02 13:00:21.119: D/ER_rates(360): 12.6
04-02 13:00:21.119: D/ER_ids(360): 7
04-02 13:00:21.129: D/ER_rates(360): Bekas
04-02 13:00:21.129: D/ER_tenors(360): 24
04-02 13:00:21.129: D/ER_rates(360): 12.7
04-02 13:00:21.129: D/ER_ids(360): 8
04-02 13:00:21.129: D/ER_rates(360): Bekas
04-02 13:00:21.129: D/ER_tenors(360): 36
04-02 13:00:21.129: D/ER_rates(360): 12.8
04-02 13:00:21.139: D/ER_ids(360): 9
04-02 13:00:21.139: D/ER_rates(360): Bekas
04-02 13:00:21.139: D/ER_tenors(360): 48
04-02 13:00:21.139: D/ER_rates(360): 12.9
04-02 13:00:21.139: D/ER_ids(360): 10
04-02 13:00:21.149: D/ER_rates(360): Bekas
04-02 13:00:21.149: D/ER_tenors(360): 60
04-02 13:00:21.149: D/ER_rates(360): 12.1
04-02 13:00:21.188: D/Get AS(360): GEtting All AS
04-02 13:00:21.188: E/DatabaseHelper(360): SELECT * FROM asuransi_rate
04-02 13:00:21.399: D/AS_ids(360): 1
04-02 13:00:21.399: D/AS_tenors(360): 12
04-02 13:00:21.399: D/AS_tlos(360): 0.07
04-02 13:00:21.399: D/AS_comps(360): 2.07
04-02 13:00:21.408: D/AS_combs(360): 2.14
.
.
.
04-02 13:00:21.509: D/AS_ids(360): 15
04-02 13:00:21.509: D/AS_tenors(360): 60
04-02 13:00:21.519: D/AS_tlos(360): 0.21
04-02 13:00:21.519: D/AS_comps(360): 1.93
04-02 13:00:21.519: D/AS_combs(360): 2.28
由于身体限制字符我缩短了
答案 0 :(得分:1)
显然,当您开始MainActivity
时,它会反复插入所有记录。
解决方案:在public void onCreate(SQLiteDatabase db)
方法中写下您的插入代码因为此方法在创建数据库时只执行一次。
@Override
public void onCreate(SQLiteDatabase db) {
// creating required tables
db.execSQL(CREATE_TABLE_EFFECTIVE_RATE);
db.execSQL(CREATE_TABLE_ASURANSI_RATE);
EntryEffectiveRate er_id = new EntryEffectiveRate(1, "Baru", 12, 12.1);
EntryEffectiveRate er_id1 = new EntryEffectiveRate(2, "Baru", 24, 12.2);
EntryEffectiveRate er_id2 = new EntryEffectiveRate(3, "Baru", 36, 12.3);
EntryEffectiveRate er_id3 = new EntryEffectiveRate(4, "Baru", 48, 12.4);
EntryEffectiveRate er_id4 = new EntryEffectiveRate(5, "Baru", 60, 12.5);
EntryEffectiveRate er_id5 = new EntryEffectiveRate(6, "Bekas", 12,
12.6);
EntryEffectiveRate er_id6 = new EntryEffectiveRate(7, "Bekas", 24,
12.7);
EntryEffectiveRate er_id7 = new EntryEffectiveRate(8, "Bekas", 36,
12.8);
EntryEffectiveRate er_id8 = new EntryEffectiveRate(9, "Bekas", 48,
12.9);
EntryEffectiveRate er_id9 = new EntryEffectiveRate(10, "Bekas", 60,
12.10);
long er = createEntryEffectiveRate(er_id);
long er1 = createEntryEffectiveRate(er_id1);
long er2 = createEntryEffectiveRate(er_id2);
long er3 = createEntryEffectiveRate(er_id3);
long er4 = createEntryEffectiveRate(er_id4);
long er5 = createEntryEffectiveRate(er_id5);
long er6 = createEntryEffectiveRate(er_id6);
long er7 = createEntryEffectiveRate(er_id7);
long er8 = createEntryEffectiveRate(er_id8);
long er9 = createEntryEffectiveRate(er_id9);
//Do the same for other table records.
}