SQLite DB中更新的行数错误

时间:2016-04-30 09:37:02

标签: java sql sqlite

我试图在SQLite DB中随机更改一定数量的行。问题是更改的行数总是大于预期的行数。有人可以指出我的错误吗?

" probM"和" probF"应该是行更改的概率。例如,当probM设置为5时,countM上升到大约120.但是不是更新了120行,而是970。

" DBUpdater.count"返回给定列和WHERE语句的行数。

public static void verwitwetStmt(int probM, int probF) {
    String select;
    String preparedStmt;
    Random r = new Random();
    int rnd;
    int countW = 0;
    int countM = 0;

    int anzahl = DBUpdater.count("Jahre", "Jahre BETWEEN 66 AND 70 AND Geschlecht IS 0");

    for (int i = 0; i < anzahl; i++) {
        rnd = r.nextInt(100);
        if (rnd <= probM)
            countM++;
        }

    System.out.println(anzahl + ", " +countM);

    select = "SELECT ID FROM individuen WHERE Jahre BETWEEN 66 AND 70 AND Familienstand IS NULL "
            + "AND Geschlecht IS 0 ORDER BY RANDOM() LIMIT " + Integer.toString(countM);
    preparedStmt = "UPDATE individuen SET Familienstand = ? WHERE ID = ?";

    DBUpdater.blankUpdate(select, preparedStmt, 3);

    anzahl = DBUpdater.count("Jahre", "Jahre BETWEEN 66 AND 70 AND Geschlecht IS 1");

    for (int i = 0; i < anzahl; i++) {
        rnd = r.nextInt(100);
        if (rnd <= probF)
            countW++;
        }

    select = "SELECT ID FROM individuen WHERE Jahre BETWEEN 66 AND 70 AND Familienstand IS NULL "
            + "AND Geschlecht IS 1 ORDER BY RANDOM() LIMIT " + Integer.toString(countW);
    preparedStmt = "UPDATE individuen SET Familienstand = ? WHERE ID = ?";

    DBUpdater.blankUpdate(select, preparedStmt, 3);
}

以下是blankUpdate方法:

public static void blankUpdate(String selectQuery, String preparedStatement, int wert) {
    DBController dbc = DBController.getInstance();
    dbc.initDBConnection();
    try {
        Statement stmt = DBController.connection.createStatement();
        ResultSet rs = stmt.executeQuery(selectQuery);
        PreparedStatement ps = DBController.connection.prepareStatement(preparedStatement);

        while (rs.next()) {
            ps.setInt(1, wert);
            ps.setInt(2, rs.getInt(1));
            ps.addBatch();
        }
        DBController.connection.setAutoCommit(false);
        ps.executeBatch();
        DBController.connection.setAutoCommit(true);
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

这里的计数方法:

public static int count(String zeile, String whereStmt) {
    DBController dbc = DBController.getInstance();
    dbc.initDBConnection();
    int anzahl = 0;
    try {
        Statement stmt = DBController.connection.createStatement();
        String select = "SELECT COUNT(" + zeile + ") FROM individuen WHERE " + whereStmt + ";";
        ResultSet rs = stmt.executeQuery(select);

        select = rs.getString(1);
        anzahl = Integer.parseInt(select);
        rs.close();
        stmt.close();

    } catch (SQLException e) {
        e.printStackTrace();
    }
    return anzahl;
}

示例:66至70岁之间的男性个体:2105。

该组的每个男性的概率:5%。

循环后的CountM:130。(~6%)

执行代码后,以下SELECT语句返回935行。

SELECT Familienstand 
FROM individuen 
WHERE Familienstand IS 3 
  AND Geschlecht IS 0 
  AND Jahre BETWEEN 66 AND 70

0 个答案:

没有答案