new sqlite3-python

This commit is contained in:
2024-09-15 11:04:57 +00:00
parent 355dd3f600
commit 8680e807c3
3 changed files with 50 additions and 1 deletions

View File

@@ -41,7 +41,7 @@ alias journal="sudo journalctl -u"
export GGML_CUDA=1
export LLAMA_CURL=1
export LIBGL_ALWAYS_INDIRECT=1
# export LIBGL_ALWAYS_INDIRECT=1
export LD_LIBRARY_PATH="/usr/local/cuda/lib64:/usr/local/lib:$LD_LIBRARY_PATH"
export PATH="/home/w/.venv/bin:/home/w/hub/llama.cpp:/usr/local/cuda/bin:$PATH"
@@ -120,6 +120,8 @@ add() {
}
alias remote="git remote -v"
alias stash="git stash"
alias branch="git branch"
alias checkout="git checkout"
alias vimrc="vi ~/.vimrc"
commit() {
git commit -m "$*" && git push

1
sqlite3-python/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.db

46
sqlite3-python/sqlite.py Normal file
View File

@@ -0,0 +1,46 @@
import sqlite3
con = sqlite3.connect('test.db', isolation_level=None)
con.execute('PRAGMA journal_mode=wal')
con.row_factory = lambda *args: dict(sqlite3.Row(*args))
sql = lambda *args: list(con.execute(*args))
# init
sql('''
CREATE TABLE IF NOT EXISTS books
(id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
author TEXT NOT NULL,
price REAL NOT NULL,
stock INTEGER NOT NULL)
''')
# write
for book in [
('To Kill a Mockingbird', 'Harper Lee', 12.99, 25),
('1984', 'George Orwell', 10.99, 30),
('Pride and Prejudice', 'Jane Austen', 9.99, 20),
('The Great Gatsby', 'F. Scott Fitzgerald', 11.99, 15),
('The Catcher in the Rye', 'J.D. Salinger', 10.50, 22)
]:
sql('INSERT INTO books (title, author, price, stock) VALUES (?, ?, ?, ?)', book)
# read
sql('SELECT * FROM books')
# 특정 조건으로 데이터 조회
expensive_books = sql('SELECT title, author, price FROM books WHERE price > 11.00 ORDER BY price DESC')
print("\nBooks priced over $11:")
for book in expensive_books:
print(f"{book['title']} by {book['author']} - ${book['price']:.2f}")
# 재고 업데이트
sql('UPDATE books SET stock = stock - 1 WHERE title = ?', ('1984',))
# 업데이트 확인
updated_book = sql('SELECT title, stock FROM books WHERE title = ?', ('1984',))[0]
print(f"\nUpdated stock for '1984': {updated_book['stock']}")