frompysqlx_engineimportPySQLXEngineasyncdefmain():uri="sqlite:./db.db"db=PySQLXEngine(uri=uri)awaitdb.connect()sql="SELECT 1 AS id, 'Rian' AS name"resp1=awaitdb.query_as_dict(sql=sql)resp2=awaitdb.query_first_as_dict(sql=sql)print("returned:",resp1,"as a list of dicts")print("returned:",resp2,"as a dict")importasyncioasyncio.run(main())
frompysqlx_engineimportPySQLXEngineSyncdefmain():uri="sqlite:./db.db"db=PySQLXEngineSync(uri=uri)db.connect()sql="SELECT 1 AS id, 'Rian' AS name"resp1=db.query_as_dict(sql=sql)resp2=db.query_first_as_dict(sql=sql)print("returned:",resp1,"as a list of dicts")print("returned:",resp2,"as a dict")# running the codemain()
Running the code using the terminal
$ python3main.py
returned: [{'id': 1, 'name': 'Rian'}] as a list of dict returned: {'id': 1, 'name': 'Rian'} as a dict
The .query() and .query_first() methods are useful when you want to get the results as a list of BaseRow or a BaseRow.
BaseRow is a class that represents a row of the query result. It has the same attributes as the columns of the query result.
The BaseRow is an inheritance from Pydantic BaseModel.
frompysqlx_engineimportPySQLXEngineasyncdefmain():uri="sqlite:./db.db"db=PySQLXEngine(uri=uri)awaitdb.connect()sql="SELECT 1 AS id, 'Rian' AS name"resp1=awaitdb.query(sql=sql)resp2=awaitdb.query_first(sql=sql)print("returned:",resp1,"as a list of BaseRow")print("returned:",resp2,"as a BaseRow")importasyncioasyncio.run(main())
frompysqlx_engineimportPySQLXEngineSyncdefmain():uri="sqlite:./db.db"db=PySQLXEngineSync(uri=uri)db.connect()sql="SELECT 1 AS id, 'Rian' AS name"resp1=db.query(sql=sql)resp2=db.query_first(sql=sql)print("returned:",resp1,"as a list of BaseRow")print("returned:",resp2,"as a BaseRow")# running the codemain()
Running the code using the terminal
$ python3main.py
returned: [BaseRow(id=1, name='Rian')] as a list of BaseRowreturned: BaseRow(id=1, name='Rian') as a BaseRow
The .query() and .query_first() methods are useful when you want to get the results as a list of MyModel or a MyModel.
The PySQLXEngine has a feature to convert the query result to a custom model. You can use the model argument to specify the model.
Using the model argument, you can have an autocomplete feature in your IDE, this brings more security and makes development easier.
None
The model argument is only available for the .query() and .query_first() methods.
The model needs to inherit from BaseRow, you can use from pysqlx_engine import BaseRow to import the BaseRow class.
Create a models.py file and add the code examples below.
frompysqlx_engineimportPySQLXEnginefrommodelsimportUserasyncdefmain():uri="sqlite:./db.db"db=PySQLXEngine(uri=uri)awaitdb.connect()sql="SELECT 1 AS id, 'Rian' AS name"resp1=awaitdb.query(sql=sql,model=User)resp2=awaitdb.query_first(sql=sql,model=User)print("returned:",resp1,"as a list of User")print("returned:",resp2,"as a User")importasyncioasyncio.run(main())
frompysqlx_engineimportPySQLXEngineSyncfrommodelsimportUserdefmain():uri="sqlite:./db.db"db=PySQLXEngineSync(uri=uri)db.connect()sql="SELECT 1 AS id, 'Rian' AS name"resp1=db.query(sql=sql,model=User)resp2=db.query_first(sql=sql,model=User)print("returned:",resp1,"as a list of User")print("returned:",resp2,"as a User")# running the codemain()
You can see the autocomple.
Visual Studio Code
PyCharm
Running the code using the terminal
$ python3main.py
returned: [User(id=1, name='Rian')] as a list of Userreturned: User(id=1, name='Rian') as a User