gpt4 book ai didi

Python连接mysql错误: unexpected indent

转载 作者:行者123 更新时间:2023-11-29 18:04:17 31 4
gpt4 key购买 nike

我是 Python 新手。

这是我编写的用于向 mysql 表插入一行的代码:

db = MySQLdb.connect(host="localhost",    # your host, usually localhost
user="me", # your username
passwd="mepassword", # your password
db="Database") # name of the data base
cursor = db.cursor()

f = '[%d/%b/%Y:%I:%M:%S %z]'
now = datetime.datetime.strptime(result[1], f)

sql = (
"INSERT INTO Database.Table (Name, Time, Number, Size, Answer) "
"VALUES (%s, %s, %s, %d, %d, %s)"
)
data = (result[0],now.strftime('%Y-%m-%d %H:%M:%S'),int (result[3]),int (result[4]),result[2])
cursor.execute(sql, data)
db.close()

但是当我运行此代码时,出现错误:

File "myscript.py", line 40
cursor = db.cursor()
^
IndentationError: unexpected indent

我不明白这个错误。这怎么会是游标定义的问题呢?我使用默认的游标定义。或者这是mysql连接的问题?问题出在哪里?

最佳答案

试试这个:

db = MySQLdb.connect(host="localhost", user="me", passwd="mepassword", db="Database")
cursor = db.cursor()

f = '[%d/%b/%Y:%I:%M:%S %z]'
now = datetime.datetime.strptime(result[1], f)

sql = ("INSERT INTO Database.Table (Name, Time, Number, Size, Answer) " "VALUES (%s, %s, %s, %d, %d, %s)")
data = (result[0],now.strftime('%Y-%m-%d %H:%M:%S'),int (result[3]),int (result[4]),result[2])
cursor.execute(sql, data)
db.close()

您的代码中的缩进不正确。在Python中我们不使用括号{},相反,我们使用缩进来表示层次结构。

关于Python连接mysql错误: unexpected indent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48051206/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com