'How can I convert from SQLite3 format to dictionary

How can i convert my SQLITE3 TABLE to a python dictionary where the name and value of the column of the table is converted to key and value of dictionary.



Solution 1:[1]

I have made a package to solve this issue if anyone got into this problem..

aiosqlitedict

Here is what it can do

  1. Easy conversion between sqlite table and Python dictionary and vice-versa.
  2. Get values of a certain column in a Python list.
  3. Order your list ascending or descending.
  4. Insert any number of columns to your dict.

Getting Started

We start by connecting our database along with the reference column

from aiosqlitedict.database import Connect

countriesDB = Connect("database.db", "user_id")

Make a dictionary

The dictionary should be inside an async function.

async def some_func():
    countries_data = await countriesDB.to_dict("my_table_name", 123, "col1_name", "col2_name", ...)

You can insert any number of columns, or you can get all by specifying the column name as '*'

    countries_data = await countriesDB.to_dict("my_table_name", 123, "*")

so you now have made some changes to your dictionary and want to export it to sql format again?

Convert dict to sqlite table

async def some_func():
    ...
    await countriesDB.to_sql("my_table_name", 123, countries_data)

But what if you want a list of values for a specific column?

Select method

you can have a list of all values of a certain column.

country_names = await countriesDB.select("my_table_name", "col1_name")

to limit your selection use limit parameter.

country_names = await countriesDB.select("my_table_name", "col1_name", limit=10)

you can also arrange your list by using ascending parameter and/or order_by parameter and specifying a certain column to order your list accordingly.

country_names = await countriesDB.select("my_table_name", "col1_name", order_by="col2_name", ascending=False)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abdo Sabry