二、数据批量查询
select_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import pymysql
import pandas as pd
import numpy as np
def get_tags():
# 连接数据库,地址,端口,用户名,密码,数据库名称,数据格式
conn = pymysql.connect(host = 'xxx.xxx.xxx.xxx' ,port = 3307 ,user = 'root' ,passwd = '123456' ,db = 'xxxx' ,charset = 'utf8' )
cur = conn.cursor()
# 表cf_users中获取所有用户id
sql = 'select id from cf_tags where id between 204 and 298'
# 将user_id列转成列表输出
df = pd.read_sql(sql,con = conn)
# 先使用array()将DataFrame转换一下
df1 = np.array(df)
# 再将转换后的数据用tolist()转成列表
df2 = df1.tolist()
# cur.execute(sql)
# data = cur.fetchone()
# print(df)
# print(df1)
# print(df2)
return df2
conn.close()
|
三、批量更新数据
select_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import pymysql
import pandas as pd
import numpy as np
def get_tags():
# 连接数据库,地址,端口,用户名,密码,数据库名称,数据格式
conn = pymysql.connect(host = 'xxx.xxx.xxx.xxx' ,port = 3307 ,user = 'root' ,passwd = '123456' ,db = 'xxxx' ,charset = 'utf8' )
cur = conn.cursor()
# 表cf_users中获取所有用户id
sql = 'select id from cf_tags where id between 204 and 298'
# 将user_id列转成列表输出
df = pd.read_sql(sql,con = conn)
# 先使用array()将DataFrame转换一下
df1 = np.array(df)
# 再将转换后的数据用tolist()转成列表
df2 = df1.tolist()
# cur.execute(sql)
# data = cur.fetchone()
# print(df)
# print(df1)
# print(df2)
return df2
conn.close()
|
评论(0)