Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wrote this one especially for you &lt;3</p> <p>I used the same filename as you <strong>"/raw/food_db.sql"</strong> but that <strong>lead to Errors</strong> instead I had to call it <strong>"/raw/food_db"</strong>. 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. </p> <p>There is a method for within your DbSource... assuming somewhere there is code like this:</p> <pre><code>private SQLiteDatabase db; ... DbHelper dbHelper = new DbHelper(context); this.db = dbHelper.getWritableDatabase(); </code></pre> <p>You put this method in there:</p> <pre><code>/** * 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; } </code></pre> <p>Call it like this (I tried from an Activity, so that Toasts can output messages). Look closely, the errors are "Toasted" as well.</p> <pre><code>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(); } </code></pre> <p>Enjoy!</p> <p>Oh.. btw: This code is meant for a file in which each insert-Statement has its own line.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload