1

I created a system in which I can run all my postgre sql queries with only one single Async Task Class in Android Studio. This was really(!!) challenging due to the big amount of limitations that I had to face. But this works actually really great!

//Used for connecting to database and executing queries.
//Index 0 of input string must be the query, Index 1 must be the tablename we demand
//We can only gather data from 1 table for each query, so if you need data from several tablecolumns, use multiple queries like:
//[0] = query, [1] = tablename, [2] = 2nd query, [3] = 2nd tablename, [4] = 3rd query, [5] = 3rd table name ... and so on (each query must come with a tablename)
public class DBHandler extends AsyncTask<String, Void, List<String>>
{
public AsyncResponse delegate;

@Override
protected List<String> doInBackground(String...query)
{
    List<String> result = new ArrayList<String>();
    String sql;
    String tableresult = null;
    Connection conn = null;
    Statement st = null;
    ResultSet rs = null;

    try {
        Class.forName("org.postgresql.Driver");
        conn = DriverManager.getConnection("jdbc:postgresql://192.168.200.300:5439/dbname?user=anonymous&password=secretpw");
        st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE); //necessary if you want to use rs.first() after rs.next(), it makes the resultset scrollable

        for (int i = 0; i <= query.length-1; i = i+2) //queries are always stored in i=0 and/or in i+2, because i+1 contain the demanded tablenames for resultset handling
        {
            System.out.println("I is: " +i);
            if (!query[i].isEmpty())
            {
                System.out.println(query[i]);
                sql = query[i];
                rs = st.executeQuery(sql);

                while (rs.next())
                    if (!query[i + 1].isEmpty() || !rs.getString(query[i + 1]).isEmpty()) //if i+1 is empty, there is no demanded tablename. Used when we dont need any return values (ie. INSERT, UPDATE)
                    result.add(rs.getString(query[i + 1])); //demanded tablename is always stored in i+1

                //We add an empty entry if we demand multiple tablenames so we can keep them seperate
                //Might be replaced with any other char, but you will have to backtrack all usages of DBHandler and fix the filters there
                if(i+2 < query.length)
                result.add(" ");
            }
            rs.first(); //reset pointer for rs.next()
        }

        rs.close();
        st.close();
        conn.close();
        System.out.println("End of AsyncTask");
    }

    catch (SQLException ex)
    {
        ex.printStackTrace();
    }

    catch (Exception e)
    {
        e.printStackTrace();
    }
    return result;
}

//onPostExecute returns query result in a List.
//We need to use interaces delegate feature to send the result to other classes, like "Auslieferung", which is implementing the interface
@Override
protected void onPostExecute(List<String> result)
{
    super.onPostExecute(result);
    System.out.println("Result: " +result.toString());
    if (!result.isEmpty())
        delegate.processFinish(result);
}
}

There is a for-loop in this Async Task.

for (int i = 0; i <= query.length-1; i = i+2)

And now finally I can explain my issue: I usually use SELECT queries, sometimes I use an INSERT query (which can be done by a single query), but when I parse an Update Query, my for-loop stops iterating after the first pass, so i+2 never happens. The update queries look like this:

String updatequeries[] = {UPDATE delivery SET contactperson = 'Jon Doe' WHERE officeid = 5, " ", UPDATE delivery SET contactemail = 'abd@def.gh' WHERE officeid = 5, " "};

Why does this for loop stop running right after the first run? The debugger does not show anything unusual, everything was parsed right and there are no queries missing. Updating a table does not return any results, but nothing depends on result values here. I tried to run 20 update queries in a single string var, but the for loop stops after the first iteration anyway. No issues are displayed in the debugger or in the logs. Have I overseen something or is there anything I don't know? Might this be a bug? Please help me! This issue drives me crazy.

Sawk Medic
  • 23
  • 3
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Yoshikage Kira Sep 23 '19 at 16:53
  • no exception? not sure what happens if you call `executeQuery` with an update, which does not return a single result set (Doc: "SQLException...the given SQL statement produces anything other than a single ResultSet object...") – user85421 Sep 23 '19 at 17:13
  • Nope, no exception as far as I have seen. I will post the logs when Im back in the office – Sawk Medic Sep 24 '19 at 11:47
  • @ Carlos Heuberger: Okay, I took an even closer look. There is an W/IInputConnectionWrapper: finishComposingText on inactive InputConnection W/System.err: org.postgresql.util.PSQLException: The query returns no result (<- translated by me) As this is an exception indeed, I don't see how it stops my for loop from iterating any further. @Goion: I thought your comment is one of those ad messages. Is there any special information that you want me to look at? Otherwise I can just tell you that I am already using a debugger – Sawk Medic Sep 26 '19 at 09:12

0 Answers0