C中的mysql INSERT语句 [英] Mysql INSERT statement in C

查看:107
本文介绍了C中的mysql INSERT语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里很忙,无法解决我的问题.我查看了许多应该解决的错误的答案.

I'm going loopy here and cannot fix my problem. I have looked at many supposed answer that do not fix my error.

我正在尝试将温度和湿度数据从DHT11传感器获取到mysql数据库中.我可以让终端显示读数,并且一切正常,除了无法将数据输入数据库.编译c代码没有问题.它只是不会填充数据库.

I am trying to get temp and humidity data from a DHT11 sensor into a mysql database. I can get the terminal to display the readings and it all works fine except I cannot get the data into the database. There are no problems compiling the c code. It just wont populate the database.

  char query[2000];
  MYSQL *conn;
  conn = mysql_init(NULL);
  mysql_real_connect(conn, "localhost", "drewibbo", "monkeykangaroo", "temp_humidity", 0, NULL, 0);
  sprintf(query, "INSERT INTO readings(temp,humidity,date,time) VALUES(%d.%d,%d.%d,2015-02-18,00:00:00)",dht11_val[2],dht11_val[3],dht11_val[0],dht11_$
  mysql_query(conn, query);
  mysql_close(conn);

感谢您能提供的任何帮助.

Thanks for any help that can be given.

安迪

推荐答案

您的INSERT查询语法错误,您需要将日期和时间放在引号中,例如.

Your INSERT query has wrong syntax, you need to put the date and the time into quotes, like.

sprintf(query, "INSERT INTO readings(temp,humidity,date,time) VALUES(%d.%d,%d.%d,'2015-02-18','00:00:00')",dht11_val[2],dht11_val[3],dht11_val[0],dht11_ ...

您的查询可能还有其他问题,这只是我注意到的一个问题.如果发生错误,只需使代码报告错误即可: http://dev .mysql.com/doc/refman/5.1/en/mysql-error.html

Your query might have other issues, this is just one that I noticed. Just make your code report errors if they occur: http://dev.mysql.com/doc/refman/5.1/en/mysql-error.html

这篇关于C中的mysql INSERT语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆