我正在爲android應用程序編寫一個函數,它可以通過導入最新的csv文件來更新sqlite數據庫。 當用戶點擊按鈕時,程序應該刪除舊錶並根據最新數據創建另一個新表。該應用程序能夠在第一次按下按鈕時寫入數據。但是,如果數據已經存在,則發現錯誤。getWritableDatabase不起作用
05-09 16:41:34.088: E/SQLiteDatabase(3056): Error inserting order_qty=1 tran_code=NS item_code=IC559 line_no=1 customer_ref_no="PO# 25192600" order_date=20130221 cust_code=099496
05-09 16:41:34.088: E/SQLiteDatabase(3056): android.database.sqlite.SQLiteConstraintException: columns order_date, cust_code, item_code, tran_code are not unique (code 19)
05-09 16:41:34.088: E/SQLiteDatabase(3056): at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
05-09 16:41:34.088: E/SQLiteDatabase(3056): at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
05-09 16:41:34.088: E/SQLiteDatabase(3056): at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
05-09 16:41:34.088: E/SQLiteDatabase(3056): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
05-09 16:41:34.088: E/SQLiteDatabase(3056): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
05-09 16:41:34.088: E/SQLiteDatabase(3056): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
05-09 16:41:34.088: E/SQLiteDatabase(3056): at test.andftpclient.MainActivity$2.onClick(MainActivity.java:118)
05-09 16:41:34.088: E/SQLiteDatabase(3056): at android.view.View.performClick(View.java:4438)
05-09 16:41:34.088: E/SQLiteDatabase(3056): at android.view.View$PerformClick.run(View.java:18422)
05-09 16:41:34.088: E/SQLiteDatabase(3056): at android.os.Handler.handleCallback(Handler.java:733)
05-09 16:41:34.088: E/SQLiteDatabase(3056): at android.os.Handler.dispatchMessage(Handler.java:95)
05-09 16:41:34.088: E/SQLiteDatabase(3056): at android.os.Looper.loop(Looper.java:136)
05-09 16:41:34.088: E/SQLiteDatabase(3056): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-09 16:41:34.088: E/SQLiteDatabase(3056): at java.lang.reflect.Method.invokeNative(Native Method)
05-09 16:41:34.088: E/SQLiteDatabase(3056): at java.lang.reflect.Method.invoke(Method.java:515)
05-09 16:41:34.088: E/SQLiteDatabase(3056): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-09 16:41:34.088: E/SQLiteDatabase(3056): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-09 16:41:34.088: E/SQLiteDatabase(3056): at dalvik.system.NativeStart.main(Native Method)
我知道出現此錯誤的主要原因是,在插入數據具有相同的唯一主鍵現有的數據,但我爲什麼舊錶,則不能刪除不知道。 這裏是我的代碼:
主要活動
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button_import_csv = (Button) findViewById(R.id.button_import);
button_import_csv.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
DatabaseHelper helper = new DatabaseHelper(getApplicationContext());
SQLiteDatabase db = helper.getWritableDatabase();
try{
FileReader file = new FileReader("/sdcard/downloadedfolder/A1/adv_sales_order.csv");
BufferedReader buffer = new BufferedReader(file);
ContentValues contentValues=new ContentValues();
String line = "";
String tableName ="adv_sales_order";
db.beginTransaction();
while ((line = buffer.readLine()) != null) {
String[] str = line.split("\t");
contentValues.put("order_date", str[0]);
contentValues.put("cust_code", str[1]);
contentValues.put("customer_ref_no", str[2]);
contentValues.put("line_no", str[3]);
contentValues.put("item_code", str[4]);
contentValues.put("tran_code", str[5]);
contentValues.put("order_qty", str[6]);
db.insert(tableName, null, contentValues);
}
db.setTransactionSuccessful();
db.endTransaction();
}catch (IOException e){
}
helper.close();
}
});
}
}
DatabaseHelper類:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "icedb.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_1 = "adv_sales_order";
public DatabaseHelper (Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_TB;
CREATE_TB = "CREATE TABLE adv_sales_order ("
+ "order_date text not null, "
+ "cust_code text not null, "
+ "customer_ref_no text, "
+ "line_no integer not null, "
+ "item_code text not null, "
+ "tran_code text not null, "
+ "order_qty real not null, "
+ "constraint pk_adv_sales_order "
+ "primary key (order_date, "
+ "cust_code, "
+ "item_code, "
+ "tran_code))";
db.execSQL(CREATE_TB);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS "+ TABLE_1);
onCreate(db);
}
public void insert_adv_sales_order (ContentValues values) {
SQLiteDatabase db = getWritableDatabase();
db.insert("adv_sales_order", null, values);
db.close();
}
}
誰能給我一些提示嗎?謝謝。
1)getWriteableDatabse正常工作。 2)我在這裏什麼也看不到會導致你的桌子掉落。 3)在onUpgrade中調用onCreate可能會導致遞歸,直到您用完堆棧。 4)不要關閉數據庫。 5)我懷疑你不明白onUpdate是什麼。 –
謝謝Meike.So我從來沒有每次需要更新數據庫之前如何刪除表?當我調用getWritableDatabase函數時,應該調用onCreate和onUpgrade函數。 drop sql語句包含在onUpgrade函數中。我不明白爲什麼舊數據仍然存在。 – Helloheyyyy
http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run – laalto