当前位置:网站首页>【作业】2022.5.28 将CSV写入数据库
【作业】2022.5.28 将CSV写入数据库
2022-06-30 03:27:00 【Sprite.Nym】
import pymysql
import csv
from pymysql.cursors import Cursor
# utf-8-sig ---> 带字节序签名的utf-8(utf-8 with BOM)
f = open('2018年北京积分落户数据.csv', 'r', encoding='utf-8-sig', newline='')
reader = csv.reader(f)
# 1. 创建连接
conn = pymysql.connect(host='localhost', port=3306,
user='guest', password='guest.618',
database='hrs', charset='utf8mb4')
try:
next(reader)
# 2. 获取游标对象
with conn.cursor() as cursor: # type: Cursor
# 3. 通过游标对象执行SQL语句
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 "北京积分落户数据"'
)
params = []
count = 0
for row in reader:
row[2] = row[2] + '-01'
params.append(row)
count += 1
if count % 1000 == 0:
# insert批处理,将数据批量写入数据库
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. 手动提交(让之前的操作生效)
conn.commit()
except pymysql.MySQLError as err:
# 4. 手动回滚(撤销之前的操作)
conn.rollback()
print(err)
finally:
# 5. 关闭连接
conn.close()
f.close()
边栏推荐
- [live broadcast notes 0629] Concurrent Programming II: lock
- How to realize remote collaborative office, keep this strategy!
- *Write a program to initialize a string object with a vector < char> container*/
- 51单片机的室内环境监测系统,MQ-2烟雾传感器和DHT11温湿度传感器,原理图,C编程和仿真
- Redis is used in Windows system
- 约瑟夫环 数学解法
- C#【高级篇】 C# 匿名方法【待补充Lambda表达式。。。】
- Utf8 error in Oracle migration of Jincang Kingbase database
- Number of students from junior college to Senior College (III)
- X Book 6.89 shield unidbg calling method
猜你喜欢

X Book 6.97 shield unidbg calling method

什么是外链和内链?

124 articles in total! Motianlun "high availability architecture" dry goods document sharing (including Oracle, mysql, PG)

Tidb 6.0: making Tso more efficient tidb Book rush

HOOK Native API

LitJson解析 生成json文件 读取json文件中的字典

Prompt learning a blood case caused by a space

Redis high concurrency distributed locks (learning summary)

Principle, advantages and disadvantages of three operating modes of dc/dc converter under light load
![C # [advanced part] C # multithreading](/img/16/2a7c477b4cee32d9ce1e543c9d4c7e.png)
C # [advanced part] C # multithreading
随机推荐
C#【高级篇】 C# 多线程
问题记录:fel_lib.c:26:10: fatal error: libusb.h: 没有那个文件或目录
Global and Chinese markets for active transdermal drug delivery devices 2022-2028: Research Report on technology, participants, trends, market size and share
Regular full match: the password consists of more than 8 digits, upper and lower case letters, and special characters
Auto. JS learning notes 16: save to the mobile phone by project, instead of saving a single JS file every time, which is convenient for debugging and packaging
Use common fileUpload to upload files
OP diode limit swing
Problem record: FEL_ lib. c:26:10: fatal error: libusb. h: There is no such file or directory
If you can tell whether the external stock index futures trading platform I am trading is formal and safe?
AppData文件夹下Local,Locallow和Roaming
Realization of BFS in C language by storing adjacency matrix of graph
Practical debugging skills
【常见问题】页面跨域和接口跨域
Personal PC installation software
Mysqldump principle
On the optimization and use of idea
DC/DC变换器轻载时三种工作模式的原理及优缺点
简单自定义MVC优化
Redis is used in Windows system
Chapter 2 control structure and function (programming problem)