简体   繁体   English

SQLITE_BUSY 数据库文件被锁定

[英]SQLITE_BUSY The database file is locked

I'm getting the error [SQLITE_BUSY] The database file is locked (database is locked) , even though I close my connection and the statements.我收到错误[SQLITE_BUSY] The database file is locked (database is locked) ,即使我关闭了连接和语句。 Not sure what else to try.不知道还有什么可以尝试的。

 public void getTeams() throws SQLException {
    Connection c = DriverManager.getConnection("jdbc:sqlite:leagueDatabase.db");
    Statement teamStmt = c.createStatement();
    ResultSet teamData = teamStmt.executeQuery("SELECT * FROM TEAMS");

    while (teamData.next()) {
        teams.add(new Team(teamData.getString("city"), teamData.getString("name")));
    }
    teamData.close();
    teamStmt.close();
    c.close();

    Connection c2 = DriverManager.getConnection("jdbc:sqlite:leagueDatabase.db");
    Statement teamStatsStmt = c2.createStatement();
    ResultSet teamStatsData = teamStatsStmt.executeQuery("SELECT * FROM TEAM_STATS");

    for (int i = 0; i < teams.size(); i++) {
        for (int j = 0; j < players.size(); j++) {
            if (players.get(j).getTeam().equalsIgnoreCase(teams.get(i).getName()))
                teams.get(i).addPlayer(players.get(j));
        }
        teams.get(i).setStats(new TeamStats(teamStatsData.getInt("wins"), teamStatsData.getInt("losses"), (int) Math.round(teams.get(i).calcTeamRating()), SEASON, teams.get(i).getName()));
    }
}

You are closing the first connection (c) - but not the second connection (c2).您正在关闭第一个连接 (c) - 但不是第二个连接 (c2)。

Is there a reason you're creating two connections?您创建两个连接是否有原因? Why not simply create both statements on the first connection, then close the first connection (c) after the second statement?为什么不简单地在第一个连接上创建两个语句,然后在第二个语句之后关闭第一个连接 (c)? Then you could avoid the second connection entirely (and the fact that you're not closing it is likely to be the cause of your error)然后你可以完全避免第二个连接(事实上你没有关闭它很可能是你的错误的原因)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM