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.
Related
I am trying to connect to mariadb database using c program. Initially it was showing error for #include <mysql.h> as no such file or directory.
But after including directory name, that problem is solved now, but it is showing another error.
Following is the code I was trying to run:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// #include "C:/Program Files/MariaDB 10.11/include/mysql/my_global.h"
#include "mysql/mysql.h"
int main (int argc, char* argv[])
{
// Initialize Connection
MYSQL *conn;
if (!(conn = mysql_init(0)))
{
fprintf(stderr, "unable to initialize connection struct\n");
exit(1);
}
// Connect to the database
if (!mysql_real_connect(
conn, // Connection
"mariadb.example.net", // Host
"db_user", // User account
"db_user_password", // User password
"test", // Default database
3306, // Port number
NULL, // Path to socket file
0 // Additional options
));
{
// Report the failed-connection error & close the handle
fprintf(stderr, "Error connecting to Server: %s\n", mysql_error(conn));
mysql_close(conn);
exit(1);
}
// Use the Connection
// ...
// Close the Connection
mysql_close(conn);
return 0;
}
I am getting following error in output:
PS C:\Dev\Win> gcc Db_con.c -o Db_con
C:\Users\hajos\AppData\Local\Temp\ccGZ2Rhz.o:Db_con.c:(.text+0x1e): undefined reference to `mysql_init#4'
C:\Users\hajos\AppData\Local\Temp\ccGZ2Rhz.o:Db_con.c:(.text+0xa1): undefined reference to `mysql_real_connect#32'
C:\Users\hajos\AppData\Local\Temp\ccGZ2Rhz.o:Db_con.c:(.text+0xaf): undefined reference to `mysql_error#4'
C:\Users\hajos\AppData\Local\Temp\ccGZ2Rhz.o:Db_con.c:(.text+0xd9): undefined reference to `mysql_close#4'
collect2.exe: error: ld returned 1 exit status
Can anyone explain what is the problem and how to solve it?
You have to link against the MariaDB Connector/C libraries.
From MariaDB Connector/C documentation:
Linking your application against MariaDB Connector/C
Windows
For static linking the library libmariadb.lib is required, for dynamic linking use libmariadb.dll. Using the MSI installer, these libraries can be found in the lib directory of your MariaDB Connector/C installation.
Unless you use the experimental plugin remote_io (which requires the curl library) there are no dependencies to other libraries than the Windows system libraries.
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));
}
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.
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.