Strcmp not with working data in mysql - mysql

#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.

Related

Why would MySQL break wget_iri_relative_to_abs?

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();
}

Problem with mysql_query and inserting variable in it via sprintf()

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;
}

How to reverse a structure in text file with C?

I have an exercise and I don't know how to solve it well!
I want write a C program that give from user the information of a student and then save it to file A.txt. After that reverse the first name, last name and student number and save it to file B.txt.
For example:
john
lopez
123456
It changes to:
nhoj
zepol
654321
#include <stdio.h>
#include <stdlib.h>
#define SIZE 50
struct student {
char fname[SIZE];
char lname[SIZE];
char num[SIZE];
}st;
int main()
{
FILE *in, *out;
char ch;
int tmp=0,flag=0,i;
printf("INPUT First name: ");
scanf("%s", &st.fname);
printf("INPUT Last name: ");
scanf("%s", &st.lname);
printf("INPUT personal num: ");
scanf("%s", &st.num);
in= fopen("A.txt", "w");
fputs(st.fname, in);
fputs(st.lname, in);
fputs(st.num, in);
fclose(in);
in= fopen("A.txt", "r");
out=fopen("B.txt", "w");
fgets(st.fname, strlen(st.fname)+1,in);
strrev(st.fname);
fputs(st.fname, out);
printf("\n%s", st.fname);
fgets(st.lname, strlen(st.lname)+1, in);
strrev(st.lname);
fputs(st.lname, out);
printf("\n%s", st.lname);
fgets(st.num, strlen(st.num)+1, in);
strrev(&st.num);
fputs(st.num, out);
printf("\n%d", st.num);
fclose(in);
fclose(out);
return 0;
}
If you want to copy the data to a file, then you first need to put the data in some dynamic memory allocation and then after reversing the content required in dynamic memory, you need to copy it into your file.
this is the program to reverse the each word in given string
#include
#include
void reverse_string(char*);
void reverse_words(char*);
int main() {
char a[100];
gets(a);
reverse_words(a);
printf("%s\n", a);
return 0;
}
void reverse_words(char *s) {
char b[100], *t, *z;
int c = 0;
t = s;
while(*t) { //processing complete string
while(*t != ' ' && *t != '\0') { //extracting word from string
b[c] = *t;
t++;
c++;
}
b[c] = '\0';
c = 0;
reverse_string(b); // reverse the extracted word
z = b;
while (*z) { //copying the reversed word into original string
*s = *z;
z++;
s++;
}
while (*s == ' ') { // skipping space(s)
s++;
}
/*
* You may use if statement in place of while loop if
* you are assuming only one space between words. If condition is
* used because null terminator can also occur after a word, in
* that case we don't want to increment pointer.
* if (*s == ' ') {
* s++;
* }
*/
t = s; // pointing to next word
}
}
/*
* Function to reverse a word.
*/
void reverse_string(char *t) {
int l, c;
char *e, s;
l = strlen(t);
e = t + l - 1;
for (c = 0; c < l/2; c++) {
s = *t;
*t = *e;
*e = s;
t++;
e--;
}
}
Then you can try using fputc, i.e., reading char by char along with a loop to get your data line by line as 3 lines.
char *ch;
for(i=0;st.fname[i];i++)
{
ch=getc(st.fname[i]);
fputc(ch,in);
}
repeat the same even for st.lname and st.num

How can I verify if database is set

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.

mysql with c program

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.