英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论