国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python文件讀寫并使用mysql批量插入示例分享(python操作mysql)

2019-11-25 18:30:59
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

# -*- coding: utf-8 -*-
'''
Created on 2013年12月9日

@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("插入")
                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()

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 张家港市| 乐清市| 临沧市| 镇巴县| 高雄市| 新宾| 宁津县| 晋城| 衡南县| 盐城市| 丁青县| 即墨市| 绥化市| 无为县| 廉江市| 英德市| 新化县| 平定县| 元朗区| 镇巴县| 双桥区| 海伦市| 邵阳市| 安塞县| 嘉禾县| 濉溪县| 仪陇县| 客服| 永仁县| 兴化市| 桂东县| 莲花县| 江山市| 通州市| 鞍山市| 鄂尔多斯市| 上犹县| 武威市| 吴江市| 丹东市| 汪清县|