-1

Here is my code for insertion.

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))

my database name is news_table which has 9 attributes, but when i am trying to insert the data with the code it gives me the following error

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")

i am using mysql with python for the first time. Can anyone provide some help about the issue. NB: I have no database connection error or package installation error problem.

Pan DA
  • 37
  • 1
  • 1
  • 9

2 Answers2

0

You have

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

There are more fields in the destination columns than %s in your VALUES(...)

you MUST supply a value for each destination column

Alfabravo
  • 7,493
  • 6
  • 46
  • 82
  • [link](https://ideone.com/g5i51r) new problem have arisen & sorry for the last problem as undoing is not noticed before posting the question. & i already have converted the string need to be sent to the database with default str() function for safe case. but still the problem arises. – Pan DA Jul 27 '18 at 17:19
  • @sourav sorry. Please include ALL the relevant info IN your question. The idea is to keep the solution (whatever it is) for everyone, here in SO. – Alfabravo Jul 27 '18 at 19:24
0

Your error says "Column count doesn't match value count at row 1". Number of columns doesn't match your %s

It should be

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))

After removing news_view and news_hit

OR

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))

After adding default values 0 for news_view and news_hit

Sookie Singh
  • 1,543
  • 11
  • 17
  • [link](https://ideone.com/g5i51r) new problem have arisen & sorry for the last problem as undoing is not noticed before posting the question. – Pan DA Jul 27 '18 at 17:14
  • i already have converted the string need to be sent to the database with default str() function for safe case. but still the problem arises. – Pan DA Jul 27 '18 at 17:18
  • Look at this for that error: https://stackoverflow.com/questions/10957238/incorrect-string-value-when-trying-to-insert-utf-8-into-mysql-via-jdbc and https://stackoverflow.com/questions/1168036/how-to-fix-incorrect-string-value-errors/24559308 – Sookie Singh Jul 27 '18 at 17:21