I have tried to connect to mysql server from C code.
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
main(){
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "12345"; /* set me first */
char *database = "mydatabase";
conn = mysql_init(NULL);
printf("done 1\n");
if (!mysql_real_connect(conn, server,
user, password, database, 3306, "/tmp/mysql.sock", 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
printf("done 2\n");
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
printf("done 3\n");
res = mysql_use_result(conn);
printf("done 4\n");
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
printf("done 5\n");
mysql_free_result(res);
mysql_close(conn);
}
But I have this weird problem:
done 1
Plugin caching_sha2_password could not be loaded: /usr//usr/lib/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory
I do not know why it tries to load lib from /usr//usr/
Please help if you have a deal with it already!
Related
I have a short program written to try to connect to a MySQL database.
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
int main () {
MYSQL *conn;
char *server = "localhost";
char *user = "root";
char *password = "";
char *database = "database";
int port = 3306;
conn = mysql_init(NULL);
mysql_real_connect(conn, server,user,password,database, port, NULL, 0);
return 0;
}
It builds fine, but when I run it, the console reads
<terminated> (exit value: -1,073,741,515)
I don't think that is good, but I also have no idea what it means. Can anybody help me to decipher this?
This program worked for me against a fresh mysql 5.7 install with Ubuntu Linux.
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
int main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "<passwrd>"; /* set me first */
char *database = "mysql";
conn = mysql_init(NULL);
/* 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 */
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* output table name */
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
/* close connection */
mysql_free_result(res);
mysql_close(conn);
}
Test
./a.out
MySQL Tables in mysql database:
columns_priv
db
engine_cost
event
func
general_log
gtid_executed
help_category
help_keyword
help_relation
help_topic
innodb_index_stats
innodb_table_stats
ndb_binlog_index
plugin
proc
procs_priv
proxies_priv
server_cost
servers
slave_master_info
slave_relay_log_info
slave_worker_info
slow_log
tables_priv
time_zone
time_zone_leap_second
time_zone_name
time_zone_transition
time_zone_transition_type
user
Please check that your mysql is running and accepting connections and that the program terminates properly. It might be easier to use the command-line to link your mysql.h (I did it with gcc main.c -lmysqlclient when I compiled the above).
I want to integrate MySQL database into my C program.
Is there a possible way to do so ? What are the libraries that should be used ? (If it's possible)
The C API code is distributed with MySQL. It is included in the mysqlclient library and allows C programs to access a database.
Many of the clients in the MySQL source distribution are written in C. If you are looking for examples that demonstrate how to use the C API, take a look at these clients. You can find these in the clients directory in the MySQL source distribution.
Here is a small program that connects to a MySQL server and list all tables from the database:
#include <mysql.h>
#include <stdio.h>
int main(void) {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
/* Change me */
char *server = "localhost";
char *user = "root";
char *password = "PASSWORD";
char *database = "mysql";
conn = mysql_init(NULL);
/* 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 */
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* output table name */
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
/* close connection */
mysql_free_result(res);
mysql_close(conn);
return 0;
}
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.
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);
}
Hi
I'm trying to connect MySql using C in my project and it's the first time I use C and MySql.
I try to save a user name, password and role in MySql. When I run the program like that it's ok.
#include <mysql.h>
#include <stdio.h>
main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "admin"; /* set me first */
char *database = "profile";
char query[500];
memset(query,0,500);
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
int i =2;
/* send SQL query */
char userName[45] ="hi";
char userPassword[45];
char role[45];
strcpy(userName,"koko");
strcpy(userPassword,"hi");
strcpy(role,"admin");
sprintf(query,"insert into userTbl(name,password,role) values (\'%s\',\'%s\',\'%s\'); ",userName,userPassword,role);
if (mysql_query(conn, query)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(2);
}
/* close connection */
mysql_close(conn);
}
I can run it without any errors though there are some warnings.
But when i wrote this program by using function, I got this error when I compile it .
Access denied for user 'root'#'localhost' (using password: YES)
#include <mysql.h>
#include <stdio.h>
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "admin"; /* set me first */
char *database = "profile";
void saveUser(char * , char * ,char * );
void updateUser(char * , char * ,char * );
void showData();
main()
{
saveUser("kevin","hi","admin");
}
void saveUser(char *name, char *password,char *role)
{
char query[500];
memset(query,0,500);
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
int i =2;
/* send SQL query */
sprintf(query,"insert into userTbl(name,password,role) values (\'%s\',\'%s\',\'%s\'); ",name,password,role);
if (mysql_query(conn, query)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(2);
}
res = mysql_use_result(conn);
/* close connection */
mysql_free_result(res);
mysql_close(conn);
}
void updateUser(char *name, char *password,char *role)
{
char query[500];
memset(query,0,500);
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
printf("1234511\n");
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
int i =2;
/* send SQL query */
sprintf(query,"update userTbl set password=\'%s\',role=\'%s\' where name=\'%s\'; ",password,role,name);
if (mysql_query(conn, query)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(2);
}
/* close connection */
mysql_close(conn);
}
void showData()
{
/* output table name */
system("clear");
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
}
Can anybody explain me where I went wrong and how can I correct it.
I use gcc to get the executable file and run it on command prompt.
Thanks in advance.
Kevin
i read your post it's not the problem with your code try to connect to mysql server with the parameters(username and password) using a mysql client software like mysql query browser or sql yog or phpmyadmin whatever.
i think you set your password to blank, so simply replace the line
saveUser("kevin","hi","admin");
to
saveUser("kevin","","admin");
also ensure that you are passing the parameters in correct case since ubuntu's behavior is case senstive
give it a try if it helps.
please check for your correct username and passwords. usually when you install mysql server it asks for password and user to be set. as you are using ubuntu you might be using lamp server so try to get the correct credentials
there only the problem is with your credentials
Hi both Simon and Devjosh , Thanks for you help. I found the problem._The global variable name password is the same with the parameter name password in saveUser function _. Thanks a lot and so sorry disturb you .
Kevin