ธุรกรรมฐานข้อมูล Android
ฉันได้สร้างฐานข้อมูลแล้ว ฉันต้องการทำรายการ SaveCustomer()มีคำสั่งมากกว่าหนึ่งรายการเพื่อแทรกระเบียนลงในCustomer, CustomerControl, Profile, Paymentตารางในขณะนั้น เมื่อผู้ใช้เรียกใช้ SaveCustomer()method ข้อมูลนั้นจะไปที่ 4 ตารางเหล่านี้ดังนั้นฉันจะทำธุรกรรมได้อย่างไร? หากการแทรกตารางล้มเหลวจำเป็นต้องย้อนกลับทุกอย่าง ตัวอย่างเช่นเมื่อตารางที่ 3 แทรกระเบียนฉันได้รับข้อผิดพลาดจากนั้นจำเป็นต้องย้อนกลับระเบียนแทรกของตารางสองรายการก่อนหน้านี้ด้วย ดูรหัสของฉัน: public void saveCustomer(){ DBAdapter dbAdapter = DBAdapter.getDBAdapterInstance(RetailerOrderKeyActivity.this); dbAdapter.openDataBase(); ContentValues initialValues = new ContentValues(); initialValues.put("CustomerName",customer.getName()); initialValues.put("Address",customer.getAddress()); initialValues.put("CustomerPID",strPID); initialValues.put("Date",strDateOnly); long n = dbAdapter.insertRecordsInDB("Customer", null, initialValues); } ในทำนองเดียวกันคำสั่งอื่น ๆ ก็มีเช่นกัน รหัส DBAdpter คือ: public long insertRecordsInDB(String tableName, …