我的代码有什么问题,
with connect_db() as conn:
cursor = conn.cursor()
stmt = """INSERT INTO ip_records ip_address VALUES (%s)"""
try:
cursor.execute(stmt, (ip))
except mysql.connector.Error as err:
print err
return
我正在尝试将ip地址作为字符串插入mysql表。但我收到此错误:
1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'ip_address VALUES (192.168.11.111)' at line 1
答案 0 :(得分:1)
您缺少将IP视为字符串的""
:
with connect_db() as conn:
cursor = conn.cursor()
stmt = """INSERT INTO ip_records (ip_address) VALUES (%s)"""
try:
cursor.execute(stmt, (ip,))
except mysql.connector.Error as err:
print err
return
编辑
您还需要昏迷以将参数作为元组(ip,)
而不是(ip)
实际错误是语法错误(因为(ip_address)
周围没有括号,而python参数错误是由于元组(ip,)
没有逗号引起的错误。
引号不是必需的。
答案 1 :(得分:0)
You need to add放在ip_address
周围的括号中或将其删除。