Skip to main content

SQLite-compatible database with CJK FTS5 support

Project description

sql5

SQLite-compatible database with native CJK FTS5 support.

Installation

pip install sql5

Python API

import sql5

# Create in-memory database
db = sql5.connect()

# Or open a file
# db = sql5.connect("mydb.db")

# Execute SQL
db.execute("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)")
db.execute("INSERT INTO users VALUES (1, 'Alice', 30)")
db.execute("INSERT INTO users VALUES (2, 'Bob', 25)")
db.execute("INSERT INTO users VALUES (3, 'Charlie', 35)")

# Query with parameters
db.execute("INSERT INTO users VALUES (?, ?, ?)", (4, "David", 28))

# Fetch results
cursor = db.execute("SELECT * FROM users WHERE age > ?", (25,))
for row in cursor:
    print(row)
# (1, 'Alice', 30)
# (2, 'Bob', 25)
# (3, 'Charlie', 35)
# (4, 'David', 28)

# Fetch as list
cursor = db.execute("SELECT name, age FROM users ORDER BY age")
rows = cursor.fetchall()
print(rows)
# [('Bob', 25), ('David', 28), ('Alice', 30), ('Charlie', 35)]

# Fetch one
cursor = db.execute("SELECT * FROM users WHERE id = ?", (1,))
row = cursor.fetchone()
print(row)
# (1, 'Alice', 30)

# Transactions
db.execute("BEGIN")
db.execute("INSERT INTO users VALUES (5, 'Eve', 40)")
db.execute("COMMIT")

# Or rollback
db.execute("BEGIN")
db.execute("INSERT INTO users VALUES (6, 'Frank', 45)")
db.execute("ROLLBACK")

# Full-text search (FTS5)
db.execute("CREATE VIRTUAL TABLE articles USING fts5(title, body)")
db.execute("INSERT INTO articles VALUES ('Hello World', 'The quick brown fox')")
db.execute("INSERT INTO articles VALUES ('Rust Guide', 'Memory safety without GC')")
db.execute("INSERT INTO articles VALUES ('中文測試', '繁體中文全文檢索')")

cursor = db.execute("SELECT * FROM articles WHERE articles MATCH ?", ("rust",))
print(cursor.fetchall())
# [('Rust Guide', 'Memory safety without GC')]

cursor = db.execute("SELECT * FROM articles WHERE articles MATCH ?", ("中文",))
print(cursor.fetchall())
# [('中文測試', '繁體中文全文檢索')]

# Close database
db.close()

CLI Usage

# Run the REPL
sql5

# Open a database file
sql5 /path/to/database.db

# Execute single query
echo "SELECT 1 + 1;" | sql5

Features

  • Full SQL support (SELECT, INSERT, UPDATE, DELETE, CREATE, DROP)
  • ACID transactions (BEGIN, COMMIT, ROLLBACK)
  • WAL mode
  • Foreign keys
  • Views
  • Triggers
  • Full-text search (FTS5) with CJK bigram tokenization
  • Multiple database attachment (ATTACH DATABASE)
  • Window functions (ROW_NUMBER, RANK, LAG, LEAD, etc.)
  • String functions (UPPER, LOWER, SUBSTR, REPLACE, etc.)
  • Date/time functions (DATE, TIME, DATETIME, STRFTIME)
  • JSON functions (JSON, JSON_EXTRACT, JSON_SET, etc.)

Requirements

  • Python 3.8+

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sql5-1.22.16.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sql5-1.22.16-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file sql5-1.22.16.tar.gz.

File metadata

  • Download URL: sql5-1.22.16.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for sql5-1.22.16.tar.gz
Algorithm Hash digest
SHA256 7633050bc4a9008be8a514e8e896965a46d2fed0567170f83cce17b259918c1e
MD5 35b31a7e573d26dc7c3246fcce53e8f5
BLAKE2b-256 ca00e7c32d78ce7e666ff47737f86cfc68f3d5f2705bd549fd9fd898bfe9eb5b

See more details on using hashes here.

File details

Details for the file sql5-1.22.16-py3-none-any.whl.

File metadata

  • Download URL: sql5-1.22.16-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for sql5-1.22.16-py3-none-any.whl
Algorithm Hash digest
SHA256 1131fbd7d55a05f46fa48cb5e6d68fa646ebc56673d7ec3969d6cf555d6b0774
MD5 cfcc8648bed0eea71a14abdc385f69d8
BLAKE2b-256 7d7a44dfe0c1f2ed7f7e2bb24a1eecac58c8b1e0edb00238c4e1f7a8ef454a27

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page