数据库初始化
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;
}
为什么一个初始化的代码要那么长? 哪些是可以省略的?
// 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;
}
为什么一个初始化的代码要那么长? 哪些是可以省略的?
...全文
相关推荐
ORACLE_RAC数据库中的初始化参数的设置
ORACLE_RAC数据库中的初始化参数的设置
TIA博途学习 | 优化的DB块中添加变量时不想初始化DB块怎么做?.docx
TIA博途学习 | 优化的DB块中添加变量时不想初始化DB块怎么做?
PB环境下SQL Server数据库的初始化、备份与恢复方法探讨.pdf
PB环境下SQL Server数据库的初始化、备份与恢复方法探讨.pdf
创建并初始化orcale数据库,是创建并初始化orcale数据库的基础教程,值得存档。
创建并初始化orcale数据库,是创建并初始化orcale数据库的基础教程,值得存档。
达梦数据库纯命令行创建实例并初始化数据库
使用达梦数据库的图形界面工具创建实例非常方便,但是实际工作中,部署数据库时往往不一定有图形界面的条件。 其实,达梦同样有一款使用命令行创建实例的工具,在 $DM_HOME/bin 下,有一款 dminit 工具。 只要指定实例的创建路径和数据库名称参数就可以快速创建实例,例如: [dmdba@dm ~]$ dminit PATH=/dm/dmdbms/data DB_NAME=DAMENG INSTANCE_NAME=DMSERVER 不过此时实例配置都是默认安装的,不建议此种方式。 建议安装时不带参数并在安装过程中逐步配置需要的实例参数,例如: [dmdba@dm ~]$ dminit

