1

以下のコードでは、MySQL クエリで 'filenames' python リストの値と同一の 'name' 列の値を選択します。結果として空の () を取得し続けます。誰でもこれで私を助けることができますか?

import MySQLdb
import fnmatch
import os
import pprint

connect = MySQLdb.connect(host = "123.5.4.4", port=3366, user = "me", passwd="*********", db="data2")

filenames=[]
maindict = {}  

for root, dirnames, filenames in os.walk('d:/data1'):  
    for filename in fnmatch.filter(filenames, '*.txt'):   
       filenames.append(filename)

with connect:            
    cur= connect.cursor()            
    cur.execute("SELECT version,name FROM data2.files WHERE name IN ('filenames'.join('')" )
    result = cur.fetchall()

pprint.pprint(result)  
4

2 に答える 2

0

交換:

cur.execute("SELECT version,name FROM data2.files WHERE name IN ('filenames'.join('')" )

と:

cur.execute("SELECT version,name FROM data2.files WHERE name IN ('%s')" % "','".join(filenames) )

Pythonシェルで:

>>> filenames=['file1', 'file2']
>>> query = "SELECT version,name FROM data2.files WHERE name IN ('%s')" % "','".join(filenames)
>>> print query
SELECT version,name FROM data2.files WHERE name IN ('file1','file2')
>>>
于 2013-04-03T09:43:13.207 に答える