为什么我的Android Studio中的for循环在第一次迭代后停止?

时间:2019-09-23 16:48:09

标签: java android-studio for-loop jdbc android-asynctask

我创建了一个系统,可以在Android Studio中仅使用一个异步任务类运行所有postgre sql查询。由于我必须面对大量的限制,这确实是一个挑战。但这实际上真的很棒!

//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);
}
}

此异步任务中有一个for循环。

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

现在我终于可以解释我的问题了: 我通常使用SELECT查询,有时我使用INSERT查询(可以通过单个查询完成),但是当我解析更新查询时,我的for循环在第一次通过后就停止迭代,因此i + 2永远不会发生。更新查询如下所示:

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

为什么此for循环在第一次运行后立即停止运行?调试器不会显示任何异常,所有内容都已正确解析,并且没有丢失任何查询。更新表不会返回任何结果,但是在此,任何内容都不取决于结果值。我试图在单个字符串var中运行20个更新查询,但是无论如何,for循环在第一次迭代后就停止了。在调试器或日志中未显示任何问题。我是否监督过某些事情,或者有什么我不知道的事情?可能是个错误吗?请帮我!这个问题使我发疯。

0 个答案:

没有答案