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.
Related
I have a simple program that should print today's day name. It was working, but I made the get_query_result function and after that, it is giving me a segmentation fault.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql.h>
#define QUERY_SIZE 1000
void finish_with_error(MYSQL* con)
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
void get_query_result(MYSQL *con, MYSQL_RES *result, char *query)
{
if(mysql_query(con, query))
{
finish_with_error(con);
}
result = mysql_store_result(con);
if(result == NULL)
{
finish_with_error(con);
}
}
int main()
{
MYSQL *con = mysql_init(NULL);
MYSQL_RES *result;
MYSQL_ROW row;
if(con == NULL)
{
fprintf(stderr, "mysql_init() failed\n");
exit(1);
}
if(mysql_real_connect(con, "127.0.0.1", "root", "mysqlpassword", "database", 0, NULL, 0) == NULL)
{
finish_with_error(con);
}
get_query_result(con, result, "SELECT DAYNAME(DATE(NOW()))");
row = mysql_fetch_row(result);
printf("%s\n", row[0]);
mysql_free_result(result);
return 0;
}
result in main is not affected by get_query_result, in consequence, you end up calling mysql_free_result with an uninitialized pointer.
Switch to
MYSQL_RES *get_query_result(MYSQL *con, char *query)
{
MYSQL_RES *result;
if(mysql_query(con, query))
{
finish_with_error(con);
}
result = mysql_store_result(con);
if(result == NULL)
{
finish_with_error(con);
}
return result;
}
and call it like:
result = get_query_result(con, "SELECT DAYNAME(DATE(NOW()))");
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 get too many connections when I try to connect to MySQL.
Why did I get that error although I close the connection whenever I connect to the MySQL server?
How should I solve that problem?
UPDATE
I run this on Ubuntu. My project is CGI which is written with C.
My web server is Apache.
This is an example of my source Code. Others are almost the same.
#include <mysql.h>
#include <stdio.h>
#include <string.h>
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "jitcomm"; /* set me first */
char *database = "profile";
int main(int argc, char *argv[])
{
if(argc==1)
getAll();
else
getOneUser(argv[1]);
return 0;
}
// show user
void getAll()
{
char query[500];
char result[1024];
memset(result,0,1024);
memset(query,0,500);
conn = mysql_init(NULL);
int i;
FILE *fout;
if((fout = fopen("gUsers","w"))==NULL)
{
printf("error with file");
}
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
/* send SQL query */
sprintf(query,"select * from userTbl");
if (mysql_query(conn, query))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(2);
}
res = mysql_use_result(conn);
/* output table name */
system("clear");
sprintf(result,"ID\t Name\t Password\t Role\n");
while ((row = mysql_fetch_row(res)) != NULL)
{
//printf("%s \n", row[0]);
//strcpy(id,row[0]);
sprintf(query,"%s\t %s\t %s\t\t %s\n",row[0], row[1], row[2], row[3]);
strcat(result,query);
for(i = 1 ;i<4;i++)
{
if(i==1)
fprintf(fout,"%s\n",row[i]); //write data to the file
if(i==2)
fprintf(fout,"%s\n",row[i]); //write data to the file
if(i==3)
fprintf(fout,"%s\n",row[i]); //write data to the file
}
}
/* close connection */
mysql_free_result(res);
mysql_close(conn);
//printf(result);
}
void getOneUser(char *n)
{
char query[500];
char result[1024];
memset(result,0,1024);
memset(query,0,500);
conn = mysql_init(NULL);
int i;
FILE *fout;
if((fout = fopen("gUsers","w"))==NULL)
{
printf("error with file");
}
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
/* send SQL query */
sprintf(query,"select * from userTbl where name = '%s'",n);
if (mysql_query(conn, query))
{
fprintf(stderr, "%s\n", mysql_error(conn));
exit(2);
}
res = mysql_use_result(conn);
/* output table name */
system("clear");
sprintf(result,"ID\t Name\t Password\t Role\n");
while ((row = mysql_fetch_row(res)) != NULL)
{
//printf("%s \n", row[0]);
// //strcpy(id,row[0]);
sprintf(query,"%s\t %s\t %s\t\t %s\n",row[0], row[1], row[2], row[3]);
strcat(result,query);
for(i = 1 ;i<4;i++)
{
if(i==1)
fprintf(fout,"%s\n",row[i]); //write data to the file
if(i==2)
fprintf(fout,"%s\n",row[i]); //write data to the file
if(i==3)
fprintf(fout,"%s\n",row[i]); //write data to the file
}
}
/* close connection */
mysql_free_result(res);
mysql_close(conn);
//printf(result);
}