python傳遞utf8 到c++_使用python往資料庫表造數
阿新 • • 發佈:2021-01-05
技術標籤:python傳遞utf8 到c++
使用python呼叫資料庫命令,往資料庫表插入大量的資料。
注意事項:關注索引,欄位需儘量覆蓋不同資料型別;
關注主鍵和外來鍵,資料儘量不要單一重複。
前置操作:
# 安裝pymysql外掛,python2使用pip,python3使用pip3命令pip/pip3 install pymysql# 安裝連線驅動器python -m pip install mysql-connector# 驗證安裝成功,沒有報錯import mysql.connector#python3匯入mysqlimport pymysql# 或者安裝mysqlclient外掛pip install mysqlclient# python2匯入mysqlimportMYSQLdb
1.使用pymysql庫插入批量資料:
"""建立測試表create table test_index (title varchar(10));"""from pymysql import connectdef main(): # 建立connection 連線 conn = connect( host="192.168.88.129", port=3306, database="jing_dong", user="root", password="123456", charset="utf8") # 獲得cursor物件 cursor = conn.cursor() # 插入10w條資料 for i in range(100000): cursor.execute("insert into test_index (id,title) values(0,'he-%d')" %i) # 提交資料 conn.commit()if __name__ == "__main__": main()
2.使用mysqlclient庫批量插入資料:
#! /usr/bin/python3# author: Torry Zhang# contact: [email protected]# datetime: 2020/9/5 22:01# software: IntelliJ IDEA"""python介面測試造數"""import MySQLdbdef main(): conn = MySQLdb.connect( host="192.168.88.129", port=3306, db="jing_dong", user="root", password="123456", charset="utf8" ) c = conn.cursor() for i in range(10000): c.execute("insert into test_index (id,title) values (0,'ha-%d')" %i) conn.commit() conn.close()if __name__ == "__main__": main()
檢視資料庫表結果: