zoukankan      html  css  js  c++  java
  • 用python实现往数据库里面插入多条数据

    import random
    import MySQLdb
    
    def prepare_data():
        result = []
        type = ["a", "b", "c", "d", "e"]
    
        for i in range(50000):
            index = random.randint(0, 4)
            result.append((str(i), str(type[index]), str(i + 10), str(i)))
        return result
    
    def insert_data():
        db = MySQLdb.connect(host='10.124.207.xxx',
                             user='xxxx',
                             passwd='xxxxx',
                             db='usecase',
                             charset='utf8')
        sql = 'INSERT INTO test_table ( name, type, phone, addr) VALUES ( %s, %s, %s, %s);'
        cur = db.cursor()
        cur.executemany(sql, prepare_data())
        db.commit()
        db.close()
    
    if __name__ == '__main__':
        try:
            insert_data()
        except Exception as e:
    

      

  • 相关阅读:
    JDBC
    Listener监听器
    Filter过滤器
    Jstl标签库
    el表达式
    Ajax技术
    数据交换格式之
    MVC模式
    函数
    二维数组练习
  • 原文地址:https://www.cnblogs.com/onelove1/p/14582548.html
Copyright © 2011-2022 走看看