现在再自学Python3.0爬虫,源代码如下:
import pymysql
db = pymysql.connect(host='localhost',user='root',password='123456',port=3306)
cursor = db.cursor()
cursor.execute('SELECT VERSION()')
data = cursor.fetchone()
print('Database version:',data)
cursor.execute("CREATE DATABASE spiders DEFAULT CHARACTER SET utf8")
db.close()
运行结果如下:
Database version: ('8.0.13',)
Traceback (most recent call last):
File "C:\Users\yuxiaofeng1229\Desktop\lianxi.py", line 12, in <module>
cursor.execute("CREATE DATABASE spiders DEFAULT CHARACTER SET utf8")
File "C:\Users\yuxiaofeng1229\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\cursors.py", line 170, in execute
result = self._query(query)
File "C:\Users\yuxiaofeng1229\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\cursors.py", line 328, in _query
conn.query(q)
File "C:\Users\yuxiaofeng1229\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 516, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "C:\Users\yuxiaofeng1229\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 727, in _read_query_result
result.read()
File "C:\Users\yuxiaofeng1229\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 1066, in read
first_packet = self.connection._read_packet()
File "C:\Users\yuxiaofeng1229\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 683, in _read_packet
packet.check_error()
File "C:\Users\yuxiaofeng1229\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "C:\Users\yuxiaofeng1229\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.ProgrammingError: (1007, "Can't create database 'spiders'; database exists")
下面这些问题是什么问题呢?应该怎么修改哈?
1 回答
已采纳

pardon110
TA贡献1038条经验 获得超227个赞
注意看报错信息Can't create database 'spiders'; database exists
如果不理解,百度翻译一下也会知道,很明显告诉你,所创建的库spiders,在数据库中已经存在。你要么删除数据库中的spiders库,再执行代码。要么在代码中不要有建这个库的语句。
添加回答
举报
0/150
提交
取消