You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
860 B

package main
import (
"database/sql"
"fmt"
"time"
"math/rand"
"os"
_ "modernc.org/sqlite"
)
func main() {
rand.Seed(time.Now().UnixNano())
db, err := sql.Open("sqlite", "prayers.db")
if err != nil {
fmt.Println("error loading database driver: "+ err.Error())
os.Exit(1)
}
langcode := "en"
if len(os.Args) > 1 {
langcode = os.Args[1]
}
rows, err := db.Query("select text, author from prayers where language = ?", langcode)
if err != nil {
fmt.Println("database query failed (is prayers.db in your current directory?): " +err.Error())
os.Exit(2)
}
var prayers []string
for rows.Next() {
var prayer, author string
rows.Scan(&prayer, &author)
prayers = append(prayers, prayer+"\n\n-- "+author)
}
if len(prayers) < 1 {
fmt.Println("query returns no prayers")
os.Exit(3)
}
fmt.Println(prayers[rand.Intn(len(prayers))])
}