Python读取数据库百万级数据存入另外的数据库(python读取数据库

http://www.itjxue.com  2023-04-01 12:31  来源:未知  点击次数: 

从数据库里python获取数据存到本地数据库

python项目中从接口获取数据并存入本地数据库

首先用postman测试接口

根据请求方式将数据存入数据库中

首先用postman测试接口

通过url,选择相应的请求方式,头部,数据格式,点击send看能否获取数据

根据请求方式将数据存入数据库中

下面是post请求方式def get() URL = '' HEADERS = {'Content-Type': 'application/json'} JSON = {} response = request.post(URL,headers=HEADERS,json=JSON) #json.loads()用于将str类型的数据转成dict jsondata = json.load(response.txt) for i in jsondata: date1 = i[data] type1 = i[type] ... #拼接sql语句 sql="" conn=MySQLdb.connect(host="localhost",user="root",passwd="sa",db="mytable")  cursor=conn.cursor()  ursor.execute(sql)

Python存200w数据到数据库需要多久

Python存200w数据到数据库需要474秒,因为正常的三万八千条数据仅需要9秒,以此类推出200万需要的时间。

【python存数据库速度】

1、需要从文本中读取三万条数据写入mysql数据库,文件中为用@分割的sql语句,但是在读取的过程中发现速度过慢,三万八千条数据需要220秒,

2、经测试发现,影响速度的主要原因是commit(),因为没过几秒提交一次即可,但是因为提交的字符长度有限制,所以要设置一个合理的时间读取。

3、更改后,写入三万八千条数据仅需要9秒

python 读取本地数据然后插入到另一个数据库中

class?Buffer(object):

????MAXSIZE?=?8192

????def?__init__(self,?conn,?sql):

????????self.conn?=?conn

????????self.sql?=?sql

????????self.buffer?=?[]

????def?append(self,?data):

????????self.buffer.append(data)

????????if?len(self.buffer)??self.MAXSIZE:

????????????self.flush()

????def?flush(self):

????????data,?self.buffer?=?self.buffer,?[]

????????curr?=?self.conn.cursor()

????????curr.executemany(self.sql,?data)

????????self.conn.commit()

#?here?are?your?code?for?init?database?connect?conn_src?and?conn_store...

buff?=?Buffer(conn_store,?"insert?into?sybase_user?values?(%s,?%s)")

sql_query?=?"select?a.id,?a.name?from?user_info?a?where?a.id=%s"

curr_src?=?conn_src.cursor()

curr_src.execute(sql_query,?'0001')

for?row?in?curr_src:

????buff.append(row)

buff.flush()

(责任编辑:IT教学网)

更多

推荐新手入门文章