sqlite3.row_factory.namedtuple
Write a thin wrapper that returns SQLite3 select result as namedtuple type.
Requirement
- 2022-10-15
- Raspbierry Pi 4 Model B Rev 1.2
- Raspberry Pi OS buster 10.0 2020-08-20 setup
- bash 5.0.3(1)-release
- Python 3.10.5
$ uname -a
Linux raspberrypi 5.10.103-v7l+ #1529 SMP Tue Mar 8 12:24:00 GMT 2022 armv7l GNU/Linux
Installation
git clone https://github.com/ytyaru/Python.sqlite3.row_factory.namedtuple.20221015151253
Usage
unit test
cd Python.sqlite3.row_factory.namedtuple.20221015151253/src
./test-ntlite.py
import
from ntlite import NtLite
new
db = NtLite() # :memory:
db = NtLite('./db/my.sqlite3')
API
method | call sqlite3 method |
---|---|
exec |
execute |
execm |
executemany |
execs |
executescript |
get |
execute + fetchone |
gets |
execute + fetchall |
example
#!/usr/bin/env python3
# coding: utf8
import os
from ntlite import NtLite
path = 'my.db'
os.remove(path)
db = NtLite(path)
db.exec("create table users(id integer, name text);")
db.execm("insert into users values(?,?);", [(0,'A'),(1,'B')])
assert 2 == db.get("select count(*) num from users;").num
rows = db.gets("select * from users;")
assert 0 == rows[0].id
assert 'A' == rows[0].name
assert 1 == rows[1].id
assert 'B' == rows[1].name
Author
ytyaru
License
This software is CC0 licensed.