0

I have a strange issue that I cannot resolve for the life of me.

The sqlite database is simple. Two rows that are each String and one that is INTEGER.

enter image description here

The table only has a single record in:

enter image description here

   public PubList readDatabase(String currentPostcode) {
        databaseReader dbReader;
        dbReader = new databaseReader(this);

        try {
            dbReader.updateDataBase();
        } catch (IOException mIOException) {
            throw new Error("Unable to update database");
        }

        try {
            pubDatabase = dbReader.getReadableDatabase();
        } catch (SQLException mSQLException) {
            throw new Error("SQLException");
        }


        String query = "SELECT Town,PostcodeArea,NumberOfPubs FROM PubLists WHERE PostcodeArea = '" + currentPostcode.substring(0, 4) + "'";
        final Cursor cursor = pubDatabase.rawQuery(query, null);
        if (cursor.moveToNext()) {
            currentTown.setListName(cursor.getString(cursor.getColumnIndex("Town")));
            currentTown.setPostcode(cursor.getString(cursor.getColumnIndex("PostcodeArea")));
            currentTown.setNumberOfPubs(cursor.getInt(cursor.getColumnIndex("NumberOfPubs")));
        }
        cursor.close();
        return currentTown;
    }

With some debugging I can confirm both of the String columns are being pulled correctly, but the int column, which should be 23, ALWAYS returns 3 which I have no idea where the 3 is coming from.

Would love some pointers.

Edit: As asked, here is the code to upload a publist to the database. This code is ran from a javaFX application on a desktop.

public void uploadPubList(PubList pubList){
        String sql = "INSERT OR REPLACE INTO main.PubLists(Town,PostCodeArea,NumberOfPubs) VALUES(?,?,?)";
        try {
            Connection conn = this.connect();
            for (Pub pub : pubList.getPubs()) {
                PreparedStatement pstmt = conn.prepareStatement(sql);
                pstmt.setString(1, pubList.getName());
                pstmt.setString(2, pubList.getPostCodeArea());
                pstmt.setInt(3, pubList.getPubs().size());
                pstmt.executeUpdate();
                insertPub(pubList, pub);
            }

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }
5
  • Are you performing any operation in setNumberOfPubs? Commented May 2, 2019 at 11:57
  • did you debug and check if you get value in cursor? Commented May 2, 2019 at 11:59
  • set number of pubs is just a standard setter. Value in cursor is just 3 Commented May 2, 2019 at 12:17
  • Show me how are you inserting records in table ? Commented May 2, 2019 at 12:20
  • Posted the code in the OP. It's ran from a desktop application. By running this method and then looking at the Database in dataGrip I can confirm the value of 23 is correctly uploaded. Commented May 2, 2019 at 12:26

1 Answer 1

1

In uploadPubList() why are you iterating over pubList.getPubs()?
You want to insert only 1 row: the name of the town, the post code area and the number of pubs, right?
Also, this line:

insertPub(pubList, pub);

what does it do?
Remove the for loop and keep these:

public void uploadPubList(PubList pubList){
    String sql = "INSERT OR REPLACE INTO main.PubLists(Town,PostCodeArea,NumberOfPubs) VALUES(?,?,?)";
    try {
        Connection conn = this.connect();
        PreparedStatement pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, pubList.getName());
        pstmt.setString(2, pubList.getPostCodeArea());
        pstmt.setInt(3, pubList.getPubs().size());
        pstmt.executeUpdate();
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
}

Uninstall the app from the device so the database is deleted and rerun.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks forpas for taking a look we found the solution and it was a sillier error than 'missing a semi-colon bug'. Turns out the database we were looking at on dataGrip was not the same database the app was using. We were looking at a database with the same name we had used for testing earlier. Stupid mistake :') sorry for wasting your time!
Fine if it is solved. Also if this answer did not provide a solution you should not mark it as the right answer. But I'm still curious as why you have that loop in your code.
Looking at the code the pub list bits should be pulled out of the for loop, that is my own error. PubLists have a list of Pub objects that are to be uploaded to a separate table, hence looping through and running insertPub() on each one (which does the uploading).
Yes but this executes the sql insert statement many times: once for each pub. Use the loop only to for insertPub() then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.