我的应用程序的数据库需要填充大量数据,所以在onCreate()这期间,它不仅是一些create table sql指令,还有很多插入.我选择的解决方案是将所有这些指令存储在res/raw中的sql文件中并加载Resources.openRawResource(id).
它运作良好,但我面对编码问题,我在sql文件中有一些突出的caharacters,在我的应用程序中看起来很糟糕.这是我的代码:
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);
} …Run Code Online (Sandbox Code Playgroud)