Skip to content
Advertisement

CSV data to MySQL table

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

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement