用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:

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM