I'm working on a project in C with MySQL
I'm having a problem connection to the MySQL server. First have one time a connection, this works.
But now in an other section an also need to retrieve info from the db
There I use the same code to connect.
conn = mysql_init(NULL);
//check if there is a connection
if (conn == NULL)
{
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
if (mysql_real_connect(conn, "localhost", "test", "test", "test", 0, NULL, 0) == NULL)
{
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
}
But when I use this a second time, I get an error
Unhandled exception at 0x009818c9 in
simple.exe: 0xC0000005: Access
violation reading location 0x00000000.
it crashes on the if(conn == NULL)
Anyone got an idea why this don't work?
That seems to be windows's version of a segmentation fault (I've never compiled anything on windows), so, check your pointers and make sure they aren't null, and if they are to be global, make sure they are on the heap.
Related
I want to manage a database in C language. So I am using C API for mysql.
I am unable to find a function in C API for loading the a .sql file directly to mysql.
I have gone through documentation but didn't find it.
I also tried using SOURCE file.sql by giving it as a query but I am getting a weird error
source error.You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SOURCE /home/iota/Academics/4-2/NP/a/a4/cars.sql' at line 1
Related code is below:
void connectdb(){
con = mysql_init(NULL);
if (mysql_real_connect(con, "localhost", "root", "psswd", NULL, 0, NULL, 0) == NULL){
fprintf(stderr, "qqddd%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
mysql_query(con, "drop database if exists testdb");
mysql_query(con, "create database testdb");
if(mysql_query(con, "use testdb")){
fprintf(stderr, "qq%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
if(mysql_query(con, "SOURCE /home/iota/Academics/4-2/NP/a/a4/cars.sql")){ /*error here*/
fprintf(stderr, "source error.%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
}
Could you please suggest any way to do this ?
Thank you.
I am connecting C to mysql and then creating a database .
My code is:
#include <mysql.h>
#include <my_global.h>
int main(int argc, char **argv)
{MYSQL *conn;
conn = mysql_init(NULL);
if (conn == NULL) {
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);}
if (mysql_real_connect(conn, "localhost", "zetcode", "passwd", NULL, 0, NULL, 0) == NULL)
{printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);}
if (mysql_query(conn, "create database testdb")) {
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
mysql_close(conn);}
But i don't have the headers mysql.h and my_global.h
How can i get them??Thanks
In ubuntu/debian
$sudo apt-get install libmysqlclient libmysqlclient-dev
in centos/fedora/RHEL
$yum install mysql-devel
Once you have installed the Client....you should be able to just link them? Or at least add the include/libs to your compiler?
http://www.mysql.com/downloads/
You have to install the library:
http://dev.mysql.com/downloads/connector/c/
Insufficient. You need all the files for development. You need the header and proper libraries or you won't get anywhere. So if you are one some linux check out something like libmysql or search on the proposed download pages.
I can use mysql from terminal to query data and I try to connect to mysql server with C but here is some problem i don't know to fix but seems like version conflict.
#include <my_global.h>
#include <mysql.h>
int main(int argc, char **argv)
{
MYSQL *conn;
conn = mysql_init(NULL);
if (conn == NULL) {
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
// upper code works
// following don't work...
if (mysql_real_connect(conn, "localhost", "username", "password", NULL, 0, NULL, 0) == NULL)
{
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
etc...
When I start this code message "Error 2002: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)" appears.
Problem is that I don't have mysql.sock anywhere in computer but have /var/run/mysqld/mysqld.sock.
In linker options I added /var/lib/libmysql.so and program compiles OK.
Is here any simple way to get this to work?
why not connecting directly to the socket?
mysql_real_connect(conn, "localhost", "username", "password",
NULL, 0, "/var/run/mysqld/mysqld.sock", 0)
I have no environment to test it right now, but I think it should work
To answer question in your comment:
I think its' because of your configuration, mysql's default configuration is to store socket at /tmp/mysql.sock, and it didn't find it there, you just specified your location of the socket, this is not a portable solution, it depends on purpose of your application
I am trying to connect to the server with a wrong user or password and as expected it fails but the mysql_errno always returns 0. is there something wrong with my mysql installation?.
EDIT: I am using a c program to connect.
int main(int argc, char *argv[]) {
MYSQL my_connection;
mysql_init(&my_connection);
if (mysql_real_connect(
&my_connection,
"localhost",
"rick",
"I do not know",
"foo", 0, NULL, 0)) {
printf("Connection success\n");
mysql_close(&my_connection);
} else {
fprintf(stderr, "Connection failed\n");
if (mysql_errno(&my_connection)) {
fprintf(stderr, "Connection error %d: %s\n",
mysql_errno(&my_connection), mysql_error(&my_connection));
}
}
return EXIT_SUCCESS;
}
IN the book the output is supposed to say error 1045:
In my case it is 0 and nothing but connection failed is printed.
mysql_errno() only reports errors from a valid connection.
The error number comes from the actual database. How can it retrieve these without a connection in the first place?
You'll notice in the API example, they don't bother with the error number for a failed connection
MYSQL mysql;
mysql_init(&mysql);
mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP,"your_prog_name");
if (!mysql_real_connect(&mysql,"host","user","passwd","database",0,NULL,0))
{
fprintf(stderr, "Failed to connect to database: Error: %s\n",
mysql_error(&mysql));
}
My C client program connects with MySQL server. I can connect, but the problem arises when I try to implement error detection codes. If I do the following (here I have intentionally put incorrect user name):
#include "mysql.h"
int main()
{
MYSQL* conn_ptr_db;
conn_ptr_db = mysql_init(NULL);
if(!conn_ptr_db)
{
perror("Error connecting to MySQL! ");
exit(0);
}
if(mysql_real_connect(conn_ptr_db,"localhost","rooti","root","mysql",0,NULL,0))
{
printf("Hurrah! we have connected to MySQL! ");
mysql_close(conn_ptr_db);
}
else
{
printf("Connection failed to MySQL! \n");
printf("Error code: %d %s %s\n",mysql_errno(conn_ptr_db),mysql_sqlstate(conn_ptr_db),mysql_error(conn_ptr_db));
}
//mysql_close(conn_ptr_db);
return 0;
}
Now the output to this program:
./db_access
Connection failed to MySQL!
Error code: 1045 28000 Access denied for user 'rooti'#'localhost' (using password: YES)
This is the correct output.
But if I do the following:
#include "mysql.h"
int main()
{
MYSQL* conn_ptr_db;
conn_ptr_db = mysql_init(NULL);
if(!conn_ptr_db)
{
perror("Error connecting to MySQL! ");
exit(0);
}
//CHANGES -------------
conn_ptr_db = mysql_real_connect(conn_ptr_db,"localhost","rooti","root","mysql",0,NULL,0);
if(conn_ptr_db)
{
printf("Hurrah! we have connected to MySQL! ");
mysql_close(conn_ptr_db);
}
else
{
printf("Connection failed to MySQL! \n");
printf("Error code: %d %s %s\n",mysql_errno(conn_ptr_db),mysql_sqlstate(conn_ptr_db),mysql_error(conn_ptr_db));
}
//mysql_close(conn_ptr_db);
return 0;
}
I get the following output:
./db_access
Connection failed to MySQL!
Error code: 0 08001
Here, the mysql_errno() and mysql_error() are not working. Why?
After the connection has failed conn_ptr_db is set to NULL. You then try and use this value as a paramter to a call to msql_errono(), in effect msql_errno(NULL). Hence the error code your getting.
Because in the second example you assign the result to conn_ptr_db.
You should not have done so.
From the documentation:
A MYSQL* connection handle if the
connection was successful, NULL if the
connection was unsuccessful. For a
successful connection, the return
value is the same as the value of the
first parameter.
In the case of an unsuccessful connection (which you have), you've replaced your conn_ptr_db object with NULL then attempt to do stuff with it (mysql_error).
The documentation for mysql_error doesn't explicitly say that a NULL parameter is disallowed, but it's clear that you won't get the error relating to the original, actual connection context that you overwrote.