python 文件导入mysql

环境:

win7 旗舰版 32bit
python2.7
mysql5.2

程序:

import os
import MySQLdb

rootdir = "e:/20121110"
i = 1
if __name__ == '__main__': 
    for parent, dirnames, filenames in os.walk(rootdir):    
        for dirname in dirnames:
            print "parent is:" + parent
            print "dirname is:" + dirname
        for filename in filenames:
            print "filename is:" + filename
            try:
                conn = MySQLdb.connect(host='localhost', user='root', passwd='******', db='lydtest', port=3306)
                cur = conn.cursor()
                sql = "load data infile 'e:/20121110/" + filename + "' into table taxi fields terminated by ',' enclosed by '\\'' lines terminated by '\\r\\n'";        
                cur.execute(sql)
                conn.commit();
                #count = cur.execute('select * from taxi')
                print 'the' + str(i) + 'success!'
                #print 'there has %s rows record' % count
                i = i + 1
                cur.close()
                conn.close()
            except MySQLdb.Error, e:
                print "Mysql Error %d: %s" % (e.args[0], e.args[1])
    print 'finished!'


你可能感兴趣的:(python 文件导入mysql)