创建Android应用程序与数据库的数据量较大 [英] Creating android app Database with big amount of data

查看:100
本文介绍了创建Android应用程序与数据库的数据量较大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的数据库,需要填充大量的数据, 所以在的onCreate(),它不仅是一些创建表的SQL 说明,有很多刀片。我选择的解决方法是 存储在位于RES /生,其中一个SQL文件这一切说明 装有 Resources.openRawResource(ID)

The database of my application need to be filled with a lot of data, so during onCreate(), it's not only some create table sql instructions, there is a lot of inserts. The solution I chose is to store all this instructions in a sql file located in res/raw and which is loaded with Resources.openRawResource(id).

它运作良好,但我面对的编码问题,我有些突出 caharacters在我的应用程序出现不好的SQL文件。本 我的code要做到这一点:

It works well but I face to encoding issue, I have some accentuated caharacters in the sql file which appears bad in my application. This my code to do this:

public String getFileContent(Resources resources, int rawId) throws
IOException
  {
    InputStream is = resources.openRawResource(rawId);
    int size = is.available();
    // Read the entire asset into a local byte buffer.
    byte[] buffer = new byte[size];
    is.read(buffer);
    is.close();
    // Convert the buffer into a string.
    return new String(buffer);
  }

public void onCreate(SQLiteDatabase db) {
   try {
        // get file content
        String sqlCode = getFileContent(mCtx.getResources(), R.raw.db_create);
        // execute code
        for (String sqlStatements : sqlCode.split(";"))
        {
            db.execSQL(sqlStatements);
        }

        Log.v("Creating database done.");
        } catch (IOException e) {
            // Should never happen!
            Log.e("Error reading sql file " + e.getMessage(), e);
            throw new RuntimeException(e);
        } catch (SQLException e) {
            Log.e("Error executing sql code " + e.getMessage(), e);
            throw new RuntimeException(e);
        }

我发现避免这个问题的解决方案是加载SQL指令 从一个巨大的静态最后弦乐的所有,而不是一个文件, 突出的字符出现很好。

The solution I found to avoid this is to load the sql instructions from a huge static final String instead of a file, and all accentuated characters appear well.

不过,是不是有一个更优雅的方式来加载SQL指令比一个大 静态最后弦乐属性与所有SQL指令?

But isn't there a more elegant way to load sql instructions than a big static final String attribute with all sql instructions?

推荐答案

我觉得你的问题是在这条线:

I think your problem is in this line:

return new String(buffer);

您要转换的字节数组中的一个 java.lang.String中但你不能告诉的Java / Android的使用的编码。所以字节的重音字符不被转换为正确的错误的编码被使用。

You're converting the array of bytes in to a java.lang.String but you're not telling Java/Android the encoding to use. So the bytes for your accented characters aren't being converted correctly as the wrong encoding is being used.

如果您使用的<一个href="http://developer.android.com/reference/java/lang/String.html#String%28byte%5B%5D,%20java.lang.String%29"相对=nofollow> 字符串(字节[],&LT;编码&GT;) 构造函数,你可以指定文件的编码和你的人物会被正确转换

If you use the String(byte[],<encoding>) constructor you can specify the encoding your file has and your characters will be converted correctly.

这篇关于创建Android应用程序与数据库的数据量较大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆