我在我的android應用程序中使用兩個數據庫,假設A和B.I想將數據庫A的內容複製到數據庫B.如何實現?我嘗試像這樣。它不工作。請建議解決方案。如何將內容從一個數據庫複製到sqllite android中的另一個數據庫?
String alter_query3="insert into B.TableName1 select * from A.TableName2";
db.execSQL(alter_query3);
我在我的android應用程序中使用兩個數據庫,假設A和B.I想將數據庫A的內容複製到數據庫B.如何實現?我嘗試像這樣。它不工作。請建議解決方案。如何將內容從一個數據庫複製到sqllite android中的另一個數據庫?
String alter_query3="insert into B.TableName1 select * from A.TableName2";
db.execSQL(alter_query3);
第一個ATTACH
一個數據庫到另一個和然後使用INSERT INTO ... SELECT ...
語法:
-- working on database "A"
ATTACH '/path/to/other/database.db' AS B;
INSERT INTO TableName1 SELECT * FROM B.TableName2;
從數據庫中獲得的數據/內容,並製作一個列表/數組列表:
public ArrayList<Customer> getCustomerFromA() {
String sql = "SELECT * FROM " + DB_TABLE.CUSTOMER ;
ArrayList<Customer> customerList = new ArrayList<Customer>();
Cursor cursor = dbA.rawQuery(sql, null);
Customer customer = null;
if (cursor.moveToFirst()) {
customer = new Customer();
customer.setCode(cursor.getString(cursor.getColumnIndex(DB_TABLE.CUSTOMER_CODE)));
customer.setName(cursor.getString(cursor.getColumnIndex(DB_TABLE.CUSTOMER_NAME)));
customer.setAddress(cursor.getString(cursor.getColumnIndex(DB_TABLE.CUSTOMER_ADDRESS)));
customerList.add(customer);
}
cursor.close();
return customerList;
}
然後intert到數據庫B:
public void insertCustomerListToB(ArrayList<Customer> customerList) {
for (Customer customer : customerList) {
ContentValues cv = new ContentValues();
cv.put(DB_TABLE.CUSTOMER_CODE, customer.getCode());
cv.put(DB_TABLE.CUSTOMER_NAME, customer.getName());
cv.put(DB_TABLE.CUSTOMER_ADDRESS, customer.getAddress());
dbB.insert(DB_TABLE.CUSTOMER, null, cv);
}
}
客戶是實體類
嗯是的,據我所知,有可能通過這個代碼:
例子:
INSERT INTO db2.tbl SELECT * FROM db1.tbl
隨意解釋什麼是「不工作」意味着你。 – laalto 2014-10-30 06:49:41