Приветствую всех, возникла ошибка в скрипте при добавлении данных из .csv файла в MySQL бд. Ошибка состоит в том, что пайтон неправильно передает значение столбика из .csv [INT -> %d] а SQL думает что значение=строка -> %d, а формат столбика INT. Как правильно передать параметр int из .csv в INT MySQL?
.csv 1строка
160942891 Muni Inspection 2016-04-03T00:00:00 2016-04-03T00:00:00 2016-04-03T00:00:00 18:15 2016-04-03T18:15:00 Not recorded 900 Block Of Ulloa St San Francisco CA 1 Common Location West Portal Muni, Sf
.csv 238строка
160913103 Encampment 2016-03-31T00:00:00 2016-03-31T00:00:00 2016-03-31T00:00:00 18:57 2016-03-31T18:57:00 ADV 2700 Block Of Folsom St San Francisco CA 1 Premise Address
import MySQLdb
import csv
db = MySQLdb.connect(host="192.168.1.2",
port=3306,
user="non-root-boy",
passwd="123456",
db="sfpd")
print ("\nConnection to DataBase established..\n")
cur = db.cursor()
f = open('/home/decoy/Downloads/police-department-calls-for-service.csv')
csv_f = csv.reader(f)
for row in csv_f:
if len(row) < 12:
continue
cur.execute("INSERT INTO crimes(`Crime Id`, `Original Crime Type Name`, \
`Report Date`, `Call Date`, `Offense Date`, `Call Time`, \
`Call Date Time`, `Disposition`, `Address`, `City`, `State`, `Agency Id`, \
`Address Type`, `Common Location`) VALUES(%d, %s, %s, %s, %s, %s, \
%s, %s, %s, %s, %s, %d, %s, %s)", row)
print ("Done")
Connection to DataBase established..
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/MySQLdb/cursors.py", line 238, in execute
query = query % args
TypeError: %d format: a number is required, not str
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/decoy/Projects/Python/sfpdBASE_insert.py", line 25, in
%s, %s, %s, %s, %s, %d, %s, %s)", row)
File "/usr/lib/python3/dist-packages/MySQLdb/cursors.py", line 240, in execute
self.errorhandler(self, ProgrammingError, str(m))
File "/usr/lib/python3/dist-packages/MySQLdb/connections.py", line 52, in defaulterrorhandler
raise errorclass(errorvalue)
_mysql_exceptions.ProgrammingError: %d format: a number is required, not str
Process finished with exit code 1