错误如下:
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 'AUTO_INCERMENT,\n BOOK VARCHAR (255),\n PRIMARY KEY ('id')\n )ENGINE=INNOD' at line 2")
sql语句:
create_sql='''
CREATE TABLE python(
id INT(11) NOT NULL AUTO_INCERMENT,
BOOK VARCHAR (255),
PRIMARY KEY ('id')
)ENGINE=INNODB DEFAULT CHARSET=utf8
'''
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 'AUTO_INCERMENT,\n BOOK VARCHAR (255),\n PRIMARY KEY ('id')\n )ENGINE=INNOD' at line 2")
sql语句:
create_sql='''
CREATE TABLE python(
id INT(11) NOT NULL AUTO_INCERMENT,
BOOK VARCHAR (255),
PRIMARY KEY ('id')
)ENGINE=INNODB DEFAULT CHARSET=utf8
'''
不是 AUTO_INCERMENT单词拼错了!
import requests
from bs4 import BeautifulSoup
import pymysql
def gethtmltext(url):
try:
r=requests.get(url,timeout=100)
r.raise_for_status()
r.encoding=r.apparent_encoding
return r.text
except:
return""def getpage(html):
try:
name=[]
soup=BeautifulSoup(html,'html.parser')
div_list=soup.find_all('div',attrs={'class':'title'})
for i in div:
cont=i.find_all('a')
na_list=name.append(cont.string)
except:
return""
def mysql_conn():
db=pymysql.connect(host="localhost",user="root",
password="123456",db="doubandushu",port=3306)
return db
def create_table():
create_sql='''
CREATE TABLE hhh(
id INT(11) NOT NULL AUTO_INCREMENT,
BOOK VARCHAR (255),
PRIMARY KEY (id)
)ENGINE=INNODB DEFAULT CHARSET=utf8
'''
db=mysql_conn()
with db:
cursor=db.cursor() cursor.execute(create_sql)
db.commit()
def insert():
sql='insert into hhh(BOOK) value (%s)'
db=mysql_conn()
with db():
cursor = db.cursor()
try:
cursor.execute(sql,(na_list.string))
cursor.connection.commit()
except BaseException as e:
print ("错误在这里>>>",e,"<<<错误在这里")
db.commit()
if __name__=='__main__':
for i in range(10):
url="https://book.douban.com/subject_search?search_text=python&cat=1001&start=%s"%(i*15)
html=gethtmltext(url)
create_table()
for i in getpage(html):
insert(i)
print('over')