文件存储 --> mysql

FileStorage --> mysql

本文关键字:mysql gt 存储 文件      更新时间:2023-10-16

对不起我的英语:)为了改进我的应用程序,我想在没有字符串的情况下直接将矢量复制到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已发送到数据库众所周知,如果不将字符串直接复制到数据库中,程序运行速度会快得多

我认为您可以使用snprintf来实现这一点,但不幸的是,由于缺少变量类型,我无法给您提供确切的示例。

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