From 040633b7be1dbf7b5273462477683e9742901f96 Mon Sep 17 00:00:00 2001 From: Alex Stevenson Date: Wed, 23 Jul 2025 19:05:20 -0400 Subject: [PATCH] first commit --- README.md | 0 go.mod | 5 +++ go.sum | 2 + main.go | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ words.db | Bin 0 -> 16384 bytes 5 files changed, 127 insertions(+) create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go create mode 100644 words.db diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c8d5205 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module sqlite3_hook + +go 1.18 + +require github.com/mattn/go-sqlite3 v1.14.28 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..42e5bac --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEum7A= +github.com/mattn/go-sqlite3 v1.14.28/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= diff --git a/main.go b/main.go new file mode 100644 index 0000000..1787bd1 --- /dev/null +++ b/main.go @@ -0,0 +1,120 @@ +package main + +import ( + "database/sql" + "log" + + _ "github.com/mattn/go-sqlite3" +) + +// Book is a placeholder for book +type Word struct { + word_id int + source_id int + previous string + current string + next string + count int +} + +type Source struct { + source_id int + name string +} + +func prep_query(db *sql.DB, q string) (*sql.Stmt){ + statement, err := db.Prepare(q) + + if err != nil { + log.Println(err) + log.Println(statement) + } + + return statement +} + +func main() { + db, _ := sql.Open("sqlite3", "words.db") + + log.Println("Nuking tables...") + prep_query(db, "DROP TABLE IF EXISTS words").Exec() + prep_query(db, "DROP TABLE IF EXISTS sources").Exec() + log.Println("Done nuking tables!") + + log.Println("Creating tables...") + prep_query(db, ` + CREATE TABLE IF NOT EXISTS sources ( + source_id INTEGER PRIMARY KEY, + name VARCHAR(64) + ); + `).Exec() + + prep_query(db, ` + CREATE TABLE IF NOT EXISTS words ( + word_id INTEGER PRIMARY KEY, + source_id INTEGER, + previous VARCHAR(64) DEFAULT NULL, + current VARCHAR(64), + next VARCHAR(64) DEFAULT NULL, + count INTEGER DEFAULT 0, + FOREIGN KEY(source_id) REFERENCES sources(source_id) + ); + `).Exec() + log.Println("Done making tables!") + + log.Println("Making indexes...") + prep_query(db, "CREATE INDEX words_sources_index ON words(source_id)").Exec() + log.Println("Done making indexes!") + + log.Println("Inserting source/word...") + prep_query(db, ` + INSERT INTO sources + (name) + VALUES + (?) + `).Exec("Monqui") + + prep_query(db, ` + INSERT INTO words + (current, next, source_id) + VALUES + (?, ?, ?) + `).Exec("My", "name", 1) + + log.Println("Inserted the source/word into database!") + + rows, _ := db.Query("SELECT source_id, name FROM sources") + var tempSource Source + for rows.Next() { + rows.Scan(&tempSource.source_id, &tempSource.name) + log.Printf("source_id:%d, name:%s\n", + tempSource.source_id, tempSource.name) + } + + rows, _ = db.Query("SELECT word_id, current, next, source_id FROM words") + var tempWord Word + for rows.Next() { + rows.Scan(&tempWord.word_id, &tempWord.current, &tempWord.next, &tempWord.source_id) + log.Printf( + "word_id:%d, current:%s, next:%s, source_id:%d\n", + tempWord.word_id, tempWord.current, tempWord.next, tempWord.source_id) + } + + // Update + prep_query(db, "UPDATE sources SET name=? WHERE source_id=?").Exec("Monqu2", 1) + + rows, _ = db.Query("SELECT source_id, name FROM sources") + for rows.Next() { + rows.Scan(&tempSource.source_id, &tempSource.name) + log.Printf("source_id:%d, name:%s\n", + tempSource.source_id, tempSource.name) + } + + // +/* + // Delete + statement, _ = db.Prepare("delete from books where id=?") + statement.Exec(1) + log.Println("Successfully deleted the book in database!") +*/ +} diff --git a/words.db b/words.db new file mode 100644 index 0000000000000000000000000000000000000000..eca1ac204de93e47fb208bf90075635df4c3e6d2 GIT binary patch literal 16384 zcmeI&O;6%L7zgkfO2nEV>%laJ%X6fO+4!>2>sB0)SnBSGMo$E#CM0XCg(@C3#^Zj4 z{frzuxrd!;%L>_-s|o%mEz{0C&oeW>TN4hyy1ju<7h!bS8PF#Cz&K~0i5O!q>dvcs z+FC4!m@@k|wu%UHie#6)el*kL!cZ z**8DQN`8#2TFcpSMVdyH1=BS3tjZ)(3$;XDNctihO-G*6zBpag5x=XCBa+dp$iL}@ zai8eek<~p%R<=Ibv@2@Pk=r8g$aPmqXP?JW#1X z<5kv@ozxDbXw*Hu-w6qlq9&y9s^X9`+=*FL*S_Kft6b*y+X+<`lXbZ%u52&`OJ6kE zzrcddrT@pd^osi1oR^-Ny3im10SG_<0uX=z1Rwwb2tWV=5SS$alN)TM83xz!M*96f zVx`e69S~=M00bZa0SG_<0uX=z1Rwwb2)uKFA}=tLEiKPC@ARJn78D0B{bcGg)MM_Q f>)`khfB*y_009U<00Izz00bZafteF94AcAt;4h{6 literal 0 HcmV?d00001