亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

python通用數(shù)據(jù)庫操作工具 pydbclib的使用簡介

瀏覽:2日期:2022-07-01 15:11:27

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的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 天天久久| 欧美特级黄| 国产精品久久久久久五月尺 | 久久久久久88色偷偷 | 亚洲精品一区二区深夜福利 | 色综合天天综合高清影视 | 午夜亚洲精品久久久久 | 欧美成人国产一区二区 | 黄色二级毛片 | 网址黄色 | 免费高清毛片在线播放视频 | 黑人黑粗硬视频 | 国产精品一区在线观看你懂的 | 欧美特级一级毛片 | 国产三级精品在线观看 | 一级日本强免费 | 女人被狂躁视频免费网站 | 成人亚洲欧美 | 风间中文字幕亚洲一区中文馆 | 毛片a级放荡的护士hd | 日韩中文字幕免费在线观看 | 亚洲国产精品免费视频 | 香港性特级黄录像片 | 国产美女一区 | 欧美一级特黄aa大片 | jiucao在线看片www | 男人看片网址 | 手机看片国产在线 | 精品国产福利第一区二区三区 | 亚洲综合日韩 | 麻豆久久精品免费看国产 | 久久er热视频在这里精品 | 亚洲人成人网毛片在线播放 | 国产91网站在线观看 | 亚洲图色在线 | 1769国内精品视频在线观看 | 在线日韩一区 | 亚洲午夜精品国产电影在线观看 | 日韩高清中文字幕 | 亚洲黄色网络 | 国产精品国产三级国产无毒 |