Go编程:使用sqlite3包时,sqlite_master返回EOF

huangapple go评论160阅读模式
英文:

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 &quot;os&quot;
import &quot;fmt&quot;
import &quot;time&quot;
import &quot;code.google.com/p/go-sqlite/go1/sqlite3&quot;
func main() {
dbname := &quot;sqlite.db&quot;
defer time.Sleep(5000 * time.Millisecond)
fmt.Printf(&quot;hello, world\n&quot;)
os.Remove(dbname)
fe := FileExists(dbname)
fmt.Printf(&quot;FileExists(dbname) returned: %t\n&quot;, fe) 
db, err := sqlite3.Open(dbname)
defer db.Close()
if err != nil {
    fmt.Printf(&quot;failed to open database, error: &quot; + err.Error() + &quot;\n&quot;) 
    return
}
fmt.Printf(&quot;database ok\n&quot;)
if fe != true {
    fmt.Printf(&quot;creating testtable...\n&quot;) 
    err = db.Exec(&quot;CREATE TABLE testtable (id INTEGER PRIMARY KEY AUTOINCREMENT, text VARCHAR(200));&quot;)
    if err != nil {
        fmt.Printf(&quot;error: &quot; + err.Error() + &quot;\n&quot;) 
        return
    } else {
        fmt.Printf(&quot;success!\n&quot;) 
    }
    fmt.Printf(&quot;inserting something...\n&quot;) 
    insertSql := `INSERT INTO testtable(text) VALUES(&quot;This is some random text to test it&quot;);`
    err = db.Exec(insertSql)
    if err != nil {
        fmt.Printf(&quot;Error while Inserting: &quot; + err.Error() + &quot;\n&quot;)
        return
    }
    fmt.Printf(&quot;checking testtable...\n&quot;) 
    CheckTable, err := db.Prepare(&quot;SELECT name FROM sqlite_master WHERE type=&#39;table&#39; AND name=&#39;testtable&#39;;&quot;)
    err = CheckTable.Exec()
    if err != nil {
        fmt.Printf(&quot;failed to check table, error: &quot; + err.Error() + &quot;\n&quot;)
        return
    }
    var tablename string
    //Same result removing &#39;//&#39;
    //requeststatus := CheckTable.Next() 
        err = CheckTable.Scan(&amp;tablename)
        if err != nil {
            fmt.Printf(&quot;Failed to scan variable, error: &quot; + err.Error() + &quot;\n&quot;)
            return
        }
    if tablename != &quot;testtable&quot; {
        fmt.Printf(&quot;No table detected\n&quot;)
    } else {
    fmt.Printf(&quot;Table detected\n&quot;)
    }
}
}


func FileExists(fn string) bool {
if _, err := os.Stat(fn); err == nil {
    return true
} else {
    return false
}
}

答案1

得分: 1

Stmt.Exec文档中写道:
> 不返回任何行。

要返回数据,请改用Query

英文:

The Stmt.Exec documentation says:
> No rows are returned.

To return data, use Query instead.

huangapple
  • 本文由 发表于 2014年4月5日 17:53:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/22879291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定