我正在为Android应用程序编写函数,该函数可以通过导入最新的csv文件来更新sqlite数据库。
当用户单击按钮时,程序应该删除旧表并根据最新数据创建另一个新表。首次按下该按钮时,该应用程序能够写入数据。但是,如果已经存在数据,则会发现错误。

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();
}


}

谁能给我一些提示?谢谢。

最佳答案

我认为您误解了onCreateonUpdate的用法。数据库不存在时,不是每次都调用onCreate而是每次调用getWriteableDatabase都调用getWriteableDatabase。同样,当现有数据库中的版本号与代码中的版本号不匹配时,将调用onUpdate

为了删除表,给定您的代码,我想您可以显式调用onUpdate作为函数insert_adv_sales_order的第二行。

09-26 15:39