Knowledge:
-
Notice the statement: mydb.commit(). It is required to make the changes, otherwise no changes are made to the table
-
To insert multiple rows into a table, use the executemany() method.
-
The second parameter of the executemany() method is a list of tuples or lists, containing the data you want to insert.
import
xlrd
import
pymysql
import
sys
import
datetime
def
mysql_link
(
database_name
)
:
try
:
db
=
pymysql
.
connect
(
host
=
'localhost'
,
user
=
'root'
,
password
=
'888888'
,
database
=
database_name
)
return
db
except
Exception
as
e
:
print
(
e
)
print
(
'数据库连接失败'
)
def
read_excel
(
wb_name
)
:
try
:
wb
=
xlrd
.
open_workbook
(
wb_name
)
return
wb
except
Exception
as
e
:
print
(
e
)
print
(
'打开excel文件失败'
)
def
upload_data
(
db_name
,
table_name
,
wb_name
)
:
db
=
mysql_link
(
db_name
)
cursor
=
db
.
cursor
(
)
all_data
=
[
]
wb
=
read_excel
(
wb_name
)
shts
=
wb
.
sheet_names
(
)
print
(
shts
)
for
sht
in
shts
:
sh
=
wb
.
sheet_by_name
(
sht
)
row_num
=
sh
.
nrows
num
=
0
for
i
in
range
(
1
,
row_num
)
:
row_data
=
sh
.
row_values
(
i
)
value
=
[
row_data
[
0
]
,
row_data
[
1
]
,
row_data
[
2
]
,
row_data
[
3
]
,
row_data
[
4
]
,
row_data
[
5
]
]
all_data
.
append
(
value
)
sql
=
f"""insert into
{
table_name
}
(salesdate,product_id,salesRegion,salesPerson,SalesQty,salesAmount)
values(%s,%s,%s,%s,%s,%s)"""
print
(
all_data
)
cursor
.
executemany
(
sql
,
all_data
)
db
.
commit
(
)
print
(
cursor
.
rowcount
,
"was inserted in total."
)
cursor
.
close
(
)
db
.
close
(
)
if
__name__
==
"__main__"
:
upload_data
(
'ods_testdb'
,
"t_sales"
,
'销售数据.xls'
)
#OperationalError: (1292, “Incorrect date value: ‘44400’ for column ‘salesdate’ at row 1”)
#录入数据需要日期数据需要文本?— NO
#excel数据格式要遵循常规格式 !!!!
去MySql中创建库表:
CREATE TABLE t_customers(
`name` VARCHAR(100) not null,
address VARCHAR(100)
import
mysql
.
connector
db
=
mysql
.
connector
.
connect
(
host
=
"localhost"
,
user
=
"root"
,
password
=
"888888"
,
database
=
"ods_testdb"
cursor
=
mydb
.
cursor
(
)
sql
=
"insert into t_customers(name, address)values(%s, %s)"
val
=
[
(
'Peter'
,
'Lowstreet 4'
)
,
(
'Amy'
,
'Apple st 652'
)
,
(
'Hannah'
,
'Mountain 21'
)
,
(
'Michael'
,
'Valley 345'
)
,
(
'Sandy'
,
'Ocean blvd 2'
)
,
(
'Betty'
,
'Green Grass 1'
)
,
(
'Richard'
,
'Sky st 331'
)
,
(
'Susan'
,
'One way 98'
)
,
(
'Vicky'
,
'Yellow Garden 2'
)
,
(
'Ben'
,
'Park Lane 38'
)
,
(
'William'
,
'Central st 954'
)
,
(
'Chuck'
,
'Main Road 989'
)
,
(
'Viola'
,
'Sideway 1633'
)
]
cursor
.
executemany
(
sql
,
val
)
db
.
commit
(
)
db
.
close
(
)
print
(
cursor
.
rowcount
,
"was inserted."
)
def sfzh_sql_insert():
conn = py
mysql
.connect(host='172.31.100.11', user='root', password="root",
我用sqlalchemy orm方式连接
数据库
,处理数据。一台机器上程序运行正常。但是另一台机器上就报错py
mysql
.err.OperationalError: (1040, 'Too many connections')。
首先我确定连接方式肯定没错。程序也没错。那猜测是两台机器上
mysql
的配置不同。百度之后,获得答案如下:以下是转载别人的答案。
py
mysql
.err.OperationalError: (1040, 'Too many connections') 超出连接
数据库
最大连接数所致,
解析完爬取下来的数据之后的下一个步骤就是如何高效地储存爬取下来的数据,本文主要利用
mySQL
数据库
来储存数据,所以这涉及到如何使用
python
连接
mySQL
数据库
1.pyton连接
数据库
需要先安装py
mysql
模块:pip install py
mysql
2.安装完成后导入py
mysql
模块:import py
mysql
之后
python
连接
数据库
主要分五个步骤:
1.连接
数据库
2.创建游标对象
3.对
数据库
进行增删改查
4.关闭游标
5.关闭连接
代码如下:
import py
mysql
Python
系列之:使用
python
一次性往
mysql
数据库
创建多张
表
并插入数据,使用presto查询数据再用pyspark往多张
表
中插入数据一、安装
mysql
-connector二、代码批量创建多张
表
三、一次性往多张
表
插入数据四、presto查询数据再用pyspark往多张
表
中插入数据
一、安装
mysql
-connector
pip3 install
mysql
-connector
二、代码批量创建多张
表
mydb =
mysql
.connector.connect(
host="10.10.
mysql
多
表
插入,可以一个一个语句插入,但是效率比较低。可以用事务
操作
,之前没用过。今天尝试写了一下,没有什么难度,单纯为记录。
本次实现的是三个
表
插入,其中table1中id自增,table2,table3需要使用table1中的id。
table1 结构 id, model
table2结构id,pname,age
table3结构id,score1,score2
mysql
事务开始可以使用BEGIN,也可以使用START TRANSACTION。
获取本次
操作
的id用LAST_INSERT_ID()
import py
mysql
l链接
数据库
并执行sql语句
conn = py
mysql
.connect(host='10.245.251.61',user='stashlog',password='stashlog123',database='exos_perf_auto')#创建
数据库
连接
cursor = conn.cursor()#创建游标
sql=''#要执行的sql语句
cursor.execute(sql)#提交sql语句
conn.commit()#提交缓存
cursor.close()#关
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name
LEFT JOIN table3 ON table1.column_name = table3.column_name
LEFT JOIN table4 ON table1.column_name = table4.column_name
其中,`table1`是需要查询的主
表
,`table2`, `table3`, `table4`等是需要左联的
表
,`column_name`是两个
表
共有的列名,用于关联两个
表
的数据。
在实际应用中,可以根据具体的业务需求来进行左联多张
表
的
操作
,需要注意的是,如果多张
表
之间的数据关联复杂,可能会导致查询效率下降,因此需要仔细设计和优化SQL语句。