如何从 SQLite3 格式转换为字典
How can I convert from SQLite3 format to dictionary
我如何将我的 SQLITE3 TABLE 转换为 python 字典,其中 table 列的名称和值被转换为字典的键和值。
如果有人遇到这个问题,我已经制作了一个包来解决这个问题..
这是它的功能
- Easy conversion between sqlite table and Python dictionary and vice-versa.
- Get values of a certain column in a Python list.
- Order your list ascending or descending.
- Insert any number of columns to your dict.
入门
我们首先连接我们的数据库
参考栏目
from aiosqlitedict.database import Connect
countriesDB = Connect("database.db", "user_id")
制作字典
字典应该在异步函数中。
async def some_func():
countries_data = await countriesDB.to_dict("my_table_name", 123, "col1_name", "col2_name", ...)
您可以插入任意数量的列,也可以通过指定获取全部
列名称为“*”
countries_data = await countriesDB.to_dict("my_table_name", 123, "*")
所以你现在已经对你的字典做了一些修改并且想要
再次将其导出为 sql 格式?
将 dict 转换为 sqlite table
async def some_func():
...
await countriesDB.to_sql("my_table_name", 123, countries_data)
但是如果您想要特定列的值列表怎么办?
Select方法
您可以获得特定列的所有值的列表。
country_names = await countriesDB.select("my_table_name", "col1_name")
使用 limit
参数限制您的选择。
country_names = await countriesDB.select("my_table_name", "col1_name", limit=10)
您还可以使用 ascending
参数来安排您的 list
and/or order_by
参数并指定特定列以相应地对列表进行排序。
country_names = await countriesDB.select("my_table_name", "col1_name", order_by="col2_name", ascending=False)
我如何将我的 SQLITE3 TABLE 转换为 python 字典,其中 table 列的名称和值被转换为字典的键和值。
如果有人遇到这个问题,我已经制作了一个包来解决这个问题..
这是它的功能
- Easy conversion between sqlite table and Python dictionary and vice-versa.
- Get values of a certain column in a Python list.
- Order your list ascending or descending.
- Insert any number of columns to your dict.
入门
我们首先连接我们的数据库 参考栏目
from aiosqlitedict.database import Connect
countriesDB = Connect("database.db", "user_id")
制作字典
字典应该在异步函数中。
async def some_func():
countries_data = await countriesDB.to_dict("my_table_name", 123, "col1_name", "col2_name", ...)
您可以插入任意数量的列,也可以通过指定获取全部 列名称为“*”
countries_data = await countriesDB.to_dict("my_table_name", 123, "*")
所以你现在已经对你的字典做了一些修改并且想要 再次将其导出为 sql 格式?
将 dict 转换为 sqlite table
async def some_func():
...
await countriesDB.to_sql("my_table_name", 123, countries_data)
但是如果您想要特定列的值列表怎么办?
Select方法
您可以获得特定列的所有值的列表。
country_names = await countriesDB.select("my_table_name", "col1_name")
使用 limit
参数限制您的选择。
country_names = await countriesDB.select("my_table_name", "col1_name", limit=10)
您还可以使用 ascending
参数来安排您的 list
and/or order_by
参数并指定特定列以相应地对列表进行排序。
country_names = await countriesDB.select("my_table_name", "col1_name", order_by="col2_name", ascending=False)