บทนำ
สิ่งนี้ขึ้นอยู่กับ java ของ Android และเป็นตัวอย่างที่ดีในการเปลี่ยนฐานข้อมูลโดยไม่ทำให้แฟน ๆ / ลูกค้าแอปพลิเคชันของคุณรำคาญ นี่เป็นไปตามแนวคิดของหน้าคำถามที่พบบ่อย SQLite
http://sqlite.org/faq.html#q11
ปัญหา
ฉันไม่ได้สังเกตว่าฉันต้องตั้งค่า row_number หรือ record_id เพื่อลบสินค้าที่ซื้อรายการเดียวในใบเสร็จและในขณะเดียวกันหมายเลขบาร์โค้ดของสินค้าก็หลอกให้ฉันคิดว่าจะทำให้เป็นกุญแจสำคัญในการลบรายการนั้น ฉันกำลังบันทึกรายละเอียดใบเสร็จในรหัสใบเสร็จรับเงินในตาราง การทิ้งไว้โดยไม่มี record_id อาจหมายถึงการลบระเบียนทั้งหมดของรายการเดียวกันในใบเสร็จรับเงินหากฉันใช้บาร์โค้ดของรายการเป็นคีย์
ข้อสังเกต
โปรดเข้าใจว่านี่คือการคัดลอกวางรหัสของฉันที่ฉันกำลังดำเนินการอยู่ในขณะที่เขียนนี้ ใช้เป็นเพียงตัวอย่างเท่านั้นการคัดลอกวางแบบสุ่มจะไม่ช่วยคุณ แก้ไขสิ่งนี้ก่อนตามความต้องการของคุณ
นอกจากนี้โปรดอย่าลืมอ่านความคิดเห็นในโค้ด
รหัส
ใช้วิธีนี้เป็นวิธีการในชั้นเรียนของคุณเพื่อตรวจสอบที่ 1 ว่าคอลัมน์ที่คุณต้องการเพิ่มหายไปหรือไม่ เราทำสิ่งนี้เพื่อไม่ให้ทำซ้ำขั้นตอนการแก้ไขรหัสใบเสร็จรับเงินตาราง เพียงระบุว่าเป็นส่วนหนึ่งของชั้นเรียนของคุณ ในขั้นตอนต่อไปคุณจะเห็นว่าเราจะใช้มันอย่างไร
public boolean is_column_exists(SQLiteDatabase mDatabase , String table_name,
String column_name) {
//checks if table_name has column_name
Cursor cursor = mDatabase.rawQuery("pragma table_info("+table_name+")",null);
while (cursor.moveToNext()){
if (cursor.getString(cursor.getColumnIndex("name")).equalsIgnoreCase(column_name)) return true;
}
return false;
}
จากนั้นรหัสต่อไปนี้จะใช้ในการสร้างรหัสใบเสร็จรับเงินในตารางหากยังไม่ออกสำหรับผู้ใช้แอปของคุณเป็นครั้งแรก และโปรดสังเกต "IF NOT EXISTS" ในรหัส มันมีความสำคัญ
//mDatabase should be defined as a Class member (global variable)
//for ease of access :
//SQLiteDatabse mDatabase=SQLiteDatabase.openOrCreateDatabase(dbfile_path, null);
creation_query = " CREATE TABLE if not exists receipt_barcode ( ";
creation_query += "\n record_id INTEGER PRIMARY KEY AUTOINCREMENT,";
creation_query += "\n rcpt_id INT( 11 ) NOT NULL,";
creation_query += "\n barcode VARCHAR( 255 ) NOT NULL ,";
creation_query += "\n barcode_price VARCHAR( 255 ) DEFAULT (0),";
creation_query += "\n PRIMARY KEY ( record_id ) );";
mDatabase.execSQL(creation_query);
//This is where the important part comes in regarding the question in this page:
//adding the missing primary key record_id in table receipt_barcode for older versions
if (!is_column_exists(mDatabase, "receipt_barcode","record_id")){
mDatabase.beginTransaction();
try{
Log.e("record_id", "creating");
creation_query="CREATE TEMPORARY TABLE t1_backup(";
creation_query+="record_id INTEGER PRIMARY KEY AUTOINCREMENT,";
creation_query+="rcpt_id INT( 11 ) NOT NULL,";
creation_query+="barcode VARCHAR( 255 ) NOT NULL ,";
creation_query+="barcode_price VARCHAR( 255 ) NOT NULL DEFAULT (0) );";
mDatabase.execSQL(creation_query);
creation_query="INSERT INTO t1_backup(rcpt_id,barcode,barcode_price) SELECT rcpt_id,barcode,barcode_price FROM receipt_barcode;";
mDatabase.execSQL(creation_query);
creation_query="DROP TABLE receipt_barcode;";
mDatabase.execSQL(creation_query);
creation_query="CREATE TABLE receipt_barcode (";
creation_query+="record_id INTEGER PRIMARY KEY AUTOINCREMENT,";
creation_query+="rcpt_id INT( 11 ) NOT NULL,";
creation_query+="barcode VARCHAR( 255 ) NOT NULL ,";
creation_query+="barcode_price VARCHAR( 255 ) NOT NULL DEFAULT (0) );";
mDatabase.execSQL(creation_query);
creation_query="INSERT INTO receipt_barcode(record_id,rcpt_id,barcode,barcode_price) SELECT record_id,rcpt_id,barcode,barcode_price FROM t1_backup;";
mDatabase.execSQL(creation_query);
creation_query="DROP TABLE t1_backup;";
mDatabase.execSQL(creation_query);
mdb.setTransactionSuccessful();
} catch (Exception exception ){
Log.e("table receipt_bracode", "Table receipt_barcode did not get a primary key (record_id");
exception.printStackTrace();
} finally {
mDatabase.endTransaction();
}