oursql
跳转到导航
跳转到搜索
Python 的 MySQL 驱动,支持 Python 3,使用 MySQL 客户端库。
代码示例
#!/usr/bin/env python
import sys
from urllib.parse import urlsplit
import oursql
# simple URL parsing
u = urlsplit(db_url)
login, host = u.netloc.split('@')
if ':' in host:
host, port = host.split(':')
port = int(port)
else:
port = 0
user, passwd = login.split(':')
db = u.path.strip('/')
conn = oursql.connect(host=host, user=user, passwd=passwd, db=db, port=port)
with conn as cur:
# diable autocommit. IMPORTANT!
# 多条语句要单独执行,不可像 psycopg2 那样合并在一起
cur.execute('set autocommit = 0')
cur.execute('set names utf8mb4')
def GetData():
for l in sys.stdin:
yield l, func(l)
with conn as c:
c.executemany('''
insert ignore into table_name
(val1, val2) values (?, ?)
''', GetData())