I am trying to insert rows from a csv file into a MySQL table.
I tried this code
JavaScript
x
10
10
1
with open('test.csv','r') as f:
2
reader = csv.reader(f)
3
4
for row in reader :
5
6
value=[row[0],row[3]]
7
cur.execute("insert into tab(name, nb_cases) values(%s,%s)", value)
8
9
con.commit()
10
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
JavaScript
1
2
1
float("983,469".replace(',','.'))
2
JavaScript121Create table testa( x Double)
2
✓
JavaScript121INSERT INTO testa VALUE (983,469);
2
Column count doesn't match value count at row 1
JavaScript121INSERT INTO testa VALUE (983.469);
2
✓
JavaScript121SELECT * FROM testa;
2
| x | | ------: | | 983.469 |
db<>fiddle here