我在我的android应用程序中使用两个数据库假设A和B.我想将数据库A的内容复制到数据库B.如何实现?我试着这样。它没有用。请建议解决方案。
String alter_query3="insert into B.TableName1 select * from A.TableName2";
db.execSQL(alter_query3);
答案 0 :(得分:1)
首先将ATTACH
个数据库添加到另一个数据库,然后使用INSERT INTO ... SELECT ...
语法:
-- working on database "A"
ATTACH '/path/to/other/database.db' AS B;
INSERT INTO TableName1 SELECT * FROM B.TableName2;
答案 1 :(得分:0)
从数据库A获取数据/内容并制作列表/ arraylist:
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;
}
然后进入数据库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);
}
}
客户是实体类
答案 2 :(得分:0)
嗯是的,据我所知,通过这段代码可以实现:
示例:
INSERT INTO db2.tbl SELECT * FROM db1.tbl