Esempio n. 1
0
func TestGeneral(t *testing.T) {
	sqlite3.Initialize()
	defer sqlite3.Shutdown()
	t.Logf("Sqlite3 Version: %s\n", sqlite3.LibVersion())

	dbh := new(sqlite3.Handle)
	err := dbh.Open("test.db")
	if err != "" {
		t.Errorf("Open test.db: %s", err)
	}
	defer dbh.Close()

	st, err := dbh.Prepare("CREATE TABLE foo (i INTEGER, s VARCHAR(20));")
	if err != "" {
		t.Errorf("Create Table: %s", err)
	} else {
		defer st.Finalize()
		st.Step()
	}

	st, err = dbh.Prepare("DROP TABLE foo;")
	if err != "" {
		t.Errorf("Drop Table: %s", err)
	} else {
		defer st.Finalize()
		st.Step()
	}
}
Esempio n. 2
0
func main() {
	sqlite3.Initialize()
	defer sqlite3.Shutdown()
	fmt.Printf("Sqlite3 Version: %s\n", sqlite3.LibVersion())

	dbh := new(sqlite3.Handle)
	dbh.Open("test.db")
	defer dbh.Close()

	st, err := dbh.Prepare("CREATE TABLE foo (i INTEGER, s VARCHAR(20));")
	if err != "" {
		println(err)
	} else {
		defer st.Finalize()
		st.Step()
	}

	st, err = dbh.Prepare("INSERT INTO foo values (2, 'this is a test')")
	if err != "" {
		println(err)
	} else {
		st.Step()
		st.Finalize()
	}

	st, err = dbh.Prepare("INSERT INTO foo values (3, 'holy moly')")
	if err != "" {
		println(err)
	} else {
		st.Step()
		st.Finalize()
	}

	fmt.Printf("%d changes\n", dbh.TotalChanges())

	fmt.Printf("last insert id: %d\n", dbh.LastInsertRowID())

	st, err = dbh.Prepare("SELECT * from foo limit 5;")
	if err != "" {
		println(err)
	} else {
		v, c, n := "", 0, ""
		for {
			c = st.Step()
			if c == 101 {
				break
			}
			n, v = st.ColumnText(0), st.ColumnText(1)
			fmt.Printf("data: %s, %s\n", n, v)
		}
		st.Finalize()
	}

}
Esempio n. 3
0
func main() {
	sqlite3.Initialize()
	defer func() {
		log.Stdout("closing sqlite3")
		sqlite3.Shutdown()
	}()

	dbh := new(sqlite3.Handle)
	dbh.Open("bbs.db")
	defer dbh.Close()

	InitTables(dbh)

	flag.Parse()
	templ := func() *template.Template {
		templateStr, err := io.ReadFile("tmpl/top.tmpl")
		if err != nil {
			log.Exit(err)
		}
		return template.MustParse(string(templateStr), nil)
	}()

	http.Handle("/", http.HandlerFunc(func(c *http.Conn, req *http.Request) {
		params := new(struct{ msgs []string })
		storage := new(vector.StringVector)
		func() {
			st, err := dbh.Prepare("SELECT * from entry ORDER BY id DESC limit 30;")
			func() {
				if err != "" {
					log.Exit(err)
				}
				for {
					rv := st.Step()
					switch {
					case rv == sqlite3.SQLITE_DONE:
						return
					case rv == sqlite3.SQLITE_ROW:
						body := st.ColumnText(1)
						storage.Push(body)
					default:
						println(rv)
						log.Exit(dbh.ErrMsg())
					}
				}
			}()
			if st.Finalize() != sqlite3.SQLITE_OK {
				log.Exit(dbh.ErrMsg())
			}
		}()
		params.msgs = storage.Data()
		err := templ.Execute(params, c)
		if err != nil {
			log.Exit("templ.Execute:", err)
		}
	}))
	http.Handle("/post", http.HandlerFunc(func(c *http.Conn, req *http.Request) {
		req.ParseForm()

		body := req.Form["body"][0]
		st, err := dbh.Prepare("INSERT INTO entry (body) VALUES (?)")
		if err != "" {
			log.Exit(err)
		}
		if st.BindText(1, body) != sqlite3.SQLITE_OK {
			log.Exit("cannot bind: ", dbh.ErrMsg())
		}
		if st.Step() != sqlite3.SQLITE_DONE {
			log.Exit(dbh.ErrMsg())
		}
		if st.Finalize() != sqlite3.SQLITE_OK {
			log.Exit(dbh.ErrMsg())
		}

		http.Redirect(c, "/", 302)
	}))
	http.Handle("/css/", http.HandlerFunc(func(c *http.Conn, req *http.Request) {
		http.ServeFile(c, req, "."+req.URL.Path)
	}))

	// run httpd
	func() {
		fmt.Printf("http://%s/\n", *addr)
		err := http.ListenAndServe(*addr, nil)
		if err != nil {
			log.Exit("ListenAndServe:", err)
		}
	}()
}
Esempio n. 4
0
func main() {
	sqlite3.Initialize()
	defer sqlite3.Shutdown()
	fmt.Printf("Sqlite3 Version: %s\n", sqlite3.LibVersion())

	dbh := new(sqlite3.Handle)
	dbh.Open("test.db")
	defer dbh.Close()

	st, err := dbh.Prepare("DROP TABLE IF EXISTS foo;")
	if err != "" {
		println(err)
	}
	st.Step()
	st.Finalize()

	st, err = dbh.Prepare("CREATE TABLE foo (i INTEGER, s VARCHAR(20));")
	if err != "" {
		println(err)
	} else {
		defer st.Finalize()
		st.Step()
	}

	st, err = dbh.Prepare("INSERT INTO foo values (2, 'this is a test')")
	if err != "" {
		println(err)
	} else {
		st.Step()
		st.Finalize()
	}

	st, err = dbh.Prepare("INSERT INTO foo values (?, ?)")
	if err != "" {
		println(err)
	} else {
		st.BindInt(1, 3)
		st.BindText(2, "holy moly")
		st.Step()
		st.Finalize()
	}

	fmt.Printf("%d changes\n", dbh.TotalChanges())

	fmt.Printf("last insert id: %d\n", dbh.LastInsertRowID())

	st, err = dbh.Prepare("SELECT * from foo limit 5;")
	if err != "" {
		println(err)
	} else {
		v, c, n := "", 0, ""
		func() {
			for {
				c = st.Step()
				switch {
				case c == sqlite3.SQLITE_DONE:
					return
				case c == sqlite3.SQLITE_ROW:
					n, v = st.ColumnText(0), st.ColumnText(1)
					fmt.Printf("data: %s, %s\n", n, v)
				default:
					println(dbh.ErrMsg())
					return
				}
			}
		}()
		st.Finalize()
	}

}