gpt4 book ai didi

c - 一个C cgi脚本,用于从sqlite3_column_blob指针提供二进制文件

转载 作者:行者123 更新时间:2023-12-03 13:18:24 25 4
gpt4 key购买 nike

我从sqlite数据库检索了二进制数据,现在我有了指向二进制数据(sqlite3_column_blob)的指针。如何使用C cgi脚本将此数据输出到客户端?

char *sql = "SELECT Data FROM Images WHERE Id = 1";

sqlite3_stmt *pStmt;
rc = sqlite3_prepare_v2(db, sql, -1, &pStmt, 0);

if (rc != SQLITE_OK ) {

fprintf(stderr, "Failed to prepare statement\n");
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));

sqlite3_close(db);

return 1;
}

rc = sqlite3_step(pStmt);

int bytes = 0;

if (rc == SQLITE_ROW) {

bytes = sqlite3_column_bytes(pStmt, 0);
}
// >>>>next row is function to save file but I want output binary data to client<<<<
fwrite(sqlite3_column_blob(pStmt, 0), bytes, 1, fp);

最佳答案

好的,谢谢大家的支持。在我要查找的代码下面,只需将“ sqlite3_column_blob”发送到stdout ...就可以了!

int main(void) {
sqlite3 *db;
char *err_msg = 0;
int rc = sqlite3_open("./test.db", &db);
if (rc != SQLITE_OK) {
// error opening the SQLite database, send an error message to
// requesting application as plain text.
fprintf(stdout, "%s", "Content-Type: text/plain;\n\n");
fprintf(stdout, "Cannot open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}

char *sql = "SELECT Data FROM Images WHERE Id = 1";
sqlite3_stmt *pStmt;
rc = sqlite3_prepare_v2(db, sql, -1, &pStmt, 0);
if (rc != SQLITE_OK ) {
// error with SQLite retrieving data, send an error message to
// requesting application as plain text.
fprintf(stdout, "%s", "Content-Type: text/plain;\n\n");
fprintf(stdout, "Failed to prepare statement\n");
fprintf(stdout, "Database error: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}

rc = sqlite3_step(pStmt);
int bytes = 0;
if (rc == SQLITE_ROW) {
// send the content type HTTP response directive to requesting
// application. the SQLite BLOB contains JPEG image data.
fprintf(stdout, "%s", "Content-Type: image/jpeg;\n\n");
bytes = sqlite3_column_bytes(pStmt, 0);
}

bytes = sqlite3_column_bytes(pStmt, 0);
// send the image data from the SQLite BLOB to the requesting application
fwrite(sqlite3_column_blob(pStmt, 0), bytes, 1, stdout);

// cleanup and close SQLite connection and exit
rc = sqlite3_finalize(pStmt);
sqlite3_close(db);

return 0;
}

关于c - 一个C cgi脚本,用于从sqlite3_column_blob指针提供二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30709317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com