FileStorage - > MySQL的

时间:2013-07-17 09:39:04

标签: c++ mysql

抱歉我的英语:)尝试改进我的应用程序我想将矢量直接复制到没有字符串的mysql中,它甚至可能吗?

FileStorage fs( ".yml", FileStorage::WRITE + FileStorage::MEMORY );

Ptr < FaceRecognizer > model0 = createmyfisherRecognizer();

model0->train( images, labels );

model0->save( fs );




void myfisher::save( FileStorage & fs ) const {

    static char * host = "localhost"; /* host serwera MySQL */
    static char * user = "root"; /* nazwa loginu by polaczyc sie do serwera */
    static char * password = ""; /* haslo */
    static char * sql_db = "fisher"; /* nazwa bazy */
    static unsigned int sql_port = NULL; /* port na jakim odbiera/wysyla serwer mysql */
    static char * opt_socket = NULL; /* socket name */
    static unsigned int sql_flags = 0;
    static MYSQL * sock; /* Wskaznik do polaczenia do MySQL */

    sock = mysql_init( NULL );

    if( mysql_real_connect( sock, host, user, password, sql_db, sql_port, opt_socket, sql_flags ) )
         cout << "Poprawnie polaczyles sie z baza danych" << endl;
    else
         cout << "Blad w polaczeniu z baza danych" << endl;

    fs << "num_components" << _num_components;
    fs << "mean" << _mean;
    fs << "eigenvalues" << _eigenvalues;
    writeFileNodeList( fs, "projections", _projections );
    fs << "labels" << _labels;

    string buf = fs.releaseAndGetString();

    fs.release();

    string zapytanie = "insert into baa values (\"" + buf + "\")";

    if( !mysql_query( sock, zapytanie.c_str() ) )
         cout << "Poprawnie dodano" << endl;
    else
         cout << "Blad przy zapisywaniu" << endl;

}

我在这里发布了有问题的代码。目前它以这种方式运作:

  1. 一切都保存到fs(FileStorage)
  2. fs中的数据转换为字符串
  3. fs发送到数据库 众所周知,如果程序在没有字符串的情况下直接复制到数据库,程序的运行速度会快得多。

1 个答案:

答案 0 :(得分:0)

我认为你可以使用snprintf,不幸的是因为缺少变量类型我无法给你一个确切的例子。

char string[100];
snprintf(string, 100, "printe text %s,\n and integer %i", "printf", 100); //almost same as printf