简体   繁体   English

C ++ MySQL插入数据库

[英]c++ mysql inserting in db

this is my code i am trying to insert ma in sma inside my data base but error occur as: too many arguments to function 'int mysql_query(MYSQL*,const char*)' note declared in mysql.h line in mysql.h : int STDCALL mysql_query(MYSQL *mysql, const char *q); 这是我的代码,我试图在数据库中的sma中插入ma,但是发生错误的原因是:在mysql.h的mysql.h行中声明了太多的函数'int mysql_query(MYSQL *,const char *)'注意: int STDCALL mysql_query(MYSQL * mysql,const char * q);

while(true)
    {
        mysql_query(conn, " select close, id from fivemin order by id DESC LIMIT 5 ");
    result = mysql_store_result(conn);
    num_fields = mysql_num_fields(result);
    float sum = 0;
    while((row=mysql_fetch_row(result)))
        {
                 sum += atof(row[0]);
                 last_id = atoi(row[1]);

        }
    float ma;
    ma=sum/5.0;
    if(previous_last_id != last_id)
        {
        cout << "Simple moving Average: " << ma << endl;
        previous_last_id = last_id;
        }

     mysql_query(conn,("insert into sma values('%f')"),ma);
    Sleep(1000);
}

mysql_query(conn,("insert into sma values('%f')"),ma) has three arguments. mysql_query(conn,("insert into sma values('%f')"),ma)有三个参数。

You need to format your string prior to call mysql_query. 您需要先格式化字符串,然后才能调用mysql_query.

Example: 例:

char str[80];
sprintf(str, "insert into sma values('%f')", ma);
mysql_query(conn, str);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM