python批量導入文件夾內文件到mysql數據庫腳本|大量數據入庫腳本

 


由於有大量txt數據要導入數據庫,上網搜了搜些方法,在這裏做個總結。

第一個想到的是方法是採用:LOAD DATA LOCAL INFILE "d:/www/t.txt"  ignore INTO TABLE xxindu_com  FIELDS TERMINATED BY ':' (col1, col2);
理由很簡單,這方法足夠快,然而我導入的文件有有一些行有亂碼問題,導致報錯。友情提示,如果列不同,是在最後指定列名。

後來我又找了找,終於找到了批量導入多文件夾內txt文件到mysql數據庫python腳本。
修改方法:

1.數據庫用戶名&密碼
2.字段名
3文件路徑
4.分割字符

#coding:utf-8
import MySQLdb
import os,sys,time


sgkint=7



def crea_table(sgkint):
    db=MySQLdb.connect(host='localhost',user='root',passwd='root',db='sgk1',charset="utf8") 
    cursor=db.cursor()
    try:
        #創建索引的語句
        cursor.execute ("drop table if exists www_"+str(sgkint)+";CREATE TABLE www_"+str(sgkint)+" (`id` bigint(10) NOT NULL AUTO_INCREMENT,`email` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,PRIMARY KEY (`id`) USING BTREE,INDEX `email`(`email`) USING BTREE) ENGINE = MyISAM AUTO_INCREMENT = 0 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;SET FOREIGN_KEY_CHECKS = 1")
        print u"創建表成功"
    except Exception, e:
            print e
    
    cursor.close()
    db.close()


f = []
for root,dirs,files in  os.walk('G:/www'):
    for name in files:
        if 'symbols' not in name:
            x= os.path.join(root,name)
            f.append(x)
            





def file_readlines(filename):
    f = open(filename)
    lines = f.readlines()
    f.close()
    return lines


def datas(sgkint,email,password):
    try:
        #print "INSERT INTO sgk_%s (email,password) VALUES ( '%s','%s')" % (str(sgkint),email,password)
        cursor.execute("INSERT INTO www_%s (email,password) VALUES ( '%s','%s')" % (str(sgkint),email,password))
    except Exception, e:
        print e
        


def chuli_data(li):
    x_array=[]
    li = li.strip('\n')
    try:
        if '----' in li:
            x_array=li.split("----")   
    except:
        print u"沒有找到分割字符,注意找到分割字符"
    
    return x_array




conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='sgk1',charset="utf8") 
cursor=conn.cursor()


#計算器


crea_table(sgkint)

count_f = len(f)
ii=1

for ff in f:
    lines = file_readlines(ff)
    count_line = len(lines)

    for li in lines:
        
        x_array=chuli_data(li)
        if x_array:
            email=MySQLdb.escape_string(x_array[0])
            password=MySQLdb.escape_string(x_array[1])
        
        
        print ff+" "+str(count_f)+" "+str(count_line)+" "+str(ii)+' '+str(sgkint)
        try:
            #print "INSERT INTO sgk_%s (email,password) VALUES ( '%s','%s')" % (str(sgkint),email,password)
            cursor.execute("INSERT INTO www_%s (email,password) VALUES ( '%s','%s')" % (str(sgkint),email,password))
          
        except Exception, e:
            print e
        #if cursor.lastrowid >=1000000:
        ii=ii+1

        if ii >100000000:
            sgkint=sgkint+1
           
            crea_table(sgkint)
            time.sleep(5)
            ii=1
        count_line=count_line-1
    count_f=count_f-1
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章