Skip to content

Instantly share code, notes, and snippets.

@titanous
Last active December 18, 2015 03:29
Show Gist options
  • Save titanous/5718296 to your computer and use it in GitHub Desktop.
Save titanous/5718296 to your computer and use it in GitHub Desktop.
package main
import (
"database/sql"
"fmt"
_ "code.google.com/p/go-sqlite/go1/sqlite3"
)
var a string
var b = ""
func main() {
db, _ := sql.Open("sqlite3", "test.db")
db.Exec("CREATE TABLE a (a)")
c := ""
var d string
var e = ""
f := make([]string, 1)
g := []string{""}
h := make(map[int]string)
i := map[int]string{0: ""}
j := struct{ a string }{}
k := struct{ a string }{""}
s, _ := db.Prepare("INSERT INTO a (a) VALUES (?)")
s.Exec("")
s.Exec(a)
s.Exec(b)
s.Exec(c)
s.Exec(d)
s.Exec(e)
s.Exec(f[0])
s.Exec(g[0])
s.Exec(h[0])
s.Exec(i[0])
s.Exec(j.a)
s.Exec(k.a)
for z := 1; z <= 12; z++ {
var out *string
err := db.QueryRow("SELECT a FROM a WHERE rowid = ? AND a IS NOT NULL", z).Scan(&out)
if err == sql.ErrNoRows {
fmt.Println(string(" abcdefghijk"[z-1]), "is NULL")
}
}
var count int
db.QueryRow("SELECT COUNT(*) FROM a").Scan(&count)
fmt.Println("count:", count)
}
// go version go1.1 darwin/amd64 OUTPUT:
// a is NULL
// b is NULL
// d is NULL
// f is NULL
// h is NULL
// j is NULL
// count: 12
@titanous
Copy link
Author

titanous commented Jun 6, 2013

Probably has to do with this:

// cStr returns a pointer to the first byte in s. s must end with '\x00' to be
// used as a regular C string.
func cStr(s string) *C.char {
        h := (*reflect.StringHeader)(unsafe.Pointer(&s))
        return (*C.char)(unsafe.Pointer(h.Data))
}

https://code.google.com/p/go-sqlite/source/browse/go1/sqlite3/util.go#267

@titanous
Copy link
Author

titanous commented Jun 6, 2013

Quick fix:

+var emptyStr = C.CString("")
+
 // cStr returns a pointer to the first byte in s. s must end with '\x00' to be
 // used as a regular C string.
 func cStr(s string) *C.char {
+       if len(s) == 0 {
+               return emptyStr
+       }
        h := (*reflect.StringHeader)(unsafe.Pointer(&s))
        return (*C.char)(unsafe.Pointer(h.Data))
 }

@titanous
Copy link
Author

titanous commented Jun 6, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment