Using our existing tools, we can already bind Payload instances to sqlite3 databases:
import sqlite3
from dataclasses import dataclass
from ipv8.messaging.payload_dataclass import DataClassPayload
from ipv8.messaging.serialization import default_serializer
# 1. Typical Payload
@dataclass
class DBPayload(DataClassPayload):
a: int
b: str
c: bool
# 2. Register it as a database type
sqlite3.register_adapter(DBPayload, default_serializer.pack_serializable)
sqlite3.register_converter("DBPayload", lambda s: default_serializer.unpack_serializable(DBPayload, s))
# == Demo/how to use ==
con = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)
cur = con.execute("CREATE TABLE mytable(p DBPayload)")
cur.execute("INSERT INTO mytable(p) VALUES(?)", (DBPayload(1, "2", False),))
cur.execute("SELECT p FROM mytable")
result, length = cur.fetchone()[0]
print(result)
cur.close()
con.close()
Output:
DBPayload
| a: 1
| b: '2'
| c: False
We could add this to our documentation or offer a 1-liner to register payloads with sqlite3.
Additionally, we could show how to convert the format list of a Payload into a row in the database
Using our existing tools, we can already bind
Payloadinstances tosqlite3databases:Output:
We could add this to our documentation or offer a 1-liner to register payloads with
sqlite3.Additionally, we could show how to convert the format list of a
Payloadinto a row in the database