-1

I'm trying to loop through the contents of a decrypted file held in the variable "decrypt" and write those contents into a MySQL database table (data_1).

Please help me identify what this error means and also a possible work around the error.

>>> 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']
[]
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
user3530362
  • 89
  • 1
  • 1
  • 5

1 Answers1

0

One of the column you listed is ID which expect number as a value and you trying to insert "obin" as a value for ID in database, there fore database saying that

TypeError: %d format: a number is required, not str
Kishor Pawar
  • 3,386
  • 3
  • 28
  • 61