SQLite Blob插入c ++ [英] SQLite Blob insertion c++

查看:267
本文介绍了SQLite Blob插入c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在访问了包含关于SQLite的信息的几十个网站后,我仍然找不到解决方案来修复绑定Blob时的错误。这里是表解析:

After visiting dozens of websites containing info about SQLite I still cannot find a solution to fix an error while binding a blob. Here is the table decleration:

CREATE TABLE ONE ( 
ID    INTEGER     PRIMARY KEY AUTOINCREMENT
                  NOT NULL,
NAME  CHAR( 50 )  NOT NULL,
LABEL CHAR( 50 ),
GRP   CHAR( 50 ),
FILE  BLOB 
);

这里是插入代码:

int InsertFile(string name)
{
const char* dbname = name.c_str();
sqlite3 *database;
int rc = sqlite3_open(dbname, &database);
char *zErrMsg = 0;
unsigned char *buffer = (unsigned char*) malloc(sizeof(char)*MAX);

ifstream file;
file.open("Sql.pdf", ios::in|ios::binary);

    if ( ! file )
{
        cout << "An error occurred opening the file" << endl;
}

int count = 0;

const void* fileptr = NULL;


fileptr = buffer;

while(file.good())
{
    char c=file.get();
    buffer[count]=c;
    count++;
}
file.close();


sqlite3_stmt *stmt = NULL;

char* statement = "INSERT INTO ONE(     ID,    NAME,    LABEL,    GRP,    FILE ) VALUES (     NULL,    'fedfsdfss',    NULL,    NULL,  ?);";

rc = sqlite3_prepare_v2(database, statement, 0, &stmt, NULL);


rc = sqlite3_bind_blob(stmt, 1, fileptr, sizeof(char)*count, SQLITE_TRANSIENT);


const char* result = sqlite3_errmsg(database);


rc = sqlite3_step(stmt);

result = sqlite3_errmsg(database);

sqlite3_close(database);


free(buffer);

fileptr=NULL;

return 0;

}
编辑:粘贴完整功能,试图插入的字符数约为350K。

} Pasted full function, the amount of characters im trying to insert is about 350K.

binb_blob的结果始终为21,错误代码不包含任何内容。缓冲区包含二进制文件数据,这最可能不是太大,因此错误代码。任何提示将被实施。

The result from binb_blob is always 21, error code contains nothing. buffer contains binary file data, which most probably isn't too big hence the error code. Any hints would be apprieciated.

推荐答案

您的代码包含太多错误。

Your code has too many errors to count.

类似这样:

int InsertFile(const string& db_name)
{
    ifstream file("Sql.pdf", ios::in | ios::binary);
    if (!file) {
        cerr << "An error occurred opening the file\n";
        return 12345;
    }
    file.seekg(0, ifstream::end);
    streampos size = file.tellg();
    file.seekg(0);

    char* buffer = new char[size];
    file.read(buffer, size);

    sqlite3 *db = NULL;
    int rc = sqlite3_open_v2(db_name.c_str(), &db, SQLITE_OPEN_READWRITE, NULL);
    if (rc != SQLITE_OK) {
        cerr << "db open failed: " << sqlite3_errmsg(db) << endl;
    } else {
        sqlite3_stmt *stmt = NULL;
        rc = sqlite3_prepare_v2(db,
                                "INSERT INTO ONE(ID, NAME, LABEL, GRP, FILE)"
                                " VALUES(NULL, 'fedfsdfss', NULL, NULL, ?)",
                                -1, &stmt, NULL);
        if (rc != SQLITE_OK) {
            cerr << "prepare failed: " << sqlite3_errmsg(db) << endl;
        } else {
            // SQLITE_STATIC because the statement is finalized
            // before the buffer is freed:
            rc = sqlite3_bind_blob(stmt, 1, buffer, size, SQLITE_STATIC);
            if (rc != SQLITE_OK) {
                cerr << "bind failed: " << sqlite3_errmsg(db) << endl;
            } else {
                rc = sqlite3_step(stmt);
                if (rc != SQLITE_DONE)
                    cerr << "execution failed: " << sqlite3_errmsg(db) << endl;
            }
        }
        sqlite3_finalize(stmt);
    }
    sqlite3_close(db);

    delete[] buffer;
}

这篇关于SQLite Blob插入c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆