Created
May 4, 2022 14:33
-
-
Save wperron/49a66dd1c14520805d4e67fa6749d698 to your computer and use it in GitHub Desktop.
quick and dirty autocomplete feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"database/sql" | |
"fmt" | |
"log" | |
"os" | |
_ "github.com/mattn/go-sqlite3" | |
) | |
// -- Database generated using: | |
// CREATE TABLE words (text); | |
// INSERT INTO words VALUES (apple); | |
// INSERT INTO words VALUES (banana); | |
// INSERT INTO words VALUES (blueberry); | |
// INSERT INTO words VALUES (strawberry); | |
func main() { | |
db, err := sql.Open("sqlite3", "./CASSIDOO") | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer db.Close() | |
args := os.Args[1:] | |
stmt, err := db.Prepare("select * from words where text like ?") | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer stmt.Close() | |
for _, a := range args { | |
log.Printf("=> searching for %s", a) | |
rows, err := stmt.Query(fmt.Sprintf("%%%s%%", a)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
for rows.Next() { | |
var word string | |
err = rows.Scan(&word) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(word) | |
} | |
err = rows.Err() | |
if err != nil { | |
log.Fatal(err) | |
} | |
_ = rows.Close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment