我正在尝试检查表创建后是否存在表,但"SELECT name FROM sqlite_master WHERE type='table' AND name='testtable';"
不返回任何内容(EOF
)。我做错了什么?
Sqlite3包取自http://code.google.com/p/go-sqlite/source/browse/#hg%2Fgo1%2Fsqlite3 转版: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
预期:
hello, world
FileExists(dbname) returned: false
database ok
creating testtable...
success!
inserting something...
checking testtable...
Table detected
代码:
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\n")
os.Remove(dbname)
fe := FileExists(dbname)
fmt.Printf("FileExists(dbname) returned: %t\n", fe)
db, err := sqlite3.Open(dbname)
defer db.Close()
if err != nil {
fmt.Printf("failed to open database, error: " + err.Error() + "\n")
return
}
fmt.Printf("database ok\n")
if fe != true {
fmt.Printf("creating testtable...\n")
err = db.Exec("CREATE TABLE testtable (id INTEGER PRIMARY KEY AUTOINCREMENT, text VARCHAR(200));")
if err != nil {
fmt.Printf("error: " + err.Error() + "\n")
return
} else {
fmt.Printf("success!\n")
}
fmt.Printf("inserting something...\n")
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() + "\n")
return
}
fmt.Printf("checking testtable...\n")
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() + "\n")
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() + "\n")
return
}
if tablename != "testtable" {
fmt.Printf("No table detected\n")
} else {
fmt.Printf("Table detected\n")
}
}
}
func FileExists(fn string) bool {
if _, err := os.Stat(fn); err == nil {
return true
} else {
return false
}
}