如何在不使用密码的情况下连接MySQL?(PyMySQL)

Eri*_*ans 7 python mysql

这是我的代码的相关部分:

        try:
            if self.pass_entry.get():
                self.connection = pymysql.connect(
                    host=self.host_entry.get(),
                    user=self.user_entry.get(),
                    password=self.pass_entry.get(),
                    db=self.db_entry.get(),
                    charset="utf8mb4",
                    cursorclass=pymysql.cursors.DictCursor
                )
            else:
                self.connection = pymysql.connect(
                    host=self.host_entry.get(),
                    user=self.user_entry.get(),
                    db=self.db_entry.get(),
                    charset="utf8mb4",
                    cursorclass=pymysql.cursors.DictCursor
                )
        except Exception as e:
            self.console.insert(tk.END, "Error: {err}\n".format(err=str(e)))
Run Code Online (Sandbox Code Playgroud)

这是错误:

Connecting to 127.0.0.1...
Error: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")
Run Code Online (Sandbox Code Playgroud)

这里"root"上没有密码,PyMySQL假设它有一个密码.如何在不使用密码的情况下连接?(当密码字段/字符串为空时,我试图省略密码选项,但PyMySQL不考虑它).

小智 1

当数据库没有密码时,您需要将“”作为密码传递给函数。

在你的其他地方试试这个:

        else:
            self.connection = pymysql.connect(
                host=self.host_entry.get(),
                user=self.user_entry.get(),
                password="",
                db=self.db_entry.get(),
                charset="utf8mb4",
                cursorclass=pymysql.cursors.DictCursor
            )
Run Code Online (Sandbox Code Playgroud)