向MYSQL数据库添加数据的C程序-未添加结果

向MYSQL数据库添加数据的C程序-未添加结果,mysql,sql,c,Mysql,Sql,C,我正在尝试构建一个C程序,将数据添加到MYSQL数据库中,我希望在SQL字符串中使用变量。我想在一列中插入UNIX时间(历元),在另一列中插入电能表的结果(双精度) 即使它构建时没有错误或警告,我也无法让它将数据插入表中。有人能告诉我该去哪里找吗 感谢所有我能得到的帮助,因为我几乎在黑暗中摸索 问候,, 米凯尔 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包含//用于退出功能 int loggingok;//指示登录或注销的全局变量 双结果=323.234567//调试 双倍实际时

我正在尝试构建一个C程序,将数据添加到MYSQL数据库中,我希望在SQL字符串中使用变量。我想在一列中插入UNIX时间(历元),在另一列中插入电能表的结果(双精度)

即使它构建时没有错误或警告,我也无法让它将数据插入表中。有人能告诉我该去哪里找吗

感谢所有我能得到的帮助,因为我几乎在黑暗中摸索

问候,, 米凯尔

#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包含//用于退出功能
int loggingok;//指示登录或注销的全局变量
双结果=323.234567//调试
双倍实际时间秒;
计算瓦特的空隙(空隙)
{
MYSQL*conn=MYSQL_init(NULL);
char*server=“localhost”;
char*user=“root”;
char*password=“password”/*首先设置我*/
char*database=“power”;
字符SQL_字符串[100];
char time_char[11];
char result_char[11];
时间不是实际时间;
实际时间=时间(0);
实际_时间_秒=difftime(实际_时间,0);
sprintf(时间字符,“%g”,实际时间秒);
sprintf(结果字符,“%g”,结果);
printf(“Tid:%g\n”,实际时间(秒);//调试
printf(“结果:%g\n”,result);//调试
strcpy(SQL_字符串,“插入消耗(时间、消耗)值(”);
strcat(SQL\u字符串、time\u字符);
strcat(SQL_字符串,“,”);
strcat(SQL字符串、结果字符);
strcat(SQL_字符串“)”;
printf(“SQL:%s”,SQL_字符串);//调试
//SQL_String=“插入消耗(时间、消耗)值(“+actual_time_sec+”,“+result+”);
if(!mysql\u real\u connect(连接、服务器、用户、密码、数据库、0、NULL、0)){
fprintf(stderr,“%s\n”,mysql_错误(conn));
退出(1);}
if(mysql\u查询(conn,SQL\u字符串)){
fprintf(stderr,“%s\n”,mysql_错误(conn));
退出(1);}
}
int main(int argc,char*argv[])
{
printf(argv[1]);//调试
if(strcmp(argv[1],“db”)){
loggingok=1;
printf(“Efergy E2经典解码\n\n”);
计算_瓦特();}
否则{
loggingok=0;}
返回0;
}

这是一个很好的例子

。注意这里sprintf的用法。第二个和第三个参数与sprintf一起工作,将这两个参数中的数据精确地放置在SQL语句中所需的位置

$query = sprintf("SELECT firstname, lastname, address, age FROM friends 
    WHERE firstname='%s' AND lastname='%s'",
    mysql_real_escape_string($firstname),
    mysql_real_escape_string($lastname));

// Perform Query
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

请注意if(!$result)的用法这允许您快速验证您是否成功。如果发现错误,则从mysql_error重新返回的文本将放置在消息变量中,然后在应用程序死亡时显示。

按照您的逻辑,如果(!mysql_query(conn,SQL_String)){则不应该是
$query = sprintf("SELECT firstname, lastname, address, age FROM friends 
    WHERE firstname='%s' AND lastname='%s'",
    mysql_real_escape_string($firstname),
    mysql_real_escape_string($lastname));

// Perform Query
$result = mysql_query($query);

// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}