简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM