One problem SQLite3 binding function families need to pay attention when using

Recent sqlite3_bind _ * () function encountered a problem when inserting data is garbled, and English and Chinese are garbled, and insert garbled data are the same, in addition to strings, other types of data is normal, the code as follows:

​
....
sqlite3_stmt *stmt;
unsigned int rec_offset = 0;
if (sqlite3_prepare_v2 (dbHandle, cmdString, strlen(cmdString), &stmt, NULL) == SQLITE_OK)
{
    for (unsigned int i = 0; i < row_num; ++i)
    {
        for (unsigned int k = 0; k < columnVector.size(); k++)
​​​​​​​        {
            switch (columnVector[k].type)
            {
                case DB_DATATYPE_STRING :
                {
                    char * tmp_char = (char*)malloc (columnVector[k].data_size);
                    memcpy (tmp_char, (char*)data_buffer + rec_offset, columnVector[k].data_size);
                    sqlite3_bind_text (stmt, k + 1, tmp_char, columnVector[k].data_size,								                   SQLITE_STATIC);
                    free (tmp_char);
                    break;
                }
                case DB_DATATYPE_INT:
                {
                    int tempInt;
                    memcpy (&tempInt, (char*)data_buffer + rec_offset, sizeof(int));
                    sqlite3_bind_int(stmt, k + 1, tempInt);
                    break;
                }
                ...
                default:
                {
                    cout<<"不支持的数据类型!!!!!"<<endl;
                    break;
                }
            }//switch 一行的每一列按类型进行绑定处理
            rec_offset += columnVector[k].data_size;
        }//for(k) 一行的所有列处理完毕
        int retcode = sqlite3_step (stmt);
        if ( retcode == SQLITE_DONE)
        {
            sqlite3_reset (stmt);
        }
    }//for(i) 所有数据绑定完毕
    int retcode = sqlite3_finalize (stmt);
    ...
}

​

After the Internet to find the problem, find a post ( https://www.jb51.net/article/110054.htm ) I felt a bit like my case, part of the code has been modified to solve the problem, because in sqlite3_step (stmt) before calling bind the binding of a row of data must still exist, can not be released, otherwise it can not really write.

Modify the code as follows:

​
​
....
if (sqlite3_prepare_v2 (dbHandle, cmdString, strlen(cmdString), &stmt, NULL) == SQLITE_OK)
{
    for (unsigned int i = 0; i < row_num; ++i)
    {
        //申请最大内存,这里假设字符串类型字段长度不超过500,每个字段都是字符串类型
        char *tmp_char = (char*)calloc(500 * columnVector.size(), 1);
        for (unsigned int k = 0; k < columnVector.size(); k++)
​​​​​​​        {
            switch (columnVector[k].type)
            {
                case DB_DATATYPE_STRING :
                {
                    memcpy (tmp_char + 500 * k, (char*)data_buffer + rec_offset, columnVector[k].data_size);
                    sqlite3_bind_text (stmt, k + 1, tmp_char + 500 * k, columnVector[k].data_size, SQLITE_STATIC);
                    break;
                }
                ...
            }//switch 一行的每一列按类型进行绑定处理
            rec_offset += columnVector[k].data_size;
        }//for(k) 一行的所有列处理完毕
        int retcode = sqlite3_step (stmt);
        free(tmp_char);  //释放申请的内存
        if ( retcode == SQLITE_DONE)
        {
            sqlite3_reset (stmt);
        }
    }//for(i) 所有数据绑定完毕
    int retcode = sqlite3_finalize (stmt);
    ...
}

​

​

Record this issue, hoping to help friends solve the same problem.

Published 65 original articles · won praise 34 · views 260 000 +

Guess you like

Origin blog.csdn.net/huanggang982/article/details/98167786