I am trying to check if table exists after table creation but "SELECT name FROM sqlite_master WHERE type='table' AND name='testtable';"
returns nothing (EOF
). What I am doing wrong?
Sqlite3 package is taken from http://code.google.com/p/go-sqlite/source/browse/#hg%2Fgo1%2Fsqlite3 Go version: 1.2.1
Got:
hello, world
FileExists(dbname) returned: false
database ok
creating testtable...
success!
inserting something...
checking testtable...
Failed to scan variable, error: EOF
Expected:
hello, world
FileExists(dbname) returned: false
database ok
creating testtable...
success!
inserting something...
checking testtable...
Table detected
Code:
package main
import "os"
import "fmt"
import "time"
import "code.google.com/p/go-sqlite/go1/sqlite3"
func main() {
dbname := "sqlite.db"
defer time.Sleep(5000 * time.Millisecond)
fmt.Printf("hello, world
")
os.Remove(dbname)
fe := FileExists(dbname)
fmt.Printf("FileExists(dbname) returned: %t
", fe)
db, err := sqlite3.Open(dbname)
defer db.Close()
if err != nil {
fmt.Printf("failed to open database, error: " + err.Error() + "
")
return
}
fmt.Printf("database ok
")
if fe != true {
fmt.Printf("creating testtable...
")
err = db.Exec("CREATE TABLE testtable (id INTEGER PRIMARY KEY AUTOINCREMENT, text VARCHAR(200));")
if err != nil {
fmt.Printf("error: " + err.Error() + "
")
return
} else {
fmt.Printf("success!
")
}
fmt.Printf("inserting something...
")
insertSql := `INSERT INTO testtable(text) VALUES("This is some random text to test it");`
err = db.Exec(insertSql)
if err != nil {
fmt.Printf("Error while Inserting: " + err.Error() + "
")
return
}
fmt.Printf("checking testtable...
")
CheckTable, err := db.Prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='testtable';")
err = CheckTable.Exec()
if err != nil {
fmt.Printf("failed to check table, error: " + err.Error() + "
")
return
}
var tablename string
//Same result removing '//'
//requeststatus := CheckTable.Next()
err = CheckTable.Scan(&tablename)
if err != nil {
fmt.Printf("Failed to scan variable, error: " + err.Error() + "
")
return
}
if tablename != "testtable" {
fmt.Printf("No table detected
")
} else {
fmt.Printf("Table detected
")
}
}
}
func FileExists(fn string) bool {
if _, err := os.Stat(fn); err == nil {
return true
} else {
return false
}
}