【问题标题】:Insertion error in MySQL database using python package pymysql使用python包pymysql在MySQL数据库中插入错误
【发布时间】:2019-01-04 18:47:35
【问题描述】:

这是我的插入代码。

myCursor.execute("INSERT INTO news_table(news_title, news_source, news_link, news_img, news_date, news_view, news_text, news_hit) VALUES (%s,%s,%s,%s,%s,%s);",(nextntitle, nextnsource, nextnlink, nextnimg, nextndate, nextntext))

我的数据库名称是 news_table,它有 9 个属性,但是当我尝试使用代码插入数据时,它给了我以下错误

Traceback (most recent call last):
  File "C:/Users/qasys/PycharmProjects/conda_main/pythonmysql/checkdb.py", line 82, in <module>
    myCursor.execute("INSERT INTO news_table(news_title, news_source, news_link, news_img, news_date, news_view, news_text, news_hit) VALUES (%s,%s,%s,%s,%s,%s);",(nextntitle, nextnsource, nextnlink, nextnimg, nextndate, nextntext))
  File "C:\ProgramData\Anaconda3\envs\conda_main\lib\site-packages\pymysql\cursors.py", line 170, in execute
    result = self._query(query)
  File "C:\ProgramData\Anaconda3\envs\conda_main\lib\site-packages\pymysql\cursors.py", line 328, in _query
    conn.query(q)
  File "C:\ProgramData\Anaconda3\envs\conda_main\lib\site-packages\pymysql\connections.py", line 516, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "C:\ProgramData\Anaconda3\envs\conda_main\lib\site-packages\pymysql\connections.py", line 727, in _read_query_result
    result.read()
  File "C:\ProgramData\Anaconda3\envs\conda_main\lib\site-packages\pymysql\connections.py", line 1066, in read
    first_packet = self.connection._read_packet()
  File "C:\ProgramData\Anaconda3\envs\conda_main\lib\site-packages\pymysql\connections.py", line 683, in _read_packet
    packet.check_error()
  File "C:\ProgramData\Anaconda3\envs\conda_main\lib\site-packages\pymysql\protocol.py", line 220, in check_error
    err.raise_mysql_exception(self._data)
  File "C:\ProgramData\Anaconda3\envs\conda_main\lib\site-packages\pymysql\err.py", line 109, in raise_mysql_exception
    raise errorclass(errno, errval)
pymysql.err.InternalError: (1136, "Column count doesn't match value count at row 1")

我是第一次使用 mysql 和 python。任何人都可以就这个问题提供一些帮助。 注意:我没有数据库连接错误或包安装错误问题。

【问题讨论】:

  • @sorav 我已根据您的问题编辑了我的答案。
  • @Er_sherlockian 检查你答案的评论部分

标签: python mysql database pymysql


【解决方案1】:

你有

(news_title, news_source, news_link, news_img, news_date, news_view, news_text, news_hit) 
VALUES (%s,%s,%s,%s,%s,%s)

目标列中的字段比 VALUES(...) 中的 %s 多

您必须为每个目标列提供一个值

【讨论】:

  • link 出现了新问题,对最后一个问题表示抱歉,因为在发布问题之前没有注意到撤消。 & 我已经转换了需要使用默认 str() 函数发送到数据库的字符串,以确保安全。但问题仍然存在。
  • @sourav 抱歉。请在您的问题中包含所有相关信息。我们的想法是在 SO 中为每个人保留解决方案(无论它是什么)。
【解决方案2】:

您的错误显示“列计数与第 1 行的值计数不匹配”。列数与您的 %s 不匹配

应该是

myCursor.execute("INSERT INTO news_table(news_title, news_source, news_link, news_img, news_date, news_view, news_text, news_hit) VALUES (%s,%s,%s,%s,%s,0,%s,0);",(nextntitle, nextnsource, nextnlink, nextnimg, nextndate, nextntext))

删除 news_viewnews_hit

myCursor.execute("INSERT INTO news_table(news_title, news_source, news_link, news_img, news_date, news_view, news_text, news_hit) VALUES (%s,%s,%s,%s,%s,0,%s,0);",(nextntitle, nextnsource, nextnlink, nextnimg, nextndate, nextntext))

news_viewnews_hit

添加默认值 0 后

【讨论】:

猜你喜欢
  • 2018-10-14
  • 2019-09-06
  • 2015-04-13
  • 2019-07-13
  • 1970-01-01
  • 2020-02-02
  • 2019-02-19
  • 2021-08-13
  • 2021-01-03
相关资源
最近更新 更多