Push the serialized data of protocol buffers into sqlite.
blob
column in sqlitebuffer
str
to load(It's just treated as a binary regardless of protobuf)
person.proto
message Person {
optional string name = 1;
optional int32 age = 2;
}
sample.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sqlite3
from person_pb2 import Person
if __name__ == '__main__':
#Connect to database and create table
conn = sqlite3.connect(":memory:")
c = conn.cursor()
c.execute("create table people (person blob)")
p = Person(name="John Doe", age=128)
print p
#writing
wb = buffer(p.SerializeToString())
c.execute("insert into people values (?)", [wb])
#Read
c.execute("select * from people")
rb = c.fetchone()[0]
q = Person()
q.ParseFromString(str(rb))
print q
name: "John Doe"
age: 128
name: "John Doe"
age: 128
I was able to read and write.
Recommended Posts