2

I am trying to insert rows from a csv file into a MySQL table.

I tried this code

with open('test.csv','r') as f:
reader = csv.reader(f)

for row in reader :

value=[row[0],row[3]]
cur.execute("insert into tab(name, nb_cases) values(%s,%s)", value)

con.commit()

Nb: tab is a table with two columns name (varchar 20) and nb_cases (double)

I get this error:

DataError: (1265, "Data truncated for column 'nb_cases' at row 1")

6
  • try put row[0],row[3] inside () directly inside cur Commented Apr 28, 2020 at 20:59
  • the same message error appears Commented Apr 28, 2020 at 21:02
  • first check which tyoe nb_cases has and then print row[3] and see it it fits Commented Apr 28, 2020 at 21:06
  • Out[2]: '983,469' Commented Apr 28, 2020 at 21:13
  • Shouldn't the query be: cur.execute("insert into tab(name, nb_cases) values (%s, %s)", value)? Commented Apr 28, 2020 at 21:29

1 Answer 1

2

your number doesn't fit it must be 983.469 with a point not a comma

use

float("983,469".replace(',','.'))
Create table testa( x Double)
INSERT INTO testa VALUE (983,469);
Column count doesn't match value count at row 1
INSERT INTO testa VALUE (983.469);
SELECT * FROM testa;
|       x |
| ------: |
| 983.469 |

db<>fiddle here

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.