C++ SQLite Blob插入c++;

C++ SQLite Blob插入c++;,c++,sqlite,C++,Sqlite,在访问了几十个包含SQLite信息的网站之后,我仍然找不到一个解决方案来修复绑定blob时的错误。以下是表中的偏差: CREATE TABLE ONE ( ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, NAME CHAR( 50 ) NOT NULL, LABEL CHAR( 50 ), GRP CHAR( 50 ), FILE BLOB ); 下面是插入代码: int Insert

在访问了几十个包含SQLite信息的网站之后,我仍然找不到一个解决方案来修复绑定blob时的错误。以下是表中的偏差:

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;
int插入文件(字符串名称)
{
const char*dbname=name.c_str();
sqlite3*数据库;
int rc=sqlite3_open(dbname和数据库);
char*zErrMsg=0;
无符号字符*缓冲区=(无符号字符*)malloc(sizeof(字符)*最大值);
ifstream文件;
open(“Sql.pdf”,ios::in | ios::binary);
如果(!文件)
{

cout您的代码错误太多,无法计数

试着这样做:

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;
}
int插入文件(常量字符串和db_名称)
{
ifstream文件(“Sql.pdf”,ios::in | ios::binary);
如果(!文件){

cerr
fileptr=&buffer;
就是其中之一。也有可能
count
是假的——我们看不出是如何设置的。count是缓冲区中的字符数,fileptr有什么问题?放置缓冲区本身不会改变任何东西。为什么选择缓冲区地址的地址??(作为实验,替换为bind语句中带有“NULL”的“fileptr”。我打赌它运行时不会出错。)(至少假设数据库已成功打开——您是否检查了open中的返回代码??(您是否检查了prepare中的返回代码??)