问答详情
源自:4-1 存储数据到MySQL

之前好好的,重新安装了数据库,然后就报错了,望帮忙看看呢。谢谢

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/kelvintong/Documents/pycode/demo1/wiki2mysql.py

Wikipedia <----> https://en.wikipedia.org/wiki/Wikipedia

Traceback (most recent call last):

  File "/Users/kelvintong/Documents/pycode/demo1/wiki2mysql.py", line 35, in <module>

    cursor.execute(sql,(url.get_text(),"https://en.wikipedia.org"+ url["href"]))

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/cursors.py", line 166, in execute

    result = self._query(query)

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/cursors.py", line 322, in _query

    conn.query(q)

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py", line 852, in query

    self._affected_rows = self._read_query_result(unbuffered=unbuffered)

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py", line 1053, in _read_query_result

    result.read()

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py", line 1336, in read

    first_packet = self.connection._read_packet()

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py", line 1010, in _read_packet

    packet.check_error()

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/connections.py", line 393, in check_error

    err.raise_mysql_exception(self._data)

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymysql/err.py", line 107, in raise_mysql_exception

    raise errorclass(errno, errval)

pymysql.err.InternalError: (1364, "Field 'id' doesn't have a default value")

Process finished with exit code 1


提问者:慕设计6340343 2017-02-20 14:46

个回答

  • qq_清蒸红烧_03602510
    2018-03-22 12:21:08

    右键点击urls表,选设计表,然后点下面的自动增长,我就是这样可以了

  • 放学路上的小学生
    2017-05-04 22:52:08

    pymysql.err.InternalError: (1364, "Field 'id' doesn't have a default value")

    id列没有默认值。试试在mysql的表中,id列设置为自动增长autoincrement。

  • 孤独的小猪
    2017-03-07 17:56:29

    我能说,我用你的代码执行,是正确的,数据库里已经正常存入了。

    #! /usr/local/bin/python3
    # -*- coding:utf-8 -*-
    
    from urllib.request import urlopen
    from bs4 import BeautifulSoup as bs
    import re
    import pymysql.cursors
    
    resp = urlopen("https://en.wikipedia.org/wiki/Main_Page").read().decode(
        "utf-8")
    
    soup = bs(resp, "html.parser")
    
    listUrls = soup.find_all("a", href=re.compile("^/wiki/"))
    for url in listUrls:
        if not re.search("\.(jpg|JPG)$", url["href"]):
            print(url.get_text("href"), "<---->",
                  "https://en.wikipedia.org" + url["href"])
            connection = pymysql.connect(host='localhost',
                                         user='root',
                                         password='123456',
                                         db='wikiurl',
                                         charset='utf8mb4')
            try:
                with connection.cursor() as cursor:
                    sql = "insert into `urls`(`urlname`,`urlhref`)values(%s,%s)"
    
                    cursor.execute(sql, (
                    url.get_text(), "https://en.wikipedia.org" + url["href"]))
    
                    connection.commit()
    
            finally:
                connection.close()

    http://img.mukewang.com/58be83c80001e97813370645.jpg

  • 慕设计6340343
    2017-02-20 14:48:40

    #! /usr/local/bin/python3
    # -*- coding:utf-8 -*-

    # 引入开发包
    from urllib.request import urlopen
    from bs4 import BeautifulSoup as bs
    import re
    import pymysql.cursors

    # 请求URL并把结果用UTF-8编码
    resp = urlopen("https://en.wikipedia.org/wiki/Main_Page").read().decode("utf-8")

    # 使用beautifulsoup去解析
    soup = bs(resp,"html.parser")

    # 获取所有以/wiki/开头的a标签的href属性
    listUrls = soup.find_all("a", href=re.compile("^/wiki/"))
    for url in listUrls:
       # 过滤-.jpg或JPG结尾的URL
       if not re.search("\.(jpg|JPG)$",url["href"]):
           # 输出URL的文字和对应的链接
           print(url.get_text("href"),"<---->","https://en.wikipedia.org"+url["href"])
           #获取数据库链接
           connection = pymysql.connect(host = 'localhost',
                                        user = 'root',
                                        password = '123456',
                                        db = 'wikiurl',
                                        charset = 'utf8mb4')
           try:
               # 获取回话指针
               with connection.cursor() as cursor:
                   sql = "insert into `urls`(`urlname`,`urlhref`)values(%s,%s)"

                   # 执行sql语句
                   cursor.execute(sql,(url.get_text(),"https://en.wikipedia.org"+ url["href"]))

                   # 提交
                   connection.commit()


           finally:
               connection.close()