数据库初始化

u010269523 2013-04-16 11:06:03
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;

}


为什么一个初始化的代码要那么长? 哪些是可以省略的?
...全文
326 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wlcw16 2013-04-17
  • 打赏
  • 举报
回复
刚才那个有点问题 有个右括号写错位置了。


    public SQLiteDatabase initdatabase() throws Exception {
        if (!new File(databasepath).exists()) {

            new File(databasepath).mkdir();
        }
        File databasefile = new File(databasepath + databasename);
        if (!databasefile.exists()) {
            System.out.println(databasepath);
            databasefile.createNewFile();
            InputStream inputStream = null;
            FileOutputStream outputStream = null;
            inputStream = activity.getResources().getAssets()
                    .open(databasename);
            outputStream = new FileOutputStream(databasepath + databasename);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
                buffer = new byte[1024];// 一次写入128B的数据
                System.out.println("write over!");
            }
            // Close the streams
            outputStream.flush();
            outputStream.close();
            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;

    }
代码大概的意思是 先看看系统有没有database的路径,没有就生成一个。 再看database存在不存在 不存在的话就用assets里的资源文件生成一个database存放在系统里。 最后返回一个数据库实例。
csdn_2013 2013-04-17
  • 打赏
  • 举报
回复
引用 5 楼 u010269523 的回复:
引用 4 楼 wlcw16 的回复: 引用 2 楼 u010269523 的回复:引用 1 楼 wlcw16 的回复:public SQLiteDatabase initdatabase() throw Exception 然后把trycatch删掉。 能给我一个删除之后的代码吗?谢谢啦 Java code?12345678910111213141516171819202122232……
throws Exception 添加到方法名后面 就是说这个方法在执行出现异常情况下 直接抛出异常信息 不去try catch自己手动增加代码捕获 至于初始化数据库的代码 先是判别当前数据库是否存在,如果不存在 那么会将源码的根路径下本地的assets包下数据 重新用来创建数据库
wlcw16 2013-04-17
  • 打赏
  • 举报
回复
这是java的机制,对异常的处理。 数据库初始化的时候会抛出很多异常,比如写入读取的时候会有IOException,创建文件的时候会有FileNotFoundException等等,你的程序把它们都catch住,并将错误信息输出到堆栈。改过之后抛出他们的父类Exception,将错误问题往上抛,留给调用initdatabase()方法的程序进行处理。 推荐你一边学android,一般在看看java的机制,有助于技术的增长。
u010269523 2013-04-17
  • 打赏
  • 举报
回复
引用 4 楼 wlcw16 的回复:
引用 2 楼 u010269523 的回复:引用 1 楼 wlcw16 的回复:public SQLiteDatabase initdatabase() throw Exception 然后把trycatch删掉。 能给我一个删除之后的代码吗?谢谢啦 Java code?123456789101112131415161718192021222324252627……
throws Exception 这2个是干嘛用的? 我对这个初始化不怎么懂 你能帮我大致解释下吗? 谢谢啦
wlcw16 2013-04-17
  • 打赏
  • 举报
回复
引用 2 楼 u010269523 的回复:
引用 1 楼 wlcw16 的回复:public SQLiteDatabase initdatabase() throw Exception 然后把trycatch删掉。 能给我一个删除之后的代码吗?谢谢啦

public SQLiteDatabase initdatabase() throws Exception {
        if (!new File(databasepath).exists()) {

            new File(databasepath).mkdir();
        }
        File databasefile = new File(databasepath + databasename);
        if (!databasefile.exists()) {
            System.out.println(databasepath);
            databasefile.createNewFile();
            InputStream inputStream = null;
            FileOutputStream outputStream = null;
            inputStream = activity.getResources().getAssets()
                    .open(databasename);
            outputStream = new FileOutputStream(databasepath + databasename);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inputStream.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
                buffer = new byte[1024];// 一次写入128B的数据
                System.out.println("write over!");
            }
            // Close the streams
            outputStream.flush();
            outputStream.close();
            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;
        }
    }
u010269523 2013-04-17
  • 打赏
  • 举报
回复
不掉沉啊
u010269523 2013-04-16
  • 打赏
  • 举报
回复
引用 1 楼 wlcw16 的回复:
public SQLiteDatabase initdatabase() throw Exception 然后把trycatch删掉。
能给我一个删除之后的代码吗?谢谢啦
wlcw16 2013-04-16
  • 打赏
  • 举报
回复
public SQLiteDatabase initdatabase() throw Exception 然后把trycatch删掉。

80,362

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧