please help me with this error

please help me with this error: https://paste.victor.computer/Sys4bFaD4
package main

import (
_ "github.com/mattn/go-sqlite3"
"database/sql"
"errors"
)

var db *sql.DB

func DBinit() error {
db, err := sql.Open("sqlite3", "./database.sqlite")
if err != nil {
return err
}
_, err = db.Exec("CREATE TABLE IF NOT EXISTS guides (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT UNIQUE, content TEXT)")
return err
}
func DBinsert(title, content string) error {
_, err := db.Exec("INSERT INTO guides (title, content) VALUES (?, ?)", title, content)
return err
}
func DBget(title string) (string, error) {
rows, err := db.Query("SELECT content FROM guides WHERE title = ?", title)
if err != nil {
return "", err
}
if rows.Next() {
var content string
err = rows.Scan(&content)
if err != nil {
return "", err
}
return content, nil
}
return "", errors.New("No guide found")
}
func DBclose() {db.Close()}
 
package main

import (
"log"
"fmt"
"net/http"
"net/url"
)

func main() {
err := DBinit()
if err != nil {log.Fatal(err)}
defer DBclose()
http.HandleFunc("/create", create)
http.HandleFunc("/edit/", edit)
http.HandleFunc("/guide/", guide)
http.HandleFunc("/", home)
log.Fatal(http.ListenAndServe(":8080", nil))
log.Println("Running stuyguides")
}
func guide(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path[len("/guide/"):]
title, content := DBget(path)
fmt.Fprintf(w, "You requested the guide: %s\nTitle: %s\nContent: %s\n", path, title, content)
}
func edit(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path[len("/edit/"):]
fmt.Fprintf(w, "You are going to edit the guide: %s", path)
}
func create(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/create" {
http.NotFound(w, r)
return
}
switch r.Method {
case "GET":
fmt.Fprintf(w, "You are creating a guide")
case "POST":
if r.FormValue("content") == "" || r.FormValue("title") == "" {
http.Error(w, "Either the title or the content are missing", http.StatusBadRequest)
return
}
/*err := DBinsert(r.FormValue("content"), r.FormValue("title"))
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
log.Println(err)
return
}*/
http.Redirect(w, r, "/guide/" + url.PathEscape(r.FormValue("title")), http.StatusFound)
default:
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
}
}
func home(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/":
fmt.Fprint(w, "Welcome to the stuygides website")
case "/about":
fmt.Fprint(w, "Welcome to the about page!")
default:
http.NotFound(w, r)
}
}
can't make heads or tails of it... based off of the error the problem probably came from the SQL query but I have no idea what to do about it.

or even what the actual problem is

You already invited:

AOD

Upvotes from:

Your package-level `db` is never being set
Also, don't use package-level variables for anything with state
In `DBInit` `db, err := sql.Open("sqlite3", "./database.sqlite")` is declaring a new `db` variable, shadowing the package level one

If you wanna answer this question please Login or Register