add basic read/execute loop

This commit is contained in:
Antoine Beaupré 2018-03-12 16:38:09 -04:00
parent beacf72c77
commit 45937002d9
No known key found for this signature in database
GPG Key ID: 3EA1DDDDB261D97B
1 changed files with 23 additions and 2 deletions

View File

@ -1,25 +1,46 @@
package main
import (
"bufio"
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"log"
"os"
"strings"
)
func main() {
log.Printf("args: %d\n", len(os.Args))
if len(os.Args) < 2 {
log.Fatal("not enough arguments")
}
path := os.Args[1]
query := os.Args[2]
db, err := sql.Open("sqlite3", path)
if err != nil {
log.Fatal(err)
}
defer db.Close()
if len(os.Args) < 3 {
dumbShell(db)
} else {
query := os.Args[2]
runQuery(db, query)
}
}
func dumbShell(db *sql.DB) {
for {
reader := bufio.NewReader(os.Stdin)
fmt.Print("sql> ")
var query string
query, _ = reader.ReadString('\n')
//fmt.Println(query)
query = strings.TrimSpace(query)
if len(query) > 0 {
runQuery(db, query)
}
}
}
func runQuery(db *sql.DB, query string) (err error) {