I have managed to create a table called drivers
however I am unable to load a csv file called drivers.csv
into this table
Error:
File "/workspaces/87976355/project/app.py", line 17, in <module>
db.execute("INSERT INTO drivers (driverId, driverRef, number, code, forename, surname, dob, nationality, url) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", row.split(","))
RuntimeError: more placeholders (?, ?, ?, ?, ?, ?, ?, ?, ?) than values ('driverId', 'driverRef', 'number', 'code', 'forename', 'surname', 'dob', 'nationality', 'url
')
I've also tried to do:
db.execute("INSERT INTO drivers (driverId, driverRef, number, code, forename, surname, dob, nationality, url) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", driverId, driverRef, number, code, forename, surname, dob, nationality, url)
But no luck which I believe python is reading driverid
, driverref
etc. as variables rather than name of the columns in table - drivers.
Would anyone know why I am encountering this?
I think you're looking for string formatting in Python, which uses a %
and %s
or %d
. So something like this
with open('drivers.csv', 'r') as file:
for row in file:
db.execute("INSERT INTO drivers(driverId, driverRef, number, code, forename, surname, dob, nationality, url) values ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')" % row.split(","))
#instead of directly inserting into the database you should probably first test it, like so:
#print("INSERT INTO drivers(driverId, driverRef, number, code, forename, surname, dob, nationality, url) values ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')" % row.split(","))
However, that will probably still give errors for decimals values or empty NaN values. And also, it doesn't check if the csv contains too many or too few columns.
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.