免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
python不用數(shù)據(jù)庫,達(dá)到快速存儲(chǔ)數(shù)據(jù)的方法
userphoto

2023.03.24 廣西

關(guān)注

在Python中,數(shù)據(jù)庫并不是存儲(chǔ)大量結(jié)構(gòu)化數(shù)據(jù)的最簡單解決方案。dataset提供了一個(gè)簡單的抽象層,可以刪除大多數(shù)直接的 SQL 語句,而無需完整的 ORM 模型,數(shù)據(jù)庫可以像 JSON 文件或 NoSQL 存儲(chǔ)一樣使用。

dataset是用于快速存儲(chǔ)數(shù)據(jù)的最簡單方法。

特點(diǎn)

  • 自動(dòng)架構(gòu):如果寫入數(shù)據(jù)庫中不存在的表或列,它將自動(dòng)創(chuàng)建。
  • Upserts:創(chuàng)建或更新記錄,具體取決于是否可以找到現(xiàn)有版本。
  • 用于簡單查詢的查詢助手,例如all表中的行或distinct一組列中的所有值。
  • 兼容性:建立在SQLAlchemy之上,dataset適用于所有主要數(shù)據(jù)庫,例如 SQLite、PostgreSQL 和 MySQL。

連接數(shù)據(jù)庫

要連接到數(shù)據(jù)庫,您需要通過其URL來識(shí)別它,以下是不同數(shù)據(jù)庫后端的幾個(gè)示例:
# connecting to a SQLite databasedb = dataset.connect('sqlite:///mydatabase.db')
# connecting to a MySQL database with user and passworddb = dataset.connect('mysql://user:password@localhost/mydatabase')
# connecting to a PostgreSQL databasedb = dataset.connect('postgresql://scott:tiger@localhost:5432/mydatabase')

存儲(chǔ)數(shù)據(jù)

要存儲(chǔ)一些數(shù)據(jù),您需要獲得對(duì)表的引用
# get a reference to the table 'user'table = db['user']

將數(shù)據(jù)存儲(chǔ)在只需傳遞一個(gè)dict即可插入。不需要?jiǎng)?chuàng)建列名稱和年齡——數(shù)據(jù)集會(huì)自動(dòng)執(zhí)行此操作:

# Insert a new record.table.insert(dict(name='John Doe', age=46, country='China'))
# dataset will create 'missing' columns any time you insert a dict with an unknown keytable.insert(dict(name='Jane Doe', age=37, country='France', gender='female'))
更新現(xiàn)有條目也很容易:
table.update(dict(name='John Doe', age=47), ['name'])
使用第一列中的值作為第二個(gè)參數(shù)過濾器給出的過濾器列列表。如果您不想更新特定值,只需使用自動(dòng)生成的id列。

使用

您可以將一組數(shù)據(jù)庫更新分組到一個(gè)事務(wù)中。在這種情況下,所有更新都會(huì)立即提交,或者在異常情況下,所有更新都會(huì)被還原。通過上下文管理器支持事務(wù),因此可以通過with 語句使用它們:
with dataset.connect() as tx: tx['user'].insert(dict(name='John Doe', age=46, country='China'))
通過調(diào)用方法獲得相同的功能begin(), commit()rollback() 明確:
db = dataset.connect()db.begin()try:    db['user'].insert(dict(name='John Doe', age=46, country='China'))    db.commit()except:    db.rollback()

也支持嵌套事務(wù):

db = dataset.connect()with db as tx1:    tx1['user'].insert(dict(name='John Doe', age=46, country='China'))    with db as tx2:        tx2['user'].insert(dict(name='Jane Doe', age=37, country='France', gender='female'))

檢查數(shù)據(jù)庫和表

在處理未知數(shù)據(jù)庫時(shí),我們先檢查它們的結(jié)構(gòu)。找出數(shù)據(jù)庫中存儲(chǔ)了哪些表:
>>> print(db.tables)[u'user']

列出表中所有可用的列user

>>> print(db['user'].columns)[u'id', u'country', u'age', u'name', u'gender']
使用len()獲得表中的總行數(shù):
>>> print(len(db['user']))2

從表中讀取數(shù)據(jù)

現(xiàn)在讓我們從表中獲取一些真實(shí)數(shù)據(jù):

users = db['user'].all()

如果我們只是想遍歷表中的所有行,我們可以省略all():

for user in db['user']:   print(user['age'])

我們可以使用find()搜索特定條目find_one():

# All users from Chinachinese_users = table.find(country='China')
# Get a specific userjohn = table.find_one(name='John Doe')
# Find multiple at oncewinners = table.find(id=[1, 3, 7])
# Find by comparison operatorelderly_users = table.find(age={'>=': 70})possible_customers = table.find(age={'between': [21, 80]})
# Use the underlying SQLAlchemy directlyelderly_users = table.find(table.table.columns.age >= 70)

使用 distinct()我們可以在一個(gè)或多個(gè)列中獲取一組具有唯一值的行:

# Get one user per countrydb['user'].distinct('country')

最后,您可以使用row_type參數(shù)來選擇返回結(jié)果的數(shù)據(jù)類型:

import datasetfrom stuf import stuf
db = dataset.connect('sqlite:///mydatabase.db', row_type=stuf)

現(xiàn)在內(nèi)容將在對(duì)象中返回stuf(基本上,dict 其元素可以作為屬性 ( item.name) 以及索引 ( item['name']) 訪問的對(duì)象。

運(yùn)行自定義 SQL 查詢

當(dāng)然,您使用數(shù)據(jù)庫的主要原因是您希望使用 SQL 查詢的全部功能。下面是你如何運(yùn)行它們dataset:

result = db.query('SELECT country, COUNT(*) c FROM user GROUP BY country')for row in result:   print(row['country'], row['c'])

該query()方法還可用于訪問底層的SQLAlchemy 核心 API,它允許以編程方式構(gòu)建更復(fù)雜的查詢:

table = db['user'].tablestatement = table.select(table.c.name.like('%John%'))result = db.query(statement)

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
國產(chǎn) DB 將 SQL 和 NoSQL 融合在一起了!
MongoDB的基本使用
數(shù)據(jù)庫的操作
MongoDB聚合(aggregate)
sql server中數(shù)據(jù)庫之間復(fù)制表
python tinyDB 簡單使用
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服