duanpei8853 2014-04-05 09:53
浏览 130
已采纳

去编程:sqlite_master使用sqlite3包返回EOF

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
}
}
  • 写回答

1条回答 默认 最新

  • douchi0471 2014-04-05 11:21
    关注

    The Stmt.Exec documentation says:

    No rows are returned.

    To return data, use Query instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。