python3中没有mysqldb,需要使用pymysql,使用如下语句来链接数据库:
db = pymysql.connect(host='', port=3306, user='', password='', db='',charset='utf8')
cursor = db.cursor()
python3中通过创建的cursor来使用sql语句,例如:sql = "inset in to data_spider(name,data_body) values ('wudi','666')"
然后需要通过如下两行代码来执行sql语句:
cursor.execute(sql)
db.commit()
python在执行上述过程中有可能出现异常所以需要在try:下来执行
下面是上面过程的完整代码:
try:
sql = "insert into tongye_list(name,body_neirong) values ('%s','%s')"%(cell_A2,ss)
cursor.execute(sql)
db.commit()
except:
pass
另外python3在书写sql语句中的values语句时,如果values后面的数据为一个变量,则需要表示为下述的格式:
x = "小明"
y="小明是个好孩子"
sql = "insert into 表名(name , body) values('%s','%s')"%(x,y)
https://blog.csdn.net/Mr_wuliboy/article/details/81186786
sql = "insert into tongye_list(name,body_neirong) values (%s, %s)"%(cell_A2,ss)
sql = "insert into tongye_list(name,body_neirong) values ('%s','%s')"%(cell_A2,ss)
来源:CSDN
作者:tony2278
链接:https://blog.csdn.net/tony2278/article/details/103684138