|
1 | | -# database.md |
| 1 | +# Connecting to a Database |
| 2 | + |
| 3 | +```python exec |
| 4 | +import reflex as rx |
| 5 | +``` |
| 6 | + |
| 7 | +Connecting to a database is critical to give your app access to real data. This section will cover how to connect to a database using the AI Builder. |
| 8 | + |
| 9 | +To connect to a database you will need a `DB_URI`. Reflex.build currently supports `postgresql` and `mysql` databases. |
| 10 | + |
| 11 | +This is what it looks like for a Postgres database: |
| 12 | + |
| 13 | +```bash |
| 14 | +postgresql://username:password@hostname:port/database_name |
| 15 | +``` |
| 16 | + |
| 17 | +```bash |
| 18 | +postgresql://admin: [email protected]:5432/mydatabase |
| 19 | +``` |
| 20 | + |
| 21 | +```python eval |
| 22 | +rx.box(height="1rem") |
| 23 | +``` |
| 24 | + |
| 25 | +```python eval |
| 26 | +rx.accordion.root( |
| 27 | + rx.accordion.item( |
| 28 | + header="DB URI (More Details)", |
| 29 | + content=rx.table.root( |
| 30 | + rx.table.header( |
| 31 | + rx.table.row( |
| 32 | + rx.table.column_header_cell("Component"), |
| 33 | + rx.table.column_header_cell("Example"), |
| 34 | + rx.table.column_header_cell("Description"), |
| 35 | + ), |
| 36 | + ), |
| 37 | + rx.table.body( |
| 38 | + rx.table.row( |
| 39 | + rx.table.row_header_cell(rx.code("postgresql://")), |
| 40 | + rx.table.cell(rx.code("postgresql://")), |
| 41 | + rx.table.cell("The database ", rx.el.span("dialect", font_weight="bold"), " or driver (can also be ", |
| 42 | + rx.code("mysql://"), ", ", rx.code("sqlite:///"), ", etc.)"), |
| 43 | + ), |
| 44 | + rx.table.row( |
| 45 | + rx.table.row_header_cell(rx.code("username")), |
| 46 | + rx.table.cell(rx.code("admin")), |
| 47 | + rx.table.cell("The ", rx.el.span("username", font_weight="bold"), " used to authenticate with the database"), |
| 48 | + ), |
| 49 | + rx.table.row( |
| 50 | + rx.table.row_header_cell(rx.code("password")), |
| 51 | + rx.table.cell(rx.code("secret123")), |
| 52 | + rx.table.cell("The ", rx.el.span("password", font_weight="bold"), " for the database user (insecure to expose this in plain text)"), |
| 53 | + ), |
| 54 | + rx.table.row( |
| 55 | + rx.table.row_header_cell(rx.code("hostname")), |
| 56 | + rx.table.cell(rx.code("db.mycompany.com")), |
| 57 | + rx.table.cell("The ", rx.el.span("host", font_weight="bold"), " where the database server is running"), |
| 58 | + ), |
| 59 | + rx.table.row( |
| 60 | + rx.table.row_header_cell(rx.code("port")), |
| 61 | + rx.table.cell(rx.code("5432")), |
| 62 | + rx.table.cell("The ", rx.el.span("port", font_weight="bold"), " the database server is listening on (default for PostgreSQL)"), |
| 63 | + ), |
| 64 | + rx.table.row( |
| 65 | + rx.table.row_header_cell(rx.code("database_name")), |
| 66 | + rx.table.cell(rx.code("mydatabase")), |
| 67 | + rx.table.cell("The ", rx.el.span("name", font_weight="bold"), " of the database to connect to"), |
| 68 | + ), |
| 69 | + ), |
| 70 | + width="100%", |
| 71 | + ), |
| 72 | + ), |
| 73 | + variant="surface", |
| 74 | +) |
| 75 | +``` |
| 76 | + |
| 77 | +```python eval |
| 78 | +rx.box(height="1rem") |
| 79 | +``` |
| 80 | + |
| 81 | +You can also use a MySQL database. The connection string looks like this: |
| 82 | + |
| 83 | +```bash |
| 84 | +mysql://username:password@hostname:port/database_name |
| 85 | +``` |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +## Connecting your Database before the app is generated |
| 90 | + |
| 91 | +You can add your `Database URI` at the start of your generation as shown below. |
| 92 | + |
| 93 | +```python eval |
| 94 | +rx.image( |
| 95 | + src="/ai_builder/add_db_before_app.gif", |
| 96 | + height="auto", |
| 97 | + padding_bottom="2rem", |
| 98 | +) |
| 99 | +``` |
| 100 | + |
| 101 | +Here if you wanted to build a dashboard for example we recommend a prompt as follows: |
| 102 | + |
| 103 | +`Build a dashboard around my database data` |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +## Connecting your Database after the app is generated |
| 108 | + |
| 109 | +You can add your `Database URI` after you've already generated an app or directly from a template that you have forked as shown below. |
| 110 | + |
| 111 | +```python eval |
| 112 | +rx.image( |
| 113 | + src="/ai_builder/add_db_in_app.gif", |
| 114 | + height="auto", |
| 115 | + padding_bottom="2rem", |
| 116 | +) |
| 117 | +``` |
| 118 | + |
| 119 | +Here if you wanted to hook up your data correctly we recommend a prompt as follows: |
| 120 | + |
| 121 | +`Use the database I added to rewrite the dashboard to display my expense reporting data, keep the existing layout charts and structure the same` |
0 commit comments