Copy CodeThe code is as follows:
#-*-Coding:utf-8-*-
'''
Created on December 9, 2013
@author: Hhdys
'''
Import OS
Import Mysql.connector
Config = {
' User ': ' Root ',
' Password ': ' ****** ',
' Host ': ' 127.0.0.1 ',
' Database ': ' Test ',
' Raise_on_warnings ': True,
}
CNX = Mysql.connector.connect (**config)
Class ReadFile:
def readlines (self):
f = open ("E:/data/2013-11-5.txt", "R", 1, "Utf-8")
I=0
List=[]
For line in F:
STRs = Line.split ("\ t")
If Len (strs)! = 5:
Continue
Data= (Strs[0], strs[1], strs[2], strs[3], strs[4].replace ("\ n", ""))
List.append (data)
Cursor=cnx.cursor ()
sql = "INSERT into Data_test (Uid,log_date,fr,is_login,url) VALUES (%s,%s,%s,%s,%s)"
If i>5000:
Cursor.executemany (Sql,list)
Cnx.commit ()
Print ("Insert")
I=0
List.clear ()
I=i+1
If i>0:
Cursor.executemany (Sql,list)
Cnx.commit ()
Cnx.close ()
F.close ()
Print ("OK")
def listfiles (self):
D = Os.listdir ("e:/data/")
Return D
if __name__ = = "__main__":
ReadFile = ReadFile ()
Readfile.readlines ()