I'm trying to insert values of temperature readed from DS18B20 sensor in mysql database using sprintf to store variables in string and mysql_query to write them in database. In database i have two column "temp" and "date", which are int and string data types.
In my previous database all was working perfect, but in this i have some problems.
Default data in columns is NOT NULL
As the result i see temperature and date in terminal, but
SELECT * FROM tmpdate1
gives me "Empty set"
Please help me to find out where the problem is and solve it
Here is my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "libfahw.h"
#include <mysql.h>
#include <unistd.h>
#include <time.h>
#define BUF_SIZE (64)
int main(int argc, char ** argv)
{
time_t t;
char times[1024]={};
char timestr[] = {"%s"};
time(&t);
MYSQL *conn;
char query[1024]={};
char query1[1024]={};
char query_str[] = {"INSERT INTO tmpdate1(temp) VALUES (%d)"};
char query_str1[] = {"INSERT INTO tmpdate1(date) VALUES (%s)"};
if ((conn=mysql_init(NULL))==NULL)
{
fprintf(stderr, "Could not init DB\n");
return EXIT_FAILURE;
}
char temperature[BUF_SIZE];
memset(temperature, 0, BUF_SIZE);
if(mysql_real_connect(conn, "localhost", "user12", "34klq*", "defense3", 0, NULL, 0) == NULL)
{
fprintf(stderr, "DB connection error\n");
return EXIT_FAILURE;
}
for (int i = 1; i>0; i++)
{
printf("\n %d", atoi(temperature)/1000);
printf("\n %s", ctime(&t));
sprintf(times, timestr, ctime(&t));
sprintf(query, query_str, atoi(temperature)/1000);
sprintf(query1, query_str1, timestr);
mysql_query(conn, query);
mysql_query(conn, query1);
}
return 0;
}
Related
Consider the following reproducer:
#include <wget.h>
#include <string.h>
#include <mysql.h>
MYSQL *mysql_con;
void
mysql_stop()
{
mysql_close(mysql_con);
}
void
mysql_start()
{
mysql_con = mysql_init(NULL);
if (mysql_con == NULL)
{
fprintf(stderr, "%s\n", mysql_error(mysql_con));
exit(1);
}
if (mysql_real_connect(mysql_con, "localhost", "crawler", "password", "crawl", 0, NULL, 0) == NULL)
{
fprintf(stderr, "%s\n", mysql_error(mysql_con));
exit(EXIT_FAILURE);
}
}
const char * parseURI (char *base_url, char *url)
{
wget_iri *base = wget_iri_parse(base_url, NULL);
wget_buffer *buf = wget_buffer_alloc(8192);
const char *uri = wget_iri_relative_to_abs(base, url, strlen(url), buf);
wget_buffer_free(&buf);
wget_iri_free(&base);
return uri;
}
void crawler_init()
{
const char *url2 = parseURI("http://www.google.com/", "/intl/en/policies/privacy/");
printf("%s\n", url2);
}
int main (int argc, char **argv)
{
const char *url2 = parseURI("http://www.google.com/", "/intl/en/policies/privacy/");
printf("%s\n", url2);
mysql_start();
crawler_init();
mysql_stop();
}
The output from running this program is:
http://www.google.com/intl/en/policies/privacy/
��7�
The first printf in main is printing out the absolute url as expected.
The second printf in crawler_init is printing out garbage instead of the same absolute url.
This appears to be related to mysql_real_connect as when this call is commented out the second printf in crawler_init prints out the expected absolute url.
Why?
Adding buf->data = NULL; fixes this:
#include <wget.h>
#include <string.h>
#include <mysql.h>
MYSQL *mysql_con;
void
mysql_stop()
{
mysql_close(mysql_con);
}
void
mysql_start()
{
mysql_con = mysql_init(NULL);
if (mysql_con == NULL)
{
fprintf(stderr, "%s\n", mysql_error(mysql_con));
exit(1);
}
if (mysql_real_connect(mysql_con, "localhost", "crawler", "1q2w3e4r", "crawl", 0, NULL, 0) == NULL)
{
fprintf(stderr, "%s\n", mysql_error(mysql_con));
exit(EXIT_FAILURE);
}
}
const char * parseURI (char *base_url, char *url)
{
wget_iri *base = wget_iri_parse(base_url, NULL);
wget_buffer *buf = wget_buffer_alloc(8192);
const char *uri = wget_iri_relative_to_abs(base, url, strlen(url), buf);
buf->data = NULL;
wget_buffer_free(&buf);
wget_iri_free(&base);
return uri;
}
void crawler_init()
{
const char *url2 = parseURI("http://www.google.com/", "/intl/en/policies/privacy/");
printf("%s\n", url2);
}
int main (int argc, char **argv)
{
const char *url2 = parseURI("http://www.google.com/", "/intl/en/policies/privacy/");
printf("%s\n", url2);
mysql_start();
crawler_init();
mysql_stop();
}
I am trying to use MYSQL_PLUGIN_DIR with mysql_options(). And on doing so my application crashes.
Here is simple code which crashes--
#include "stdafx.h"
#include <mysql.h>
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
#include <Windows.h>
#include<process.h>
MYSQL *conn; // the connection
MYSQL_RES *res; // the results
MYSQL_ROW row;
struct connection_details
{
char *server;
char *user;
char *password;
char *database;
};
MYSQL* mysql_connection_setup(struct connection_details mysql_details)
{
// first of all create a mysql instance and initialize the variables within
MYSQL *connection = mysql_init(NULL);
// connect to the database with the details attached.
if (!mysql_real_connect(connection,mysql_details.server, mysql_details.user, mysql_details.password, NULL, 0, NULL, 0)) {
printf("Conection error : %s\n", mysql_error(connection));
exit(1);
}
return connection;
}
MYSQL_RES* mysql_perform_query(MYSQL *connection, char *sql_query)
{
// send the query to the database
if (mysql_query(connection, sql_query))
{
printf("MySQL query error : %s\n", mysql_error(connection));
// exit(1);
}
return mysql_use_result(connection);
}
void mythread(void)
{
mysql_thread_init();
// assign the results return to the MYSQL_RES pointer
res = mysql_perform_query(conn,"select 2");
while ((row = mysql_fetch_row(res)) !=NULL)
printf("%s\n", row[0]);
/* clean up the database result set */
mysql_free_result(res);
/* clean up the database link */
}
void mythreadconnect(void)
{
struct connection_details mysqlD;
mysqlD.server = "localhost"; // where the mysql database is
mysqlD.user = "root"; // the root user of mysql
mysqlD.password = "root"; // the password of the root user in mysql
mysqlD.database = "myfirst"; // the databse to pick
//connect to mysql
conn = mysql_connection_setup(mysqlD);
}
int main()
{
char path[500]="C:\\Users\\abhishek\\Documents\\Visual Studio 2010\\Projects\\sampleapplication\\Debug\\";
mysql_library_init(0, NULL, NULL);
mysql_init(conn);
mysql_options(conn,MYSQL_PLUGIN_DIR ,path);
mythreadconnect();
mythread ();
mysql_library_end();
printf("Other business in Main\n");
printf("Main is exiting\n");
getch();
return 0;
}
It crashes at mysql_options(conn,MYSQL_PLUGIN_DIR ,path);. I have searched a lot but can't find solution. Please help what is wrong in this code. Thanks in advance.
Your mistake is with mysql_init. Use this conn = mysql_init( NULL );. In your case you have not allocated memory for conn and try to initialize. Read docs for mysql_init param and return value.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mysql.h>
#include <my_global.h>
void replace(char * o_string, char * s_string, char * r_string) {
char buffer[1024];
char * ch;
if(!(ch = strstr(o_string, s_string)))
return;
strncpy(buffer, o_string, ch-o_string);
buffer[ch-o_string] = 0;
sprintf(buffer+(ch - o_string), "%s%s", r_string, ch + strlen(s_string));
o_string[0] = 0;
strcpy(o_string, buffer);
return replace(o_string, s_string, r_string);
}
int main()
{
MYSQL *pConn;
MYSQL_RES *pRes;
MYSQL_ROW aRow;
MYSQL_FIELD *field;
int nfields, iCounter;
pConn = mysql_init(NULL);
char aPassword[1024]="";
if (pConn == NULL){
printf("Error %u: %s\n", mysql_errno(pConn), mysql_error(pConn));
exit(1);
}
if(mysql_real_connect(pConn, 0, "root",aPassword,"data",0,NULL,0) == NULL){
printf("Error %u: %s\n", mysql_errno(pConn), mysql_error(pConn));
exit(1);
}
char *info;
printf("Content-type:text/html\n\n");
printf("<html><body>");
info = getenv("QUERY_STRING");
char sub[20]="";
int nsub;
char teacher[20]="";
char room[20]="";
int nroom;
int count=0;
char *token;
char arr[5][20];
char data[1024];
char aCommand[1024];
char data2[1024];
replace(info,"%20"," ");
replace(info,"name=","");
token = strtok(info, " ");
while(token!=NULL){
strcpy(arr[count],token);
count++;
token = strtok(NULL," ");
}
if(count==5){
strcat(arr[0]," ");
strcat(arr[0],arr[1]);
strcpy(sub,arr[0]);
strcpy(teacher,arr[2]);
strcat(arr[3]," ");
strcat(arr[3],arr[4]);
strcpy(room,arr[3]);
}
if(count==4){
strcat(arr[0]," ");
strcat(arr[0],arr[1]);
strcpy(sub,arr[0]);
strcpy(teacher,arr[2]);
strcpy(room,arr[3]);
}
strcat(teacher," ");
strcat(teacher,room);
strcat(sub," ");
strcat(sub,teacher);
puts(sub);
sprintf(aCommand,"select * from Schedule");
mysql_query(pConn, aCommand);
pRes = mysql_store_result(pConn);
nfields = mysql_num_fields(pRes);
while ((aRow = mysql_fetch_row(pRes))){
if(strncmp(aRow[2],sub,strlen(sub)-1)==0)
puts("YS");
else
puts("NO");
}
mysql_free_result(pRes);
mysql_close(pConn);
printf("</body></html>");
return 0;
}
That's the whole code. I don't know where the problem is. After I parsed the query string and display them, it seems that the field and the parsed string are equal. But I've been trying to use strcmp and they just won't work. What could be the error?
I found the problem. I tried using strstr to find the string that only matches with the data in the MySQL table. It doesn't match the whole string so I just modified the program. Instead of matching the whole string, I just used a part of the string to match with the data in the table.
I called mysql_select_db() and it returned no errors, but when I called mysql_query() I got the following error:
No database selected.
I found a parameter of MYSQL struct. It is some char* calls db. What does it represent?
mysql_select_db() does changes this parameter?
Thanks.
Edit:
My main():
#include t-mysql.h
...
T_MYSQL *db = (T_MYSQL*) malloc(sizeof(T_MYSQL));
strcpy(db->database_name,mysql_database_name);
strcpy(db->password,mysql_password);
strcpy(db->root_password,mysql_root_password);
strcpy(db->server,mysql_server);
strcpy(db->table_name,mysql_table_name);
strcpy(db->user,mysql_user);
strcpy(db->query_create_db,query_create_db);
strcpy(db->query_create_table,query_create_table);
t_mysql_init(db);
t_mysql_connect(db);
t_mysql_createdb(db);
t_mysql_setdb(db);
t_mysql_createtable(db);
...
t-mysql.h:
#include <stdio.h>
#include <mysql.h>
#include <my_global.h>
#ifndef T_MYSQL_H_
#define T_MYSQL_H_
#define MAX_NAME_LENGTH 128
#define MAX_QUERY_LENGTH 1024
typedef struct {
MYSQL *con;
char password[MAX_NAME_LENGTH];
char root_password[MAX_NAME_LENGTH];
char user[MAX_NAME_LENGTH];
char server[MAX_NAME_LENGTH];
char database_name[MAX_NAME_LENGTH];
char table_name[MAX_NAME_LENGTH];
char query_create_table[MAX_QUERY_LENGTH];
char query_create_db[MAX_QUERY_LENGTH];
char query_data[MAX_QUERY_LENGTH];
}T_MYSQL;
void finish_with_error(MYSQL *con);
int t_mysql_init(T_MYSQL *mysql);
int t_mysql_connect(T_MYSQL *mysql);
int t_mysql_disconnect(T_MYSQL *mysql);
int t_mysql_createdb(T_MYSQL *mysql);
int t_mysql_createtable(T_MYSQL *mysql);
int t_mysql_setdb(T_MYSQL *mysql);
int t_mysql_insertdata(T_MYSQL *mysql);
int t_mysql_quickinsert(T_MYSQL *mysql);
#endif /* T_MYSQL_H_ */
t-mysql.c:
#include <stdio.h>
#include <mysql.h>
#include <my_global.h>
#include "t-mysql.h"
void finish_with_error(MYSQL *con){
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con); //close connection to database
}
int t_mysql_init(T_MYSQL *mysql){
mysql->con = mysql_init(NULL);
if (mysql->con == NULL){
finish_with_error(mysql->con);
return 0;
}
return 1;
}
int t_mysql_connect(T_MYSQL *mysql){
// connect to MySQL
if (mysql_real_connect(mysql->con, mysql->server, mysql->user, mysql->password,NULL, 0, NULL, 0) == NULL){
finish_with_error(mysql->con);
return 0;
}
return 1;
}
int t_mysql_disconnect(T_MYSQL *mysql){
mysql_close(mysql->con);
if ( mysql->con == NULL){
finish_with_error(mysql->con);
return 0;
}
return 1;
}
int t_mysql_createdb(T_MYSQL *mysql){
t_mysql_disconnect(mysql); //disconnect normal user
//connect as root
t_mysql_init(mysql);
if (mysql_real_connect(mysql->con, mysql->server, "root", mysql->root_password,NULL, 0, NULL, 0) == NULL){
finish_with_error(mysql->con);
return 0;
}
// create DB, if it doesn't exist
if (mysql_select_db(mysql->con, mysql->database_name)){
if (mysql_query(mysql->con, mysql->query_create_db)){
finish_with_error(mysql->con);
return 0;
}
else {
fprintf(stdout,"Database %s created\n",mysql->database_name);
}
}else{
fprintf(stdout,"Database %s exists\n",mysql->database_name);
}
//disconnect root user
t_mysql_disconnect(mysql);
//connect as normal user
t_mysql_init(mysql);
t_mysql_connect(mysql);
return 1;
}
int t_mysql_setdb(T_MYSQL *mysql){
if (mysql_select_db(mysql->con, mysql->database_name) == NULL){
finish_with_error(mysql->con);
return 0;
}
return 1;
}
int t_mysql_createtable(T_MYSQL *mysql){
//create table:
if(mysql_query(mysql->con, mysql->query_create_table)){
finish_with_error(mysql->con);
return 0;
}
return 1;
}
int t_mysql_insertdata(T_MYSQL *mysql){
if (mysql_query(mysql->con, mysql->query_data)) {
finish_with_error(mysql->con);
return 0;
}
return 1;
}
I didn't test your code on my system, but I think, the error is here:
int t_mysql_setdb(T_MYSQL *mysql){
if (mysql_select_db(mysql->con, mysql->database_name) == NULL){//<-?
finish_with_error(mysql->con);
return 0;
}
return 1;
}
You are thinking, that 0 is the returned, when there is an error, while connecting. While MySQL docs say, that the opposite is true:
int mysql_select_db(MYSQL *mysql, const char *db)
Return Values:
Zero for success. Nonzero if an error occurred.
http://dev.mysql.com/doc/refman/5.5/en/mysql-select-db.html
So, you are checking for a wrong value. It means, that this statement:
I called mysql_select_db() and it returned no errors
Is false, and there is an error in this function, that you don't catch.
I wrote a program in c language with mysql.
It does not work well.
I create a connect function to help me connect mysql
MYSQL *ts_mysql_connect(char *host, char *user, char *pass, char *database)
{
MYSQL *conn = mysql_init(NULL);
fprintf(stderr, "[ts_mysql_connect] conn-> %ld\n", conn);
if (!mysql_real_connect(conn, host, user, pass, database, 0, NULL, 0)){
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
//set auto commit to false
if (mysql_autocommit(conn, 0))
fprintf(stderr, "%s\n", "SET MYSQL autocommit to off faild!");
fprintf(stderr, "[ts_mysql_connect] conn-> %ld\n", conn);
return conn;
}
and I use below the code below to call my function
MYSQL *test;
test = ts_mysql_connect(conf->run_conf->mysql_host,
conf->run_conf->mysql_user,
conf->run_conf->mysql_pass,
conf->run_conf->mysql_database);
conf->mysql_start = time(NULL);
if (verbose)
fprintf(stderr, "[ts_mysql_insert] mysql conn init at %ld\n",
&test);
exit(1);
i donot know why ts_mysql_connect return different address of the conn.
run log:
[ts_mysql_connect] conn-> 140065527302336
[ts_mysql_connect] conn-> 140065527302336
[ts_mysql_insert] mysql conn init at -1946154816
So why MYSQL *conn in [ts_mysql_insert] and [ts_mysql_connect] have different address
You didn't declare the function prototype of ts_mysql_connect in the second program, so the compiler assumes that the function returns an int type.
If you use -Wall directive to enable all warnings, you will see a warning message like this:
warning: implicit declaration of function ‘ts_mysql_connect’
#include <stdio.h>
#include <stdlib.h>
#include <my_global.h>
#include <mysql.h>
MYSQL *DatabaseConnection;
const char *ServerName = "192.168.1.12";
const char *User = "dbuser";
const char *Password = "dbpassword";
const char *Name = "test";
const char *DB_Socket_Path = "/var/run/mysqld/mysql.sock";
int execute_db_query(const char *sql_query)
{
MYSQL_RES *result = NULL;
MYSQL_ROW row;
int num_fields;
int i;
if (!DatabaseConnection)
return -1;
mysql_query(DatabaseConnection, sql_query);
result = mysql_store_result(DatabaseConnection);
if(result == NULL)
{
mysql_free_result(result);
return -1;
}
if (!result->eof) {
mysql_free_result(result);
return -1;
}
num_fields = mysql_num_fields(result);
int f_count = result->field_count;
int index =1 ;
while ((row = mysql_fetch_row(result))) {
for (i = 0; i < num_fields; i++) {
printf("%s ", row[i] ? row[i] : "NULL");
}
printf("\n");
index++;
}
mysql_free_result(result);
return 0;
}
void close_db_connection() {
mysql_close(DatabaseConnection);
mysql_library_end();
}
int init_connect() {
DatabaseConnection = mysql_init(NULL);
printf("MySQL client version: %s\n", mysql_get_client_info());
if (!mysql_real_connect(DatabaseConnection, ServerName, User, Password,
Name, 0, DB_Socket_Path, 0)) {
puts(mysql_error(DatabaseConnection));
close_db_connection();
return -1;
}
printf("Host : %s \n", mysql_get_host_info(DatabaseConnection));
printf("Server : %s: \n", mysql_get_server_info(DatabaseConnection));
printf("Protocol : %d\n", mysql_get_proto_info(DatabaseConnection));
return 0;
}
int main(void) {
puts("SQL Example");
init_connect() ;
execute_db_query("select * from test");
return EXIT_SUCCESS;
}
I see you are using socket less connection so you can pass NULL in this case.