python复制mysql表到另外的主机,Python将MySQL表复制到SQLite3

I've got a MySQL table with about ~10m rows. I created a parallel schema in SQLite3, and I'd like to copy the table somehow. Using Python seems like an acceptable solution, but this way --

# ...

mysqlcursor.execute('SELECT * FROM tbl')

rows = mysqlcursor.fetchall() # or mysqlcursor.fetchone()

for row in rows:

# ... insert row via sqlite3 cursor

...is incredibly slow (hangs at .execute(), I wouldn't know for how long).

I'd only have to do this once, so I don't mind if it takes a couple of hours, but is there a different way to do this? Using a different tool rather than Python is also acceptable.

解决方案

You don't show exactly how you insert rows, but you mention execute().

You might try executemany()* instead.

For example:

import sqlite3

conn = sqlite3.connect('mydb')

c = conn.cursor()

# one '?' placeholder for each column you're inserting

# "rows" needs to be a sequence of values, e.g. ((1,'a'), (2,'b'), (3,'c'))

c.executemany("INSERT INTO tbl VALUES (?,?);", rows)

conn.commit()

*executemany() as described in the Python DB-API:

.executemany(operation,seq_of_parameters)

Prepare a database operation (query or

command) and then execute it against

all parameter sequences or mappings

found in the sequence

seq_of_parameters.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值