将字节切片插入SQLite的blob字段。

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

Insert byte slice into sqlite blob

问题

我尝试使用github.com/mattn/go-sqlite3将一个字节切片插入sqlite3数据库。

数据:

thmbnail := [255 216 255 219 0 132 ...]

创建语句:

sqlStmt := `
create table result (id INTEGER NOT NULL PRIMARY KEY, fname TEXT, path TEXT, 
size INTEGER, fMDate TEXT, fUUID TEXT, fSHA1 TEXT, fPRONOM TEXT, fNSRL INTEGER, fTHMB BLOB);
pragma journal_mode=WAL;
delete from result;
`

插入:

func addEntryDB(stmt *sql.Stmt, entry fileMD) {

    _, err := stmt.Exec(nil, entry.fName, entry.fPath, entry.fSize, entry.fMDate,
	entry.fUUID, entry.fSHA1, entry.fPRONOM, entry.fNSRL, entry.fTHMB)
    if err != nil {
	    log.Fatal(err)
    }
}

问题:只有thmbnail的前四个字节被插入。我猜想这可能与第五个位置上的0字节有关。

如何插入整个[]byte?

英文:

I try to insert a slice of bytes into a sqlite3 database, using github.com/mattn/go-sqlite3.

Data:

thmbnail := [255 216 255 219 0 132 ...]

Create statement:

sqlStmt := `
create table result (id INTEGER NOT NULL PRIMARY KEY, fname TEXT, path TEXT, 
size INTEGER, fMDate TEXT, fUUID TEXT, fSHA1 TEXT, fPRONOM TEXT, fNSRL INTEGER, fTHMB BLOB);
pragma journal_mode=WAL;
delete from result;
`

Insert:

func addEntryDB(stmt *sql.Stmt, entry fileMD) {

    _, err := stmt.Exec(nil, entry.fName, entry.fPath, entry.fSize, entry.fMDate,
	entry.fUUID, entry.fSHA1, entry.fPRONOM, entry.fNSRL, entry.fTHMB)
    if err != nil {
	    log.Fatal(err)
    }
}

Problem: Only the first four bytes of thmbnail are inserted. I suppose this might be related to the 0 byte on the fifth position.

How can the whole []byte be inserted?

答案1

得分: 1

我找到了问题。它坐在显示器前面。实际上,数据已经插入了。Sqlite客户端将blob处理为文本,并在“0”处终止打印。使用sqlitebrowser检查数据库时,显示了完整的条目。

英文:

I found the problem. It sat before the monitor.The data was inserted actually. Sqlite client handles the blob as text and terminates printing at the "0". Inspecting the database with sqlitebrowser showed the complete entry.

huangapple
  • 本文由 发表于 2017年6月10日 05:54:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/44467219.html
匿名

发表评论

匿名网友

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

确定