You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The code below shows use of the [Postgres](https://spinframework.github.io/spin-python-sdk/v4/postgres.html) module and its [open](https://spinframework.github.io/spin-python-sdk/v4/postgres.html#spin_sdk.postgres.open) function for opening a connection to the database:
withawait postgres.open("user=postgres dbname=spin_dev host=localhost sslmode=disable password=password") as db:
158
-
print(db.query("SELECT * FROM test", []))
158
+
withawait Connection.open("user=postgres dbname=spin_dev host=localhost sslmode=disable password=password") as db:
159
+
columns, stream, future =await db.query("SELECT * FROM test", [])
160
+
rows =await util.collect((stream, future))
159
161
160
162
return Response(
161
163
200,
162
164
{"content-type": "text/plain"},
163
-
bytes("Hello from Python!", "utf-8")
165
+
bytes(str(rows), "utf-8")
164
166
)
165
167
```
166
168
169
+
**General Notes**
170
+
* The `query` method returns a Tuple containing a list of `columns`, a list of `rows` encapsulated via a [StreamReader](https://github.com/bytecodealliance/componentize-py/blob/1b3d2e936868307a48fb70941dcad71b54e844f8/bundled/componentize_py_async_support/streams.py#L101), and a [FutureReader](https://github.com/bytecodealliance/componentize-py/blob/1b3d2e936868307a48fb70941dcad71b54e844f8/bundled/componentize_py_async_support/futures.py#L11). You _must_ check when the stream ends, to determine if the stream ended normally, or was terminated prematurely due to an error.
171
+
172
+
> As seen in the example above, you can utilize the [collect](https://spinframework.github.io/spin-python-sdk/v4/util.html#spin_sdk.util.collect) method from the `util` package to handle the `StreamReader` and `FutureReader` pair, aggregating the resulting rows into memory.
173
+
174
+
* The `Connection` object doesn't surface the `close` function.
175
+
* Errors are surfaced as exceptions.
176
+
167
177
You can find a complete outbound PostgreSQL example in the [Spin Python SDK repository on GitHub](https://github.com/spinframework/spin-python-sdk/tree/main/examples/spin-postgres). There is also an [Outbound MySQL example](https://github.com/spinframework/spin-python-sdk/tree/main/examples/spin-mysql) available.
> [**Want to go straight to the reference documentation?** Find it here.](https://spinframework.github.io/spin-python-sdk/v4/sqlite.html)
160
160
161
-
To use SQLite functions, use the `sqlite` module in the Python SDK. The [`open`](https://spinframework.github.io/spin-python-sdk/v4/sqlite.html#spin_sdk.sqlite.open) and [`open_default`](https://spinframework.github.io/spin-python-sdk/v4/sqlite.html#spin_sdk.sqlite.open_default) functions return a [`Connection` object](https://spinframework.github.io/spin-python-sdk/v4/wit/imports/spin_sqlite_sqlite_3_1_0.html#spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.Connection). The `Connection` object provides the [`execute` method](https://spinframework.github.io/spin-python-sdk/v4/wit/imports/spin_sqlite_sqlite_3_1_0.html#spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.Connection.execute) as described above. For example:
161
+
To use SQLite functions, use the `sqlite` module in the Python SDK. The [`open`](https://spinframework.github.io/spin-python-sdk/v4/sqlite.html#spin_sdk.sqlite.open) and [`open_default`](https://spinframework.github.io/spin-python-sdk/v4/sqlite.html#spin_sdk.sqlite.open_default) functions return a [`Connection` object](https://spinframework.github.io/spin-python-sdk/v4/sqlite.html#spin_sdk.sqlite.Connection). The `Connection` object provides the [`execute` method](https://spinframework.github.io/spin-python-sdk/v4/sqlite.html#spin_sdk.sqlite.Connection.execute) as described above. For example:
162
162
163
163
```python
164
-
from spin_sdk import http, sqlite
164
+
from spin_sdk import http, util
165
165
from spin_sdk.http import Request, Response
166
-
from spin_sdk.sqlite import Value_Integer
166
+
from spin_sdk.sqlite importConnection, Value_Text, Value_Integer
result = db.execute("SELECT * FROM todos WHERE id > (?);", [Value_Integer(1)])
172
-
rows = result.rows
173
-
170
+
withawait Connection.open_default() as db:
171
+
await db.execute("INSERT INTO todos (description, due) VALUES (?, ?)", [Value_Text("Try out Spin SQLite"), Value_Text("Friday")])
172
+
columns, stream, future =await db.execute("SELECT * FROM todos WHERE id > (?);", [Value_Integer(1)])
173
+
rows =await util.collect((stream, future))
174
+
174
175
return Response(
175
176
200,
176
177
{"content-type": "text/plain"},
@@ -179,7 +180,10 @@ class HttpHandler(http.Handler):
179
180
```
180
181
181
182
**General Notes**
182
-
* The `execute` method returns [a `QueryResult` object](https://spinframework.github.io/spin-python-sdk/v4/wit/imports/spin_sqlite_sqlite_3_1_0.html#spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.QueryResult) with `rows` and `columns` methods. `columns` returns a list of strings representing column names. `rows` is an array of rows, each of which is an array of [`RowResult`](https://spinframework.github.io/spin-python-sdk/v4/wit/imports/spin_sqlite_sqlite_3_1_0.html#spin_sdk.wit.imports.spin_sqlite_sqlite_3_1_0.RowResult) in the same order as `columns`.
183
+
* The `execute` method returns a Tuple containing a list of `columns`, a list of `rows` encapsulated via a [StreamReader](https://github.com/bytecodealliance/componentize-py/blob/1b3d2e936868307a48fb70941dcad71b54e844f8/bundled/componentize_py_async_support/streams.py#L101), and a [FutureReader](https://github.com/bytecodealliance/componentize-py/blob/1b3d2e936868307a48fb70941dcad71b54e844f8/bundled/componentize_py_async_support/futures.py#L11). You _must_ check when the stream ends, to determine if the stream ended normally, or was terminated prematurely due to an error.
184
+
185
+
> As seen in the example above, you can utilize the [collect](https://spinframework.github.io/spin-python-sdk/v4/util.html#spin_sdk.util.collect) method from the `util` package to handle the `StreamReader` and `FutureReader` pair, aggregating the resulting rows into memory.
186
+
183
187
* The `Connection` object doesn't surface the `close` function.
0 commit comments