英文:
('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}")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论