zoukankan      html  css  js  c++  java
  • 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连接
  • 相关阅读:
    style实例
    背景小实例
    html表单与输入实例
    列表小知识
    表格的使用
    框架的奥秘
    链接那档子事
    判断网络是否连通
    Python3编码规范
    CentOS 7 安装 Docker-Compose
  • 原文地址:https://www.cnblogs.com/rayong/p/7413026.html
Copyright © 2011-2022 走看看