pymysql.err.ProgrammingError:

源代码:
import pymysql
#使用参数创建链接
conn = pymysql.connect(host = ‘127.0.0.1’,
port=3306,user = ‘root’,passwd = ‘Lmx18287620110’,
db = ‘python’,charset = ‘utf8’,connect_timeout = 1000)#使用connent链接数据库
cursor = conn.cursor() #创建游标
sql=’’‘create table if not exists class (id int(10) primary key auto_increment,name varchar(20) not null)’’’ #创建表
cursor.execute(sql) #执行创建表的SQL语句
cursor.execute(‘show tables’) #查看创建的表

#数据准备
import requests
import chardet
from bs4 import BeautifulSoup
url = ‘http://www.tipdm.com/tipdm/index.html’ #设置请求头
ua = {‘User-Agent’ : ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36’}
rqg = requests.get(url,headers = ua)
rqg.encoding = chardet.detect(rqg.content)[‘encoding’]
html = rqg.content.decode(‘utf-8’)
soup = BeautifulSoup(html,‘lxml’)
target = soup.title.string
print(‘标题的内容:’,target)
#插入数据
title = ‘tipdm’
sql = “insert into class (name,text) values(‘%s’,‘%s’)”
cursor.execute(sql,(title,target)) #执行插入语
conn.commit() #提交事务
data = cursor.execute(‘select * from class’) #查询数据
data = cursor.fetchmany() #使用fetchmrany方法获取操作结果
print(‘查询获取结果:’,data)
conn.close()

这是错误
Traceback (most recent call last):
File “C:\Users\lmx18\AppData\Local\Programs\Python\Python38\json存储数据.py”, line 26, in
cursor.execute(sql,(title,target)) #执行插入语
File “C:\Users\lmx18\AppData\Local\Programs\Python\Python38\lib\site-packages\pymysql\cursors.py”, line 170, in execute
result = self._query(query)
File “C:\Users\lmx18\AppData\Local\Programs\Python\Python38\lib\site-packages\pymysql\cursors.py”, line 328, in _query
conn.query(q)
File “C:\Users\lmx18\AppData\Local\Programs\Python\Python38\lib\site-packages\pymysql\connections.py”, line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File “C:\Users\lmx18\AppData\Local\Programs\Python\Python38\lib\site-packages\pymysql\connections.py”, line 732, in _read_query_result
result.read()
File “C:\Users\lmx18\AppData\Local\Programs\Python\Python38\lib\site-packages\pymysql\connections.py”, line 1075, in read
first_packet = self.connection._read_packet()
File “C:\Users\lmx18\AppData\Local\Programs\Python\Python38\lib\site-packages\pymysql\connections.py”, line 684, in _read_packet
packet.check_error()
File “C:\Users\lmx18\AppData\Local\Programs\Python\Python38\lib\site-packages\pymysql\protocol.py”, line 220, in check_error
err.raise_mysql_exception(self._data)
File “C:\Users\lmx18\AppData\Local\Programs\Python\Python38\lib\site-packages\pymysql\err.py”, line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1064, “You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘tipdm’’,’‘泰迪科技-专注于大数据技术研发及知识传播’’)’ at line 1”)

方案:
把sql = “insert into class (name,text) values(‘%s’,‘%s’)”
改成sql = ’insert into class (name,text) values(s,s)‘

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值