Put protocol buffers into sqlite with python

In 3 lines

Push the serialized data of protocol buffers into sqlite.

  1. Create a blob column in sqlite
  2. When plunging, use buffer
  3. Use str to load

(It's just treated as a binary regardless of protobuf)

Example

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

result

name: "John Doe"                                      
age: 128                                              
                                                      
name: "John Doe"                                      
age: 128

I was able to read and write.

Recommended Posts

Put protocol buffers into sqlite with python
[Python] [SQLite3] Operate SQLite with Python (Basic)
Read Protocol Buffers data in Python3
Easily handle lists with python + sqlite3
Easily handle databases with Python (SQLite3)
How to import CSV and TSV files into SQLite with Python
INSERT into MySQL with Python [For beginners]
Save / load in-memory DB with python sqlite3
Trying to handle SQLite3 with Python [Note]
[TensorFlow] Python ⇔ Protocol Buffers ⇔ GPU / Distributed Computing
gRPC-Methods used when dealing with protocol buffers types in Python CopyFrom, Extend
I tried hundreds of millions of SQLite with python
FizzBuzz with Python3
Make Python scripts into Windows-executable .exes with Pyinstaller
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Put text scraped in Python into Google Sheets
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
Automatically format Python code into PEP8-compliant code with Emacs
python starts with ()
with syntax (Python)
Put Python 2.7.x on Mac OSX 10.15.5 with pyenv
Sqlite in python
Bingo with python
Zundokokiyoshi with python
Make JSON into CSV with Python from Splunk
Excel with Python
Microcomputer with Python
Cast with python
Automatically paste images into PowerPoint materials with python + α
Specific sample code for working with SQLite3 in Python
Use Cursur that closes automatically with sqlite3 in Python
Automatically translate DeepL into English with Python and Selenium
Serial communication with Python
Zip, unzip with python
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Try scraping with Python.
Sequential search with Python
"Object-oriented" learning with python
Put stdout into numpy.loadtxt
Run Python with VBA
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learn Python with ChemTHEATER
Run prepDE.py with python3
Collecting tweets with Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS