android,如何在sqlitedatabase中执行一个sql文件 [英] android, how to exec a sql file in sqlitedatabase

查看:499
本文介绍了android,如何在sqlitedatabase中执行一个sql文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有food_db.sql文件存储在/ res / raw文件夹中,
有很多insert。

i have "food_db.sql" file stored in /res/raw folder, it has tons of 'insert' in it.

我的问题是我如何执行文件,并获得数据到我的Android应用程序中的sqlite数据库?

my question is how to i exec the file and get the data into sqlite databse in my android app?

这里是我的数据库代码。任何建议?

here is the database code of mine. any sugguestions?

private static class DbHelper extends SQLiteOpenHelper{

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_NAME + " TEXT NOT NULL, " + 
                KEY_HOTNESS + " TEXT NOT NULL);");
                    // how do i exec the sql file and get the data into this DB table?
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE);
        db.execSQL("DROP TABLE IF EXISTS" + RECORD_TABLE);
        onCreate(db);
    }

}


推荐答案

我写了一个特别为你< 3

I wrote this one especially for you <3

我使用与/ raw / food_db.sql导致错误,而是我不得不调用/ raw / food_db。我猜它是因为你不在你的代码中使用文件名,但ResourceIds写得像R.raw.food_db和点是混乱的系统。

I used the same filename as you "/raw/food_db.sql" but that lead to Errors instead I had to call it "/raw/food_db". I guess its because you don't use filenames in your code, but ResourceIds which are written like "R.raw.food_db" and the dot is confusing the system.

在你的DbSource ...中有一个方法假设某处有这样的代码:

There is a method for within your DbSource... assuming somewhere there is code like this:

private SQLiteDatabase db;
...
DbHelper dbHelper = new DbHelper(context);
this.db = dbHelper.getWritableDatabase();

您将此方法放在其中:

/**
 * This reads a file from the given Resource-Id and calls every line of it as a SQL-Statement
 * 
 * @param context
 *  
 * @param resourceId
 *  e.g. R.raw.food_db
 * 
 * @return Number of SQL-Statements run
 * @throws IOException
 */
public int insertFromFile(Context context, int resourceId) throws IOException {
    // Reseting Counter
    int result = 0;

    // Open the resource
    InputStream insertsStream = context.getResources().openRawResource(resourceId);
    BufferedReader insertReader = new BufferedReader(new InputStreamReader(insertsStream));

    // Iterate through lines (assuming each insert has its own line and theres no other stuff)
    while (insertReader.ready()) {
        String insertStmt = insertReader.readLine();
        db.execSQL(insertStmt);
        result++;
    }
    insertReader.close();

    // returning number of inserted rows
    return result;
}

这样调用它(我试过一个Activity,让Toasts可以输出消息)。仔细看,错误也是Toasted。

Call it like this (I tried from an Activity, so that Toasts can output messages). Look closely, the errors are "Toasted" as well.

try {
        int insertCount = database.insertFromFile(this, R.raw.food_db);
        Toast.makeText(this, "Rows loaded from file= " + insertCount, Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }

享受!

Oh .. btw:此代码适用于每个insert-Statement都有自己的行的文件。

Oh.. btw: This code is meant for a file in which each insert-Statement has its own line.

这篇关于android,如何在sqlitedatabase中执行一个sql文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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