AgentBench icon indicating copy to clipboard operation
AgentBench copied to clipboard

DBbench中mysql命令执行的结果,总是没有表

Open Chucy2020 opened this issue 8 months ago • 2 comments

{ "index": 298, "error": null, "info": null, "output": { "index": 298, "status": "completed", "result": { "answer": "1049 (42000): Unknown database 'team_stadiums'", "type": "UPDATE", "error": "" }, "history": [ { "role": "user", "content": "\nI will ask you a question, then you should help me operate a MySQL database with SQL to answer the question.\nYou have to explain the problem and your solution to me and write down your thoughts.\nAfter thinking and explaining thoroughly, every round you can choose to operate or to answer.\nyour operation should be like this:\nAction: Operation\nsql\nSELECT * FROM table WHERE condition;\n\nYou MUST put SQL in markdown format without any other comments. Your SQL should be in one line.\nEvery time you can only execute one SQL statement. I will only execute the statement in the first SQL code block. Every time you write a SQL, I will execute it for you and give you the output.\nIf you are done operating, and you want to commit your final answer, then write down:\nAction: Answer\nFinal Answer: ["ANSWER1", "ANSWER2", ...]\nDO NOT write this pattern unless you are sure about your answer. I expect an accurate and correct answer.\nYour answer should be accurate. Your answer must be exactly the same as the correct answer.\nIf the question is about modifying the database, then after done operation, your answer field can be anything.\nIf your response cannot match any pattern I mentioned earlier, you will be judged as FAIL immediately.\nYour input will be raw MySQL response, you have to deal with it by yourself.\n" }, { "role": "agent", "content": "Ok." }, { "role": "user", "content": "Modify the stadium capacity to 45,000 for Suwon Samsung Bluewings in team_stadiums table.\nThe name of this table is team_stadiums, and the headers of this table are Team,Location,Stadium,Stadium capacity." }, { "role": "agent", "content": "Action: Operation\nsql\nUPDATE team_stadiums SET \"Stadium capacity\" = 45000 WHERE Team = 'Suwon Samsung Bluewings';\n" }, { "role": "user", "content": "1049 (42000): Unknown database 'team_stadiums'" }, { "role": "agent", "content": "Action: Answer\nFinal Answer: The database "team_stadiums" does not exist. Please check the database name and try again." } ] }, "time": { "timestamp": 1745569432355, "str": "2025-04-25 16:23:52" } }

Chucy2020 avatar Apr 27 '25 02:04 Chucy2020

I encountered the same error. Does anyone know a solution?

nariaki3551 avatar Sep 26 '25 00:09 nariaki3551

 def execute(
        self,
        sql: str,
        database: str = None,
        data: Union[Sequence, Dict[str, Any]] = (),
    ) -> Optional[str]:
        # Try to ensure connection is alive without reconnecting if possible
        try:
            self.conn.ping(reconnect=False, attempts=1)
        except:
            try:
                self.conn.reconnect()
            except:
                pass

        result = ""
        try:
            with self.conn.cursor() as cursor:
                # Split SQL statements by semicolon and execute them individually
                statements = [stmt.strip() for stmt in sql.split(';') if stmt.strip()]

                for i, statement in enumerate(statements):
                    if not statement:
                        continue

                    # Skip COMMIT statement as it's handled by autocommit
                    if statement.upper() == 'COMMIT':
                        continue

                    # For database creation and table insertion with parameters
                    if i == len(statements) - 2 and data:  # INSERT statement (usually second to last before COMMIT)
                        cursor.execute(statement, data)
                    else:
                        # USE statement or CREATE statement
                        if statement.upper().startswith('USE'):
                            # If it's already a USE statement, execute as-is
                            cursor.execute(statement)
                        elif database and i == 0:
                            # For the first statement, ensure we're using the correct database
                            cursor.execute(f"USE `{database}`;")
                            if not statement.upper().startswith(('USE', 'CREATE', 'DROP')):
                                cursor.execute(statement)
                        else:
                            cursor.execute(statement)

                    # Fetch results for SELECT statements or affected rows for others
                    if statement.upper().startswith('SELECT') or statement.upper().startswith('SHOW'):
                        result_data = cursor.fetchall()
                        result = str(result_data)
                    else:
                        result = f"Success: {cursor.rowcount} rows affected"

            self.conn.commit()
        except Exception as e:
            result = str(e)
            self.conn.rollback()
        if len(result) > 800:
            result = result[:800] + "[TRUNCATED]"
        return result

i modify the code in "src/server/tasks/dbbench/Interaction.py", now it will auto add "use database" before executing sql

Luci-0722 avatar Nov 14 '25 10:11 Luci-0722