【问题标题】:Write data read from database to binary file将从数据库读取的数据写入二进制文件
【发布时间】:2015-05-06 08:19:22
【问题描述】:

我想将从数据库中读取的数据写入二进制文件。在查询中,我需要读取大量数据,其中一些是group_concat。现在我想将它们写入二进制文件。问题是我如何定义它们的大小。起初我使用put(),但结果是错误的,因为put 需要char 的类型,而row[]char*。然后我尝试了write 并通过sizeof(int)*group 中元素的数量定义了大小,但它仍然是错误的。请告诉我一个方法。

MYSQL_RES* res = GetDBManager()->Query("select ls.time, count(ws.id) , group_concat(wc.id) , group_concat(ws.SIGNAL_STRENGTH) , ul.LONGITUDE , ul.LATITUDE , ul.ALTITUDE  from user_location_scan ls, wifi_scan ws, wifi_cell wc, user_location ul where ls.id = ws.user_scan and ws.wifi_cell = wc.id and ls.time = ul.time group by ls.id order by ls.id");
    if (res == NULL) return false;

MYSQL_ROW row;

ofstream myFile("data.bin", ios::out | ios::binary | ios::app);

while (row = mysql_fetch_row(res)) {
    myFile.write(row[0], sizeof(char));
    myFile.write(row[1], sizeof(int));
    myFile.write(row[2], sizeof(int)*atoi(row[1]));
    myFile.write(row[3], sizeof(int)*atoi(row[1]));
    myFile.write(row[4], sizeof(double));
    myFile.write(row[5], sizeof(double));
    myFile.write(row[6], sizeof(double));
    //myFile.write(row[0], sizeof(row[0]));
    //myFile.write(row[1], sizeof(row[1]));
    //myFile.write(row[2], sizeof(row[2]));
    //myFile.write(row[3], sizeof(row[3]));
    //myFile.write(row[4], sizeof(row[4]));
    //myFile.write(row[5], sizeof(row[5]));
    //myFile.write(row[6], sizeof(row[6]));
    //myFile.put((char)row[0]);
    //myFile.put((char)row[1]);
    //myFile.put((char)row[2]);
    //myFile.put((char)row[3]);
    //myFile.put((char)row[4]);
    //myFile.put((char)row[5]);
    //myFile.put((char)row[6]);
    //myFile << row[0] << row[1] << row[2] << row[3] << row[4] << row[5] << row[6];

}
mysql_free_result(res);

【问题讨论】:

  • 什么是group_concat
  • 好的,让我给你看我的代码
  • 这就是我今天早上所尝试的
  • 不要使用sizeof() 尝试使用strlen(row[0]) + 1 来给出字符串的长度,包括空终止符。
  • @Galik wow 就像一个魅力。非常感谢你。无论如何,你能给我一些关于你刚才所说的话的文件吗?

标签: c++ database binary


【解决方案1】:

MySql 函数mysql_fetch_row() 返回一个以空字符结尾的字符串数组(字符数组)。您可以使用std::strlen() 来获取这些以空字符结尾的字符数组的长度。

如果您想将所有内容作为字符串写入文件,那么执行以下操作就足够了:

while((row = mysql_fetch_row(res)))
    for(unsigned col = 0; col < 7; ++col)
        myFile.write(row[col], std::strlen(row[col]) + 1);

如果你想将数字列写成二进制数(更难移植),那么你需要转换字符串并以不同的方式写出值。

更不推荐是用二进制写出每条记录。这是非常不便携的。

struct record
{
    // choose better column names
    char col_0[32]; // largest possible string
    int col_1;
    int col_2;
    int col_3;
    double col_4;
    double col_5;
    double col_6;
};

while((row = mysql_fetch_row(res)))
{
    record r; // store our binary values here

    //copy the string
    std::strncpy(r.col_0, row[0], sizeof(r.col_0));

    r.col_1 = std::atoi(row[1]); // convert the integers
    r.col_2 = std::atoi(row[2]);
    r.col_3 = std::atoi(row[3]);

    r.col_4 = std::atof(row[4]); // convert the floats
    r.col_5 = std::atof(row[5]);
    r.col_6 = std::atof(row[6]);

    // write the whole object out in binary (very non-portable)
    myFile.write(reinterpret_cast<char*>(&r), sizeof(r));
}

然后再次读取记录:

// reading back

record r; // store our binary values here

if(myFile.read(reinterpret_cast<char*>(&r), sizeof(r)))
{
    // success, use the record here
    std::cout << r.col_0 << '\n';
    std::cout << r.col_1 << '\n';
    std::cout << r.col_2 << '\n';
    // ... etc
}

注意:使用二进制更复杂、更难正确、不可移植,除非绝对必要,否则通常应避免使用。

【讨论】:

  • 非常感谢。起初我打算将它们转换为数字类型,以便以后更容易阅读。但我认为以字符串形式写入仍然很好,因为我可以在从文件中读取数据时将正确的类型转换为数据,不是吗?
  • @DucAnhNguyen 以字符串形式编写更可取,因为它非常便携且网络友好。但是,我将发布一个方法,您可以使用它以二进制形式写出所有内容,但它非常不可移植(即使在同一编译器上使用不同标志编译的版本之间)。
  • @DucAnhNguyen 正如您所见,使用二进制更复杂、更难正确、不可移植,除非绝对要求,否则通常应避免使用。
  • 你是对的。实际上一开始我使用 JSON 来避免二进制,但我的导师要求我使用二进制。我想他想让我知道如何使用不同的种类。再次感谢您,希望您有愉快的一天!
猜你喜欢
  • 2021-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-15
  • 2011-04-08
  • 2011-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多