Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6830241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:35:41+00:00 2026-05-26T22:35:41+00:00

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

  • 0

i have “food_db.sql” file stored in /res/raw folder,
it has tons of ‘insert’ in it.

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);
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:35:41+00:00Added an answer on May 26, 2026 at 10:35 pm

    I wrote this one especially for you <3

    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.

    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();
    

    You put this method in there:

    /**
     * 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;
    }
    

    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();
        }
    

    Enjoy!

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If we already have a database file in .sql format, how can we load
I have an xml file in the following format: <food> <desert> cake <desert> </food>
I have several keywords (1~3 words) stored in <MTEntryKeywords> field separated by commas. I
I have two tables in my database - Category and Food. Food has idCategory.
i have a xml file on a server that look for example like this
I have a basic question that I can not understand yet from ruby. If
We have XML code stored in a single relational database field to get around
I have a SQL 2005 instance that runs a job that uses a Powershell
I have an object that represents a food item to order at a restaurant.
I have the following abstract Django models: class Food(models.Model): name = models.CharField(max_length=100) class Meta:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.