我的问题在代码之后:
[root@localhost dest]# python
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import csv
>>> import MySQLdb
>>> import gnupg
>>> gpg = gnupg.GPG(gnupghome="/home/chefgit/Desktop/.gnupg")
>>> file = open("/home/chefgit/Desktop/csv/dest/file_100.gpg", 'r+')
>>> decrypt = gpg.decrypt_file(file)
>>> import StringIO
>>> csv_input = csv.reader(StringIO.StringIO(decrypt))
>>> import MySQLdb
>>> connect = MySQLdb.connect\
('localhost', 'chefgit', 'password', 'python_db_1')
>>> cursor = connect.cursor()
>>> cursor.execute("SELECT VERSION()")
1L
>>> print (cursor.fetchone())
('5.1.73',)
>>> sql = "INSERT INTO python_db_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:
... if csv_input.line_num == 1:
... continue
... cursor.execute(sql '%row')
File "<stdin>", line 4
cursor.execute(sql '%row')
^
SyntaxError: invalid syntax
>>>
我很难搞清楚如何遍历行并将它们加载到MySQL数据库表中。
我必须循环遍历csv_input变量(从StringIO.StringIO(decrypt)
获取)并逐行读取并将数据加载到数据库表中。我无法从文件中读取它。我必须从内存中的对象中读取它。这是我老板对我的要求。