|
我现在在用C语言和MYSQL来编写代码,但是遇到下列问题:
mysql_query(mysql, "select src,dst,channel,billsec,answer,end,amaflags,disposition from cdr where amaflags=3 and disposition=???");
在问号(???)这里我想写变量名,而不是常量,但不知道怎么进行字符串拼接,试过很多次都不行,代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
int main()
{
MYSQL *mysql;
MYSQL_RES *result;
MYSQL_ROW row;
unsigned int num_fields,num_rows;
int i,num = 1;
char* dis;
dis = malloc(100);
dis = "ANSWERED";
mysql = malloc(sizeof(MYSQL));
if ( !(mysql = mysql_connect(NULL, "localhost","root",NULL))){
fprintf(stderr, "Failed to connect to database: Errors: %s\n", mysql_error(mysql));
return -1;
}
if (mysql_select_db(mysql, "cdr") )
{
printf("select db wrong: %s \n", mysql_error(mysql));
return -1;
}
mysql_query(mysql, "select src,dst,channel,billsec,answer,end,amaflags,disposition from cdr where amaflags=3 and disposition=$dis);
result = mysql_store_result(mysql);
if (result == NULL)
{
printf("Error: %s\n", mysql_error(mysql));
return -1;
}
num_fields = mysql_num_fields(result);
num_rows = mysql_num_rows(result);
while((row = mysql_fetch_row(result)))
{
printf("%d\n",num);
for( i = 0; i < num_fields; i++ ){
printf("%s\n",row);
}
num++;
printf("\n");
}
mysql_free_result(result);
mysql_close(mysql);
return 0;
}
|
|