Skip to content

Instantly share code, notes, and snippets.

@wperron
Created May 4, 2022 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wperron/49a66dd1c14520805d4e67fa6749d698 to your computer and use it in GitHub Desktop.
Save wperron/49a66dd1c14520805d4e67fa6749d698 to your computer and use it in GitHub Desktop.
quick and dirty autocomplete feature
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