英文:
go programming: sqlite_master returns EOF using sqlite3 package
问题
我正在尝试在创建表后检查表是否存在,但是"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
Go版本:1.2.1
实际结果:
hello, world
FileExists(dbname)返回:false
数据库正常
创建testtable...
成功!
插入一些内容...
检查testtable...
扫描变量失败,错误:EOF
期望结果:
hello, world
FileExists(dbname)返回:false
数据库正常
创建testtable...
成功!
插入一些内容...
检查testtable...
检测到表
代码:
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)返回:%t\n", fe)
db, err := sqlite3.Open(dbname)
defer db.Close()
if err != nil {
fmt.Printf("无法打开数据库,错误:" + err.Error() + "\n")
return
}
fmt.Printf("数据库正常\n")
if fe != true {
fmt.Printf("创建testtable...\n")
err = db.Exec("CREATE TABLE testtable (id INTEGER PRIMARY KEY AUTOINCREMENT, text VARCHAR(200));")
if err != nil {
fmt.Printf("错误:" + err.Error() + "\n")
return
} else {
fmt.Printf("成功!\n")
}
fmt.Printf("插入一些内容...\n")
insertSql := `INSERT INTO testtable(text) VALUES("This is some random text to test it");`
err = db.Exec(insertSql)
if err != nil {
fmt.Printf("插入时出错:" + err.Error() + "\n")
return
}
fmt.Printf("检查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("无法检查表,错误:" + err.Error() + "\n")
return
}
var tablename string
//移除'//'获得相同结果
//requeststatus := CheckTable.Next()
err = CheckTable.Scan(&tablename)
if err != nil {
fmt.Printf("扫描变量失败,错误:" + err.Error() + "\n")
return
}
if tablename != "testtable" {
fmt.Printf("未检测到表\n")
} else {
fmt.Printf("检测到表\n")
}
}
}
func FileExists(fn string) bool {
if _, err := os.Stat(fn); err == nil {
return true
} else {
return false
}
}
英文:
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\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
}
}
答案1
得分: 1
Stmt.Exec文档中写道:
> 不返回任何行。
要返回数据,请改用Query。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论