0

我正在将资产文件夹中的现有数据库复制到设备本地数据库中。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 ;

谢谢您的帮助。

4

0 回答 0