我正在尝试遍历变量“decrypt”中保存的解密文件的内容,并将这些内容写入MySQL数据库表(data_1)。
请帮助我确定此错误的含义以及可能解决错误的方法。
>>> import gnupg
>>> import csv
>>> import MySQLdb
>>> import StringIO
>>> import io
>>> gpg = gnupg.GPG(gnupghome="/home/chefgit/Desktop/.gnupg")
>>> file = open("/home/chefgit/Desktop/file_10.gpg", 'r+')
>>> decrypt=gpg.decrypt_file(file)
>>> connect = MySQLdb.connect('localhost', 'chefgit', 'taku99', 'python_db_1')
>>> cursor = connect.cursor()
>>> sql = "INSERT INTO data_1 ('id', 'first_name', 'last_name', 'email','country', 'ip_address')VALUES('%d','%s','%s','%s','%s','%s')"
>>> csv_input=csv.reader(StringIO.StringIO(decrypt))
>>> for row in csv_input:
... cursor.execute(sql,row)
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 187, in execute
query = query % tuple([db.literal(item) for item in args])
TypeError: %d format: a number is required, not str
>>> csv_input=csv.reader(StringIO.StringIO(decrypt))
>>> for row in csv_input:
... cursor.executemany(sql, row)
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 237, in executemany
r = r + self.execute(query, a)
File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 187, in execute
query = query % tuple([db.literal(item) for item in args])
TypeError: %d format: a number is required, not str
>>> csv_input = csv.reader(StringIO.StringIO(decrypt))
>>> for row in csv_input:
... print(row)
...
['obin', 'Mitchell', 'rmitchell0@paginegialle.it', 'Czech Republic', '247.22.171.221']
['2', 'Kathy', 'Wheeler', 'kwheeler1@soup.io', 'Indonesia', '90.207.243.94']
['3', 'Johnny', 'Grant', 'jgrant2@delicious.com', 'Cyprus', '40.53.219.97']
['4', 'Henry', 'Dunn', 'hdunn3@upenn.edu', 'China', '75.234.225.243']
['5', 'Craig', 'Holmes', 'cholmes4@dailymotion.com', 'Ukraine', '218.128.169.131']
['6', 'Phillip', 'Shaw', 'pshaw5@archive.org', 'France', '96.80.252.82']
['7', 'Lillian', 'Peterson', 'lpeterson6@arstechnica.com', 'Russia', '104.156.33.182']
['8', 'Linda', 'Hall', 'lhall7@tinypic.com', 'China', '253.195.137.88']
['9', 'Denise', 'Patterson', 'dpatterson8@lycos.com', 'Kazakhstan', '71.170.34.130']
[]
答案 0 :(得分:0)
您列出的其中一列是ID,它将数字作为一个值,并且您尝试插入&#34; obin&#34;作为数据库中ID的值,前面有数据库说
TypeError: %d format: a number is required, not str