数据库初始化

u010269523 u010269523 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
public SQLiteDatabase initdatabase() {
// TODO Auto-generated method stub
if (!new File(databasepath).exists()) {

new File(databasepath).mkdir();
}

File databasefile = new File(databasepath + databasename);
if (!databasefile.exists()) {

try {
System.out.println(databasepath);
databasefile.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
InputStream inputStream = null;
FileOutputStream outputStream = null;
try {
inputStream = activity.getResources().getAssets()
.open(databasename);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
outputStream = new FileOutputStream(databasepath + databasename);
} catch (FileNotFoundException e) {
e.printStackTrace();
}

byte[] buffer = new byte[1024];
int length;
try {
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
buffer = new byte[1024];// 一次写入128B的数据
System.out.println("write over!");
}
} catch (IOException e) {
e.printStackTrace();
}
// Close the streams
try {
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
sqLiteDatabase = null;
sqLiteDatabase = SQLiteDatabase.openDatabase(databasepath
+ databasename, null, SQLiteDatabase.OPEN_READWRITE);
Toast.makeText(activity, "位于本地的数据库初始化完成!", Toast.LENGTH_LONG)
.show();

}
sqLiteDatabase = SQLiteDatabase.openDatabase(databasepath
+ databasename, null, SQLiteDatabase.OPEN_READWRITE);

return sqLiteDatabase;

}


为什么一个初始化的代码要那么长? 哪些是可以省略的?
...全文

相关推荐

评论 8
收藏 已收藏
分享
返回顶部
u010269523
Android 80,488 成员 91,371 内容