当前位置:网站首页>[operation] write CSV to database on May 28, 2022
[operation] write CSV to database on May 28, 2022
2022-06-30 03:41:00 【Sprite. Nym】
import pymysql
import csv
from pymysql.cursors import Cursor
# utf-8-sig ---> Signed with byte order utf-8(utf-8 with BOM)
f = open('2018 Beijing points settlement data in .csv', 'r', encoding='utf-8-sig', newline='')
reader = csv.reader(f)
# 1. Create connection
conn = pymysql.connect(host='localhost', port=3306,
user='guest', password='guest.618',
database='hrs', charset='utf8mb4')
try:
next(reader)
# 2. Get cursor object
with conn.cursor() as cursor: # type: Cursor
# 3. Execute through cursor objects SQL sentence
cursor.execute(
'create table if not exists tb_houses'
'('
'id int unsigned not null,'
'name varchar(10) not null,'
'birthday date not null,'
'company varchar(50) not null,'
'score decimal(5,2) not null,'
'primary key (id)'
')engine=innodb comment " Beijing integral settlement data "'
)
params = []
count = 0
for row in reader:
row[2] = row[2] + '-01'
params.append(row)
count += 1
if count % 1000 == 0:
# insert The batch , Write the data to the database in batches
cursor.executemany(
'insert into tb_houses (id, name, birthday, company, score) '
'values (%s, %s, %s, %s, %s)',
params
)
params = []
cursor.executemany(
'insert into tb_houses (id, name, birthday, company, score) '
'values (%s, %s, %s, %s, %s)',
params)
# 4. Manual submission ( Let the previous operation take effect )
conn.commit()
except pymysql.MySQLError as err:
# 4. Manual rollback ( Undo the previous operation )
conn.rollback()
print(err)
finally:
# 5. Close the connection
conn.close()
f.close()
边栏推荐
- yarn的安装和使用
- A GPU approach to particle physics
- Selenium environment installation, 8 elements positioning --01
- (04).NET MAUI实战 MVVM
- C # [advanced part] C # multithreading
- 利用反射整合ViewBinding和ViewHolder
- How to use Jersey to get the complete rest request body- How to get full REST request body using Jersey?
- Using virtual environments in jupyter notebook
- Mysql性能优化(5):主从同步原理与实现
- 【云原生】AI云开发平台——AI Model Foundry介绍(开发者可免费体验AI训练模型)
猜你喜欢
Practical debugging skills
Mathematical solution of Joseph Ring
专升本语文资源整理
How to view Tencent's 2022 school recruitment salary, the total contract of cabbage is 40W?
(03).NET MAUI实战 基础控件
1151_ Makefile learning_ Static matching pattern rules in makefile
QT中foreach的使用
December2020 - true questions and analysis of C language (Level 2) in the youth level examination of the Electronic Society
ThingsBoard教程(二三):在规则链中计算二个设备的温度差
SDS understanding in redis
随机推荐
QT中foreach的使用
华为云原生——数据开发与DataFactory
Are you a "social bull" or a "social terrorist" in the interview?
Some common functions and precautions
Openssl3.0 learning 22 provider decoder
ReSharper 7. Can X be used with vs2013 preview? [off] - can resharper 7 x be used with VS2013 preview? [closed]
Note: load the configuration file as a byte stream and get the resources in it
Redis中的SDS理解
Node-RED系列(二八):基于OPC UA节点与西门子PLC进行通讯
Installation and use of yarn
C#【高级篇】 C# 多线程
UML图与List集合
SDS understanding in redis
C [advanced] C interface
Global and Chinese markets for active transdermal drug delivery devices 2022-2028: Research Report on technology, participants, trends, market size and share
Wang Shuang - assembly language learning summary
[operation] getting started with MySQL on May 23, 2022
Hash design and memory saving data structure design in redis
Ubuntu20.04 PostgreSQL 14 installation configuration record
51单片机的室内环境监测系统,MQ-2烟雾传感器和DHT11温湿度传感器,原理图,C编程和仿真