Connecting C to mysql - mysql

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.

Related

mysql 6.1.11 client to Mysql 8 connection

I have a mysql server 8.0.21 running on ubuntu 20.04, on this later i also have a VB win7 32bi guest.
I'm trying to connect to the Mysql server from guest using c api (specifically 6.1.11 connector as newer MySQL 8 installations no more support win7)
I'm running the following basic program:
int main(){
MYSQL *conn;
conn = mysql_init (NULL);
if(mysql_real_connect ( conn, "169.254.128.100", "myuser","mypass", "my_schema", 3306, NULL,0 )==NULL)
{
fprintf(stderr, "%s\n", mysql_error(conn));
}
mysql_close ( conn );
return 0;
}
I get the error: "SSL Connection Error: Unknown error number"
The thing is i can connect fine using the ODBC DSN
I also checked the server status & it requires no ssl ("not in use")
thanks
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
static char *host = "localhost";
static char *user = "user";
static char *pass = "password";
static char *db = "mysql";
static char *socket = NULL;
unsigned int port = 3306;
unsigned int flags = 0;
/*
* brew install mysql-client
* export PATH=/usr/local/opt/mysql-client/bin:$PATH
* brew install mysql-connector-c++ (optional)
* gcc -o main $(mysql_config --cflags) main.c $(mysql_config --libs) -L/usr/local/opt/openssl/lib
*
* -L/usr/local/opt/openssl/lib is required if you get ld: library not found for -lssl error
*/
int main(int argc, char *argv[]) {
MYSQL *con = mysql_init(NULL);
if (!mysql_real_connect(con, host, user, pass, db, port, socket, flags)) {
fprintf(stderr, "Error %s (%d)", mysql_error(con), mysql_errno(con));
exit(1);
}
printf("Connected\n");
return EXIT_SUCCESS;
}
You need to do this:
int sslmode = 1;
mysql_options(&p->mysqlConn, MYSQL_OPT_SSL_MODE, &sslmode);

load mysql dump in database using mysql C API

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.

Using mysql in c programming

I installed ubuntu on a virtual machine. There, I installed mysql server sudo apt-get install mysql-server .This worked, because I could acces mysql-u root -p password
After that, I did : sudo apt-get install libmysqlclient-dev
#include <my_global.h>
#include <mysql.h>
int main(int argc, char **argv)
{
printf("MySQL client version: %s\n", mysql_get_client_info());
exit(0);
}
When I compile this with
gcc version.c -o version `mysql_config --cflags --libs`
it works.
But when I compile this one from below
gcc createdb.c -o createdb -std=c99 `mysql_config --cflags --libs`
I get some errors.
#include <my_global.h>
#include <mysql.h>
int main(int argc, char **argv)
{
MYSQL *con = mysql_init(NULL);
if (con == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}
if (mysql_real_connect(con, "localhost", "root", "root_pswd",
NULL, 0, NULL, 0) == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
if (mysql_query(con, "CREATE DATABASE testdb"))
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
mysql_close(con);
exit(0);
}
Errors:
"Usage:: No such file or directory
[OPTIONS]: No such file or directory
Options:: No such file or directory
[-I/usr/include/mysql: No such file or directory
[-L/user/lib/x86_64-linux-gnu: No such file or directory
.
.
.
unrecognized command line option '--cflags'
unrecognized command line option '--libs'
.
.
unrecognized command line option '--socket'
unrecognized command line option '--port' "
Can someone explain me what I did wrong,and how to fix it?
I just want to get some data from tables in a C program.
I suspect you actually ran
gcc createdb.c -o createdb -std=c99 `mysql_config` --cflags --libs
rather than
gcc createdb.c -o createdb -std=c99 `mysql_config --cflags --libs`
That’s not going to work; mysql_config, if it doesn’t get any arguments, is going to print out a bunch of usage instructions, which will be passed to gcc, and then you’ll follow that with --cflags --libs, which gcc also doesn’t understand. gcc is severely confused and complains.
If you make sure those arguments get to mysql_config rather than gcc, everyone will be happy.

MySql from C on Ubuntu

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

Dual connection to sql server in c code

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.