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 7620433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:56:12+00:00 2026-05-31T03:56:12+00:00

I have my code below. It correctly reads my sqlite database file(that i have

  • 0

I have my code below. It correctly reads my sqlite database file(that i have already created using the SQLite Database Browser) in my assets folder – moves it to the /data/data/packagename/databases/ path on my device then i am able to use a query and cursor to get my information and it works great. Code here:

public class DatabaseHelper extends SQLiteOpenHelper {

private Context myDbContext;
private static String dbName = "restaurant.db";
private static String outfilePath = "/data/data/dev.mypackage.com/databases/";
private static String path = outfilePath + dbName;
private static SQLiteDatabase db;

public DatabaseHelper(Context context){
    super(context, dbName, null, 2);
    this.myDbContext = context;
    db = openDb();
    String s = "select * from menu";
    Cursor c = db.rawQuery(s, null);
    Log.e("DB Constructor Row count", String.valueOf(c.getCount()).toString());

    while(c.moveToNext()){
        Log.e("DB Constructor", c.getString(c.getColumnIndex("category")));
        Log.e("DB Constructor", c.getString(c.getColumnIndex("menuItem_id")));
        Log.e("DB Constructor", c.getString(c.getColumnIndex("title")));
        Log.e("DB Constructor", c.getString(c.getColumnIndex("desc")));
        Log.e("DB Constructor", c.getString(c.getColumnIndex("price")));
        Log.e("DB Constructor", c.getString(c.getColumnIndex("icon")));
    }

    c.deactivate();
    c.close();
}

private void copyDataBase(File dbFile) throws IOException {
    try{
        InputStream dbStream = myDbContext.getAssets().open(dbName);
        OutputStream newDbFile = new FileOutputStream(outfilePath + dbName);

        byte[] buffer = new byte[1024];
        int length;
        while((length = dbStream.read(buffer)) > 0){
            newDbFile.write(buffer);
        }

        newDbFile.flush();
        newDbFile.close();
        dbStream.close();
    }
    catch(IOException e){

        throw new IOException("trying to copy the database - ERROR: " + e.getMessage());
    }   

}

private SQLiteDatabase openDb() throws SQLiteException{
    File dbFile = myDbContext.getDatabasePath(dbName);

    if(!dbFile.exists()){
        Log.e("openDb", "file does not exist");

        try {
            copyDataBase(dbFile);
        } catch (IOException e) {
            throw new RuntimeException("Error creating source database", e);
        }
    }

    return SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);

}
public void loadRestaurantInfo(){

}

@Override
public void onCreate(SQLiteDatabase db){
    // TODO Auto-generated method stub      
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

Now i had to go back and add a row of information to one of the tables(in the database in the assets folder), using the SQLite Brower, – but that is not being reflected in my cursor output – I understand why this is happening – because if(!dbFile.exists()) fails so there is no copying to be done. So my question is – is what code do i need to add to make this work? I never created the tables with code so i dont see how useful the onUpgrade method is going to be for me.

  • 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-31T03:56:14+00:00Added an answer on May 31, 2026 at 3:56 am

    You have three options:

    1. Use onUpgrade() (preferred)

    2. Delete the existing database and copy the new one (not a good idea)

    3. Copy the data in the existing database, delete the existing database, copy the new database, insert data from old database into new database (too much work when the new database schema can be upgraded in onUpgrade).

    So, to answer your question, you upgrade your database in onUpgrade() without having to recreate any tables.

    On the other hand, if you just added a new row to a particular table, the database schema has not changed and you can just insert the new row at runtime… of course, not knowing what your application’s purpose is this may not be a good idea as you can easily lose track of changes to your “static” database.

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

Sidebar

Related Questions

I'm using the code below to read a text file that contains foreign characters,
I have the code below that hides and shows the navigational bar. It is
I have the code below: using (SqlCommand command = new SqlCommand()) { command.CommandType =
I have the code below: string SQL = select * from + TableName; using
I have two very similar pieces of ASP.NET code that send a file in
I have a part of a code that does the following: It reads in
Edit: original question below, but I revise it now that I have some code
I have a python psp page code is shown below. Currently it only prints
I have code below: <select id=testSelect> <option value=1>One</option> <option value=2>Two</option> </select> <asp:Button ID=btnTest runat=server
I have the code below : public class Anything { public int Data {

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.