分享

python不用数据库,达到快速存储数据的方法

 Yy3318q 2023-03-24 发布于广西

在Python中,数据库并不是存储大量结构化数据的最简单解决方案。dataset提供了一个简单的抽象层,可以删除大多数直接的 SQL 语句,而无需完整的 ORM 模型,数据库可以像 JSON 文件或 NoSQL 存储一样使用。

dataset是用于快速存储数据的最简单方法。

特点

  • 自动架构:如果写入数据库中不存在的表或列,它将自动创建。
  • Upserts:创建或更新记录,具体取决于是否可以找到现有版本。
  • 用于简单查询的查询助手,例如all表中的行或distinct一组列中的所有值。
  • 兼容性:建立在SQLAlchemy之上,dataset适用于所有主要数据库,例如 SQLite、PostgreSQL 和 MySQL。

连接数据库

要连接到数据库,您需要通过其URL来识别它,以下是不同数据库后端的几个示例:
# 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')

存储数据

要存储一些数据,您需要获得对表的引用
# get a reference to the table 'user'table = db['user']

将数据存储在只需传递一个dict即可插入。不需要创建列名称和年龄——数据集会自动执行此操作:

# 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'))
更新现有条目也很容易:
table.update(dict(name='John Doe', age=47), ['name'])
使用第一列中的值作为第二个参数过滤器给出的过滤器列列表。如果您不想更新特定值,只需使用自动生成的id列。

使用

您可以将一组数据库更新分组到一个事务中。在这种情况下,所有更新都会立即提交,或者在异常情况下,所有更新都会被还原。通过上下文管理器支持事务,因此可以通过with 语句使用它们:
with dataset.connect() as tx: tx['user'].insert(dict(name='John Doe', age=46, country='China'))
通过调用方法获得相同的功能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()

也支持嵌套事务:

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'))

检查数据库和表

在处理未知数据库时,我们先检查它们的结构。找出数据库中存储了哪些表:
>>> print(db.tables)[u'user']

列出表中所有可用的列user

>>> print(db['user'].columns)[u'id', u'country', u'age', u'name', u'gender']
使用len()获得表中的总行数:
>>> print(len(db['user']))2

从表中读取数据

现在让我们从表中获取一些真实数据:

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()我们可以在一个或多个列中获取一组具有唯一值的行:

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

最后,您可以使用row_type参数来选择返回结果的数据类型:

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

现在内容将在对象中返回stuf(基本上,dict 其元素可以作为属性 ( item.name) 以及索引 ( item['name']) 访问的对象。

运行自定义 SQL 查询

当然,您使用数据库的主要原因是您希望使用 SQL 查询的全部功能。下面是你如何运行它们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,它允许以编程方式构建更复杂的查询:

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多