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 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝