Can we use piccolo just as query builder? #814
Answered
by
dantownsend
danechitoaie
asked this question in
Q&A
-
Can we use piccolo just as query builder? And is there a way to use it without piccolo_conf.py and piccolo_app.py as I don't have my app structured like this in project with apps in it. |
Beta Was this translation helpful? Give feedback.
Answered by
dantownsend
Apr 27, 2023
Replies: 1 comment 4 replies
-
Yeah, you can use it all in a single file if you want. I use it like that quite a lot for scripts etc. Here's an example: import asyncio
from piccolo.engine.sqlite import SQLiteEngine
from piccolo.table import Table, create_db_tables
from piccolo.columns import Varchar
#########
# Define tables
# Define the engine for connecting to the database
DB = SQLiteEngine()
# Bind it explicitly to each Table, rather than using `piccolo_conf.py`
class MyTable(Table, db=DB):
my_column = Varchar()
#########
# An example app using the query builder
app = FastAPI()
@app.get('/')
async def my_endpoint():
return await MyTable.select()
#########
# Optionally create the database tables when the script starts
async def main():
await create_db_tables(MyTable, if_not_exists=True)
if __name__ == '__main__':
asyncio.run(main())
import uvicorn
uvicorn.run(app) Main takeaways:
|
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
dantownsend
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah, you can use it all in a single file if you want.
I use it like that quite a lot for scripts etc.
Here's an example: