我想在android中请求一些关于delete()的帮助。 在我的程序中,我尝试在插入任何新数据之前删除表中的所有记录。首次按下“导入数据”按钮时,可以正常插入记录。但是,如果再次按下按钮而不重新安装应用程序,则会在LogCat中找到错误。
以下是发现的错误:
Logcat错误:
07-03 10:46:19.829: D/ice(25736): Path of csv folder get in modile
07-03 10:46:20.809: D/ice(25736): Name of csv was get and change into string
07-03 10:46:22.389: D/ice(25736): Csv column sucessfully read
07-03 10:46:25.399: D/ice(25736): Read next record
07-03 10:46:27.669: E/SQLiteDatabase(25736): Error inserting tran_code=NS order_qty =2 item_code=IC559 line_no=1 customer_ref_no="PO# 29051925" order_date=20140702 cust_code=094659
07-03 10:46:27.669: E/SQLiteDatabase(25736): android.database.sqlite.SQLiteConstraintException: columns order_date, cust_code, item_code, tran_code are not unique (code 19)
07-03 10:46:27.669: E/SQLiteDatabase(25736): at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
07-03 10:46:27.669: E/SQLiteDatabase(25736): at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782)
07-03 10:46:27.669: E/SQLiteDatabase(25736): at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
07-03 10:46:27.669: E/SQLiteDatabase(25736): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
07-03 10:46:27.669: E/SQLiteDatabase(25736): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
07-03 10:46:27.669: E/SQLiteDatabase(25736): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
07-03 10:46:27.669: E/SQLiteDatabase(25736): at com.iceapp.ImportCsvActivity$2.onClick(ImportCsvActivity.java:197)
07-03 10:46:27.669: E/SQLiteDatabase(25736): at android.view.View.performClick(View.java:4438)
07-03 10:46:27.669: E/SQLiteDatabase(25736): at android.view.View$PerformClick.run(View.java:18422)
07-03 10:46:27.669: E/SQLiteDatabase(25736): at android.os.Handler.handleCallback(Handler.java:733)
07-03 10:46:27.669: E/SQLiteDatabase(25736): at android.os.Handler.dispatchMessage(Handler.java:95)
07-03 10:46:27.669: E/SQLiteDatabase(25736): at android.os.Looper.loop(Looper.java:136)
07-03 10:46:27.669: E/SQLiteDatabase(25736): at android.app.ActivityThread.main(ActivityThread.java:5001)
07-03 10:46:27.669: E/SQLiteDatabase(25736): at java.lang.reflect.Method.invokeNative(Native Method)
07-03 10:46:27.669: E/SQLiteDatabase(25736): at java.lang.reflect.Method.invoke(Method.java:515)
07-03 10:46:27.669: E/SQLiteDatabase(25736): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
07-03 10:46:27.669: E/SQLiteDatabase(25736): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
07-03 10:46:27.669: E/SQLiteDatabase(25736): at dalvik.system.NativeStart.main(Native Method)
我认为上述错误是由于插入具有相同主键的多个记录。
在执行delete语句期间,我没有收到任何错误或消息。任何人都可以帮我弄清楚为什么删除语句不能正常工作???
Java代码:
button_import_csv.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
DatabaseHelper helper = new DatabaseHelper(getApplicationContext());
SQLiteDatabase db = helper.getWritableDatabase();
Log.d("ice","database opened");
db.delete(tableName[1], null, null );
db.delete(tableName[2], null, null );
db.delete(tableName[3], null, null );
db.delete(tableName[4], null, null );
db.delete(tableName[5], null, null );
db.delete(tableName[6], null, null );
db.delete(tableName[7], null, null );
db.delete(tableName[8], null, null );
db.delete(tableName[9], null, null );
db.delete(tableName[10], null, null );
db.delete(tableName[11], null, null );
db.delete(tableName[12], null, null );
db.delete(tableName[13], null, null );
db.delete(tableName[14], null, null );
db.delete(tableName[15], null, null );
db.delete(tableName[16], null, null );
db.delete(tableName[17], null, null );
db.delete(tableName[18], null, null );
db.delete(tableName[19], null, null );
Log.d("ice","old data existed in database was all cleared");
try{
File csvpath = new File("/sdcard/downloadedfolder/"+route_folder);
Log.d("ice","Path of csv folder get in modile");
File[] csvfile = csvpath.listFiles();
//get all csv files' name and store it as array
Log.d("ice","Name of csv was get and change into string");
//Put all name of csv into a string array
FileReader file = new FileReader(csvfile[0]);
BufferedReader buffer = new BufferedReader(file);
ContentValues contentValues=new ContentValues();
String line = buffer.readLine(); //read first line to get the column
Log.d("ice","Csv column sucessfully read");
String[] cols = line.split("\t");
db.beginTransaction();
for(int j=0;j<24;j++){
while ((line = buffer.readLine()) != null) {
Log.d("ice","Read next record");
//read every single line of record in csv
String[] str = line.split("\t");
for (int i = 0; i < cols.length; i++) {
contentValues.put(cols[i], str[i]);
}
db.insert(tableName[j], null, contentValues);
}
}
buffer.close();
db.setTransactionSuccessful();
db.endTransaction();
}catch (IOException e){
}
helper.close();
}
});
答案 0 :(得分:0)
我认为问题出在tablename [1]中,如果要删除所有记录,请将其更改为:
db.delete(tablename,null,null);
这应该有效,因为当你将选择参数设置为null时,它会删除所有表的条目。