‘can only concatenate str (not “tuple”) to str’在Python的mysql.connector.connect中。

huangapple go评论77阅读模式
英文:

('can only concatenate str (not "tuple") to str',) on mysql.connector.connect in Python

问题

I have a problem in Python. The system says "‘tuple’ object has no attribute ‘strip’" above the function cnx = mysql.connector.connect(host=self.host, database=self.database, user=self.username, password=self.password), as if it were recognizing the comma as a concatenation. I spent hours and I can't solve it.

class Database:
    def __init__(
        self,
        host="localhost",
        database="bayer",
        username="root",
        password="root",
        port="3306",
    ) -> None:
        self.host = str(host)
        self.database = str(database)
        self.username = str(username)
        self.password = str(password)
        self.port = str(port)
        self.connection = None

    def connectDatabase(self):
        try:
            logging.basicConfig(level=logging.INFO)

            if self.connection is not None:
                logging.info("return active connection for you")
                return self.connection

            self.connection = mysql.connector.connect(
                host=self.host, database=self.database, user=self.username, password=self.password
            )

            logging.info("CON02")

            if self.connection.is_connected():
                logging.info("You're connected to database: " + self.database)
                return self.connection
            else:
                logging.error("Error in connected to database: " + self.database)
            logging.info("CON03")
        except Error as e:
            logging.info("Database Error: " + e.args)
            # raise (e)

    def query(self, connection, sql, *args):
        cursor = connection.cursor()
        try:
            cursor.execute(sql, args)
            return cursor
        except Error as e:
            logging.error("Database Error: " + e.args)
            raise (e)

I just need to get past this line with the connection made.

英文:

I have a problem in Python. The system says ("'tuple' object has no attribute 'strip'",) above the function cnx = mysql.connector.connect(host=self.host, database=self.database , user= self.username , password=self.password ), as if it were recognizing the comma as a concatenation. I spent hours and I can't solve it.

class Database:
    def __init__(
        self,
        host="localhost",
        database="bayer",
        username="root",
        password="root",
        port="3306",
    ) -> None:
        self.host = str(host)
        self.database = str(database)
        self.username = str(username)
        self.password = str(password)
        self.port =str(port)
        self.connection = None
    
    
    def connectDatabase(self):
        try:
            logging.basicConfig(level=logging.INFO)
            
            if self.connection is not None:
                logging.info("return active connection for you")
                return self.connection
                       
            self.connection =  mysql.connector.connect(host=self.host, database=self.database , user=self.username , password=self.password )
            
            
            logging.info("CON02")
            
            if self.connection.is_connected():
                logging.info("You're connected to database: " + self.database)
                return self.connection
            else:
                logging.error(
                    "Error in connected to database: " +
                    self.database)
            logging.info("CON03")
        except Error as e:
            logging.info("Database Error: " + e.args)
            #raise (e)

    def query(self, connection, sql, *args):
        cursor = connection.cursor()
        try:
            cursor.execute(sql, args)
            return cursor
        except Error as e:
            logging.error("Database Error: " + e.args)
            raise (e)

I just need to get past this line with the connection made.

答案1

得分: 1

e.args是一个元组,即

>>> type(ValueError().args)
<class 'tuple'>

因此,当尝试将字符串与元组连接时,即logging.info("Database Error: " + e.args)会导致崩溃:

>>> "" + ()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "tuple") to str

您可以在e.args上使用join来连接它们,而不会引发错误:

logging.info("Database Error: " + "".join(e.args))

或者,更好的做法是使用f-strings

logging.info(f"Database Error: {e.args}")
英文:

e.args is a tuple, i.e

>>> type(ValueError().args)
<class 'tuple'>

So, when trying to concatenate a string with a tuple, i.e logging.info("Database Error: " + e.args) it crashes:

>>> "" + ()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "tuple") to str

You could join on the e.args to concatenate them without an error being raised:

logging.info("Database Error: " + "".join(e.args))

or, event better, use f-strings

logging.info(f"Database Error: {e.args}")

huangapple
  • 本文由 发表于 2023年5月22日 21:00:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定