python通用數(shù)據(jù)庫操作工具 pydbclib的使用簡介
pydbclib是一個通用的python關系型數(shù)據(jù)庫操作工具包,使用統(tǒng)一的接口操作各種關系型數(shù)據(jù)庫(如 oracle、mysql、postgres、hive、impala等)進行增刪改查,它是對各個python數(shù)據(jù)庫連接驅動包(如sqlalchemy、pymysql、cx_Oracle、pyhive、pyodbc、impala等)的封裝,依照python最簡原則SQL占位符統(tǒng)一成 ’:[name]’ 這一種形式,這點和sqlalchemy是一樣的
安裝pip3 install pydbclib簡單使用
看下簡單的查詢示例
from pydbclib import connect# 使用with上下文,可以自動提交,自動關閉連接with connect('sqlite:///:memory:') as db: db.execute(’create table foo(a integer, b varchar(20))’) # 統(tǒng)一使用’:[name]’形式的SQL的占位符 db.execute('insert into foo(a,b) values(:a,:b)', [{'a': 1, 'b': 'one'}]*4) print(db.read('select * from foo').get_one()) print(db.read('select * from foo').get_all()) print(db.read('select * from foo').to_df()) db.get_table('foo').insert({'a': 2, 'b': 'two'}) print(db.get_table('foo').find_one({'a': 2})) print(db.get_table('foo').find().get_all()) print(db.get_table('foo').find().to_df())
查詢結果記錄是以字典形式展現(xiàn),向庫里寫入記錄也是字典形式,如果要使用原生元祖形式,查詢函數(shù)read里添加as_dict=False參數(shù)
接口文檔數(shù)據(jù)庫連接,更多常用數(shù)據(jù)庫連接方式參考文章結尾
# connect函數(shù)有個driver參數(shù)決定你是通過哪個數(shù)據(jù)庫驅動包去連接的# driver參數(shù)默認值是sqlalchemy,即通過sqlalchemy驅動包連接數(shù)據(jù)庫>>> db = pydbclib.connect('sqlite:///:memory:')>>> db = pydbclib.connect(':memory:', driver=’sqlite3’)# 也可以傳入驅動包連接對象>>> import sqlite3>>> db = pydbclib.connect(driver=sqlite3.connect(':memory:'))>>> from sqlalchemy import create_engine>>> db = pydbclib.connect(driver=create_engine('sqlite:///:memory:'))原生SQL接口
1. 使用execute方法執(zhí)行SQL,和各數(shù)據(jù)庫連接包基本一致,不同點是它既可以單條執(zhí)行,也可以批量執(zhí)行(相當于executemany),另外該方法的SQL占位符是’:[name]’形式
>>> record = {'a': 1, 'b': 'one'}>>> db.execute(’create table foo(a integer, b varchar(20))’)# 插入單條記錄,結果返回影響行數(shù)>>> db.execute('insert into foo(a,b) values(:a,:b)', record)1# 插入多條記錄>>> db.execute('insert into foo(a,b) values(:a,:b)', [record, record])2
2. 查詢數(shù)據(jù)
# 查詢結果只返回一條記錄>>> db.read_one('select * from foo'){’a’: 1, ’b’: ’one’}#read返回迭代器類型,用get方法獲取前幾條記錄,使用map對每條記錄進行數(shù)據(jù)清洗>>> db.read('select * from foo').map(lambda x: {f'foo.{k}': v for k,v in x.items()}).get(2)# as_dict=False返回原生元祖記錄>>> db.read('select * from foo', as_dict=False).get(2)[(1, ’one’), (1, ’one’)]# 也可以直接for遍歷>>> for r in db.read('select * from foo'):... print(r)... {’a’: 1, ’b’: ’one’}{’a’: 1, ’b’: ’one’}{’a’: 1, ’b’: ’one’}# 轉換成pandas dataframe對象, 前提已經安裝了pandas>>> db.read('select * from foo').to_df() a b0 1 one1 1 one2 1 one
3. 提交、回滾、關閉連接
>>> db.rollback()>>> db.commit()>>> db.close()表級別操作的SQL接口封裝
1. 插入記錄
# 插入單條和插入多條,輸入參數(shù)字典的鍵值必須和表中字段同名>>> db.get_table('foo').insert({'a': 1, 'b': 'one'})1>>> db.get_table('foo').insert([{'a': 1, 'b': 'one'}]*10)10
2. 查詢記錄
# 查詢字段a=1第一條記錄>>> db.get_table('foo').find_one({'a': 1}){’a’: 1, ’b’: ’one’}# 也可以直接寫成sql條件表達式,其他接口的條件參數(shù)類似都可以是表達式>>> db.get_table('foo').find_one('a=1'){’a’: 1, ’b’: ’one’}# 查詢字段a=1所有記錄,find返回迭代器對象同上面read方法>>> db.get_table('foo').find({'a': 1}).get_all()[{’a’: 1, ’b’: ’one’},...{’a’: 1, ’b’: ’one’}]
3. 更新記錄
# 將a=1那條記錄的b字段值更新為'first'>>> db.get_table('foo').update({'a': 1}, {'b': 'first'})11>>> db.get_table('foo').find({'a': 1}).get_one(){’a’: 1, ’b’: ’first’}
4. 刪除記錄
# 將a=1那條記錄刪除>>> db.get_table('foo').delete({'a': 1})11>>> db.get_table('foo').find({'a': 1}).get_all()[]常用數(shù)據(jù)庫連接
1. Common Driver
# 使用普通數(shù)據(jù)庫驅動連接,driver參數(shù)指定驅動包名稱# 例如pymysql包driver=’pymysql’,connect函數(shù)其余的參數(shù)和driver參數(shù)指定的包的創(chuàng)建連接參數(shù)一致# 連接mysqldb = pydbclib.connect(user='user', password='password', database='test', driver='pymysql')# 連接oracledb = pydbclib.connect(’user/password@local:1521/xe’, driver='cx_Oracle')# 通過odbc方式連接db = pydbclib.connect(’DSN=mysqldb;UID=user;PWD=password’, driver='pyodbc') # 通過已有驅動連接方式連接import pymysqlcon = pymysql.connect(user='user', password='password', database='test')db = pydbclib.connect(driver=con)
2. Sqlalchemy Driver
# 使用Sqlalchemy包來連接數(shù)據(jù)庫,drvier參數(shù)默認為’sqlalchemy’# 連接oracledb = pydbclib.connect('oracle://user:password@local:1521/xe')# 連接mysqldb = pydbclib.connect('mysql+pyodbc://:@mysqldb')# 通過已有engine連接from sqlalchemy import create_engineengine = create_engine('mysql+pymysql://user:password@localhost:3306/test')db = pydbclib.connect(driver=engine)
使用過程中有任何疑問,歡迎評論交流項目地址pydbclib
以上就是python通用數(shù)據(jù)庫操作工具 pydbclib的使用簡介的詳細內容,更多關于python 數(shù)據(jù)庫操作工具pydbclib的資料請關注好吧啦網其它相關文章!
相關文章:
