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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:21:30+00:00 2026-05-30T14:21:30+00:00

My application has a SQLite database to contain a list of highways. It shows

  • 0

My application has a SQLite database to contain a list of highways. It shows them all in a list. It first tries to query the database for the roads. If the query doesn’t return any, it calls another method that downloads a list from a remote server, and populates the database. After that returns, it immediately queries the database again.

That’s how it should work. How it actually works is, the first query always returns nothing. It goes straight to downloading a fresh list, it inserts the list into the database, and queries again. The second query always returns the correct result. The strange thing is, I can repeat the operation without even exiting the application. Using the adb shell, I can read the SQLite3 database on the emulator. The data shown in the database is exactly as expected. But the application is behaving as though the data isn’t there? Is there some behaviour I’m not aware of? Here’s the code.

RoadsDataSource.java

public class RoadsDataSource {

    private DataStorage data;

    private static Context context;

    public RoadsDataSource() {
        this.data = new DataStorage(RoadsDataSource.context);
    }

    private List<Road> getRoads(Integer state) {
        List<Road> roads = loadRoadsFromDb(state);
        if (roads.isEmpty()) {
            Request api = new Request(RoadsDataSource.context);
            Roads apiRoads = api.fetchRoads(state);
            this.data.storeRoads(apiRoads);
            roads = loadRoadsFromDb(state);
        }
        return roads;
    }

    private List<Road> loadRoadsFromDb(Integer state) {
        SQLiteQueryBuilder query = new SQLiteQueryBuilder();
        query.setTables(Queries.ROAD_STATE_MATCHES);
        Cursor results = query.query(
                this.data.getWritableDatabase(),
                new String[] {Tables.ROADS + "." + Tables.Roads.ID, Tables.ROADS + "." + Tables.Roads.TYPE, Tables.ROADS + "." + Tables.Roads.NUMBER}, 
                Queries.ROADS_BY_STATE,
                new String[] {state.toString()}, null, null, null 
        );

        List<Road> roads = new ArrayList<Road>();

        results.moveToFirst();
        while (!results.isAfterLast()) {
            roads.add(new Road(results.getInt(0), results.getString(1), results.getInt(2)));
            results.moveToNext();
        }
        results.close();

        System.out.println(roads.size());
        return roads;
    }
}

DataStorage.java

public class DataStorage extends SQLiteOpenHelper {
    public void storeRoads(Roads roads) {
        SQLiteDatabase db = this.getWritableDatabase();
        for (Road road : roads.getRoads()) {
            ContentValues roadRow = new ContentValues();
            roadRow.put(Tables.Roads.ID, road.getId());
            roadRow.put(Tables.Roads.TYPE, road.getType());
            roadRow.put(Tables.Roads.NUMBER, road.getNumber());
            try {
                db.insertOrThrow(Tables.ROADS, null, roadRow);
            } catch (SQLException e) {
            }
            ContentValues linkRow = new ContentValues();
            linkRow.put(Tables.StatesRoads.STATE_ID, roads.getState());
            linkRow.put(Tables.StatesRoads.ROAD_ID, road.getId());
            try {
                db.insertOrThrow(Tables.STATES_ROADS, null, linkRow);
            } catch (SQLException e) {
            }
        }
    }
}
  • 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-30T14:21:32+00:00Added an answer on May 30, 2026 at 2:21 pm

    I’ve had huge issues in the past with not closing the database handle – generally I do all db operations inside an SQLiteOpenHelper subclass, keeping a reference to the db, opening and closing it atomically.

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

Sidebar

Related Questions

I have a Python application. It has an SQLite database, full of data about
All, I have a WPF application built upon SQLite database and user/application Configuration settings.
Say my application has a ListView that displays information from a Sqlite database, using
An Android application has two activities. Activity A shows a list of items. Activity
I have an SQLite database in the first version of my iPhone application (which
is it possible to use SQLite for a j2me application ? has anyone ever
Application has an auxiliary thread. This thread is not meant to run all the
I have written an Android application that uses SQLite database which is saved in
I am trying to connect my application to a SQLite database with LINQ-to-SQL, and
I created a C++ application that updates a SQLite database. The application reads a

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.