python文件读写并使用mysql批量插入示例分享(python操作mysql)
# -*- coding: utf-8 -*-
'''
Created on 2013年12月9日@author: hhdys
'''import os
import mysql.connectorconfig = {
'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()
您可能感兴趣的文章:
- python文件读写并使用mysql批量插入示例分享(python操作mysql)
- 两个使用Python脚本操作文件的小示例分享
- 两个使用Python脚本操作文件的小示例分享
- 关于使用python3操作Navicat for mysql 插入数据为空的几个情况
- Python3使用pandas模块读写excel操作示例
- python使用codecs模块进行文件操作-读写中英文字符
- 使用python删除nginx缓存文件示例(python文件操作)
- Python实现爬虫抓取与读写、追加到excel文件操作示例
- Python使用Pickle库实现读写序列操作示例
- python读写文件操作示例程序
- 使用python统计文件行数示例分享
- Python文件读写保存操作的示例代码
- python使用codecs模块进行文件操作-读写中英文字符
- Python实现的简单读写csv文件操作示例
- 使用python删除nginx缓存文件示例(python文件操作)
- python读写文件操作示例程序
- python使用codecs模块进行文件操作-读写中英文字符
- 使用Python读写及压缩和解压缩文件的示例
- 数据库操作_连接SQL Server数据库示例;连接ACCESS数据库;连接到 Oracle 数据库示例;SqlCommand 执行SQL命令示例;SqlDataReader 读取数据示例;使用DataAdapter填充数据到DataSet;使用DataTable存储数据库表;将数据库数据填充到 XML 文件;10 使用带输入参数的存储过程;11 使用带输入、输出参数的存储过程示;12 获得数据库中表的数目和名称;13 保存图片到SQL Server数据库示例;14 获得插入记录标识号;Exce
- python列表操作使用示例分享