[英]Python mysql.connector error while inserting boolean into mysql database
In the code below, I am trying to insert a boolean value in Network table, where the status field is declared as boolean. 在下面的代码中,我试图在网络表中插入一个布尔值,其中状态字段被声明为布尔值。
import urllib2
import mysql.connector as conn
import MySQLdb
import logging
class getData:
@staticmethod
def checkNetwork():
try:
urllib2.urlopen('https://www.google.com', timeout = 2)
return True
except urllib2.URLError as err:
return False
@staticmethod
def connectDB():
db = conn.connect(host='****', user='****', passwd='****', db='*******')
cursor = db.cursor()
return db,cursor
@staticmethod
def insertNData(data):
print type(data)
db,cursor = getData.connectDB()
sql_Query = "INSERT INTO Network(status) VALUES(%s);"
try:
result= cursor.execute(sql_Query,data)
db.commit()
logging.warn("%s", result)
logging.info("Success")
except MySQLdb.IntegrityError:
logging.warn("Failed")
finally:
db.close()
return True
netStat = getData.checkNetwork()
getData.insertNData(netStat)
When I run the code, I get the below error 运行代码时,出现以下错误
ERROR 1064 (42000): You have an error in your SQL syntax; 错误1064(42000):您的SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s)' at line 1 检查与您的MySQL服务器版本相对应的手册以获取正确的语法以在第1行附近使用'%s)'
I tried searching on google to find some solution and also changed a few things to test but still the same error. 我尝试在Google上搜索以找到一些解决方案,还更改了一些要测试的东西,但仍然是相同的错误。
Thanks in advance. 提前致谢。
There is error in this line: 这行有错误:
sql_Query = "INSERT INTO Network(status) VALUES(%s);"
You are not passing the value correctly. 您没有正确传递值。 You created a placeholder but did not fill it. 您创建了一个占位符,但没有填写。
Use: 采用:
for python3.6 and above: 对于python3.6及更高版本:
sql_Query = f"INSERT INTO Network(status) VALUES({data});"
for python 2 and 3: 对于python 2和3:
sql_Query = "INSERT INTO Network(status) VALUES({});".format(data)
or 要么
sql_Query = "INSERT INTO Network(status) VALUES(%s);" %(data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.