我正在将现有数据库从assets文件夹复制到设备本地数据库中。 db文件是test.sql,这是复制数据库代码:
private void copyDataBase() throws IOException {
InputStream mInput = mContext.getAssets().open("test.sql");
String outFileName = DB_PATH + DB_NAME;
OutputStream mOutput = new FileOutputStream(outFileName);
Log.i(TAG, outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer)) > 0) {
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
数据库已成功创建,但不会复制表。以下是test.sql文件的内容:
CREATE TABLE IF NOT EXISTS `test1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`namenew` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
INSERT INTO `test1` (`id`, `namenew`) VALUES
(1, 'fgfg'),
(2, 'fgf');
CREATE TABLE IF NOT EXISTS `test2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`testname` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
感谢您的帮助。