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

  • Home
  • SEARCH
  • 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 6547903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:53:14+00:00 2026-05-25T11:53:14+00:00

In the first version of my database I had three rows. The following methods

  • 0

In the first version of my database I had three rows. The following methods are in a class that extends SQLiteOpenHelper.

@Override
public void onCreate(SQLiteDatabase database) {
    database.execSQL("CREATE TABLE " + DATABASENAME + " (name TEXT, latitude REAL, longitude REAL);");
}

Now, I have changed the version number from 1 to 2, and added a new row:

@Override
public void onCreate(SQLiteDatabase database) {
    database.execSQL("CREATE TABLE " + DATABASENAME + " (name TEXT, latitude REAL, longitude REAL, country TEXT);");
}

I don’t care that the old database is lost, so I use this on upgrade:

@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
    db.delete(DATABASENAME, null, null);
    onCreate(db);
}

Now when I call the method getReadableDatabase(), the app crashes, with the logcat error

Caused by: android.database.sqlite.SQLiteException: Can’t upgrade
read-only database from version 1 to 2:
/data/data/com.myapp.app/databases/mydb

How can I prevent this?

EDIT

The full class:

public class StationsOpenHelper extends SQLiteOpenHelper {

    private static final String DATABASENAME = "stations";
    private static final int DATABASEVERSION = 2;

    public StationsOpenHelper(Context context) {
        super(context, DATABASENAME, null, DATABASEVERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL("CREATE TABLE " + DATABASENAME + " (name TEXT, latitude REAL, longitude REAL, country TEXT);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
        db.delete(DATABASENAME, null, null);
        onCreate(db);
    }

    public ArrayList<Station> getStations() {
        Logger.log("Trying to read stations from database");
        ArrayList<Station> result = new ArrayList<Station>();

        SQLiteDatabase database = getReadableDatabase();
        Cursor cursor = database.rawQuery("SELECT * FROM " + DATABASENAME, null);

        String name, country;
        double lat, lon;
        Station s;
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            s = new Station();
            name = cursor.getString(0);
            lat = cursor.getDouble(1);
            lon = cursor.getDouble(2);
            country = cursor.getString(3);

            s.setName(name);
            s.setLatitude(lat);
            s.setLongitude(lon);
            s.setCountry(country);

            result.add(s);

            cursor.moveToNext();
        }
        cursor.close();
        database.close();

        if (result.isEmpty()) {
            Logger.log("Reading stations from database failed");
            result = null;
        } else {
            Logger.log("Reading stations from database succeeded");
        }

        return result;
    }

    public void storeStations(ArrayList<Station> stations) {
        Logger.log("Storing stations in database");
        ContentValues values = new ContentValues();
        SQLiteDatabase db = getWritableDatabase();
        db.delete(DATABASENAME, null, null);

        for (int i = 0; i < stations.size(); i++) {
            values.put("name", stations.get(i).getName());
            values.put("latitude", stations.get(i).getLatitude());
            values.put("longitude", stations.get(i).getLongitude());
            values.put("country", stations.get(i).getCountry());
            db.insert(DATABASENAME, null, values);
        }

        db.close();
        Logger.log("Storing stations in database succeeded");
    }
}
  • 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-25T11:53:14+00:00Added an answer on May 25, 2026 at 11:53 am

    Ok, here’s the solution I propose, replace db.delete with:

    db.execSQL("DROP TABLE IF EXISTS " + DATABASENAME);
    

    I have a strange feeling that will work 😉

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

Sidebar

Related Questions

Here is the example with comments: class Program { // first version of structure
I've got a UITableView with custom tablecells. First I had a version which implemented
Let's assume that you are working at the first version of a new Django
I have an SQLite database in the first version of my iPhone application (which
Experienced with Rails / ActiveRecord 2.1.1 You create a first version with (for example)
First off, let me define the end goal: I'd like to Wordpress (version 2.8)
I've finally completed a working version of my first ever CSS-supported site (thanks to
I am about to release my first app to the iTunes store. What version
First off, I understand the reasons why an interface or abstract class (in the
UPDATE: I have determined that the SQLClient code times out on the first call,

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.