/Python.sqlite3.row_factory.namedtuple.20221015151253

Write a thin wrapper that returns SQLite3 select result as namedtuple type.

Primary LanguagePythonCreative Commons Zero v1.0 UniversalCC0-1.0

ja

sqlite3.row_factory.namedtuple

Write a thin wrapper that returns SQLite3 select result as namedtuple type.

Requirement

$ 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

  • github
  • hatena
  • twitter
  • mastodon

License

This software is CC0 licensed.

CC0