<===
2025-10-02 08:44:39
$ cat notes-import-s.py
import sqlite3
from datetime import datetime
def create_database(conn):
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS notes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date TEXT NOT NULL,
note TEXT NOT NULL
)
""")
cursor.execute("""
CREATE VIRTUAL TABLE IF NOT EXISTS notes_fts USING fts5(
note,
content='notes',
content_rowid='id'
)
""")
cursor.execute("""
CREATE TRIGGER IF NOT EXISTS notes_insert AFTER INSERT ON notes
BEGIN
INSERT INTO notes_fts(rowid, note) VALUES (new.id, new.note);
END
""")
cursor.execute("""
CREATE TRIGGER IF NOT EXISTS notes_update AFTER UPDATE ON notes
BEGIN
INSERT INTO notes_fts(notes_fts, rowid, note) VALUES('delete', old.id, old.note);
INSERT INTO notes_fts(rowid, note) VALUES (new.id, new.note);
END
""")
cursor.execute("""
CREATE TRIGGER IF NOT EXISTS notes_delete AFTER DELETE ON notes
BEGIN
INSERT INTO notes_fts(notes_fts, rowid, note) VALUES('delete', old.id, old.note);
END
""")
conn.commit()
def parse_date(date_str):
for fmt in ("%Y.%m.%d", "%Y-%m-%d"):
try:
return datetime.strptime(date_str, fmt)
except ValueError:
continue
raise ValueError(f"Дата не соответствует форматам: {date_str}")
def import_notes_from_file(conn, file_path):
cursor = conn.cursor()
with open(file_path, 'r', encoding='utf-8') as f:
for line in f:
parts = line.strip().split(' ', 2)
if len(parts) < 3:
continue
raw_date, time_str, note = parts
date_obj = parse_date(raw_date)
date_str = date_obj.strftime("%Y-%m-%d")
datetime_str = f"{date_str} {time_str}"
# Проверка уникальности по точному времени
cursor.execute("SELECT 1 FROM notes WHERE date = ?", (datetime_str,))
if cursor.fetchone():
continue
cursor.execute("INSERT INTO notes (date, note) VALUES (?, ?)", (datetime_str, note))
conn.commit()
if __name__ == "__main__":
db_path = "notebook.db"
data_file = "0.dat"
conn = sqlite3.connect(db_path)
create_database(conn)
import_notes_from_file(conn, data_file)
conn.close()