微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android预填充sqlite数据库

我的应用程序中有一个sqlite数据库.我的应用程序不应将其作为空数据库发送,但在用户第一次使用该应用程序之前,需要创建某些记录.当然我可以有很多插入语句,但这似乎很奇怪.我想到像http://vineetyadav.com/tutorials/11-adding-prefilled-sqlite-database-to-android-app.html这样的东西基本上可以工作,但这意味着我手动创建的数据库也需要所有内部表,如android_Metadata.所以我要么知道内部表存在什么以及如何填充它们.或者另一种拥有预填充数据库的智能方法.有什么建议?

谢谢.

解决方法:

您可以从SD卡读取一个平面文件,每行插入1个语句并遍历该文件.

这是一个类似的例子,我在其中读取文件并将ContactItem添加到我的contact_list:

public ArrayList<ContactItem> readInputFile(Context context, String filename) {
    String text = null;
    BufferedReader reader = null;
    ArrayList<ContactItem> contact_list = new ArrayList<ContactItem>();
    contact_list.clear();  // Clear any existing contacts when a new file is read
    try {
        reader = new BufferedReader(new FileReader(filename));
        // Repeat until EOF
        while (((text = reader.readLine()) != null)) {
            if (!(text.length() > 1024)) {  //  If we read more than 1k per line there's a problem with the input file format
                ContactItem c = new ContactItem(text);
                if (c.getIsValid()) {
                    //  We were able to parse a well formed phone number from the last line read
                    contact_list.add(c);
                }
            }
        }
    } catch (FileNotFoundException e) {
        Toast.makeText(context, R.string.error_fileNotFound, 1).show();
        e.printstacktrace();
    } catch (IOException e) {
        Toast.makeText(context, R.string.error_ioException, 1).show();
        e.printstacktrace();
    } finally { // EOFException (or other, but EOF is handled and most likely)
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            Toast.makeText(context, R.string.error_generalFailure, 1).show();
            e.printstacktrace();
        }
    }
    return contact_list;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐