检查数据库是否存在

时间:2013-08-22 22:28:44

标签: java android database sqlite

如果数据库不存在,我想将数据库从txt写入数据库:

File database=getApplicationContext().getDatabasePath("commments.db");

                if (!database.exists()) {
                    // Database does not exist so copy it from assets here
                    try {
                        txtToDb();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    Log.i("Database", "Not Found");
                } else {
                    Log.i("Database", "Found");
                }

它不起作用,数据库被反复填充。我做错了什么?

更新: txtToDb():

public void txtToDb () throws IOException {

        InputStream is =getResources().openRawResource(R.raw.data);
        BufferedInputStream bis = new BufferedInputStream(is);

        ByteArrayBuffer baf = new ByteArrayBuffer(50);

        int current = 0;

        while ((current = bis.read()) != -1) {

            baf.append((byte) current);

        }

        byte[] myData = baf.toByteArray();
        String dataInString = new String(myData);
        String[] lines = dataInString.split("\n");

        for (int i=0; i<lines.length; i++){
            comment = datasource.createComment(lines[i]);
            // adapter.add(comment);
        }

        writeDbToSd();

    }

DbToSd()将DB写入SDCARD,以便我可以使用adb pull来查看它。这两个功能都可以自行运行。 createComment将解析后的字符串添加到另一个函数的数据库中。

0 个答案:

没有答案
相关问题