python 读取本地文件批量插入mysql

 

Uin_phone.txt 本地文件内容 有1000条,这里只是展示前几条,供参考
133584752
133584759
133584764
133584773
133584775
133584777
133584780
133584781
133584785
133584788

 

代码展示

#!/usr/bin/env python

import MySQLdb  #导入mysql模块
db = MySQLdb.connect('10.99.16.12','root','20170822','Uin_phone')
cursor = db.cursor() # 使用cursor()方法获取操作游标

file_object = open('/root/Uin_phone.txt')  #读取本地文件
list_all_lines = file_object.readlines()        #读取文件每一行内容


for line in list_all_lines:
    insert_Uin = "insert into uin_phone values('%s')" % (line) 
    cursor.execute(insert_Uin)  #开始mysql插入
    db.commit()  #提交到数据库执行

file_object.close() #关闭已打开的文件
db.close  #关闭MySQL连接

 

posted @ 2017-08-22 18:34  Ray雷  阅读(644)  评论(0)    收藏  举报