We usually use files to save data using C language (I at least). Is it possible to connect a relational database to some application made in C ? If so, do you need any library for this and how is that connection made? Preferably MySQL.
We usually use files to save data using C language (I at least). Is it possible to connect a relational database to some application made in C ? If so, do you need any library for this and how is that connection made? Preferably MySQL.
Why would not I?
Programming languages are just mechanisms for communicating with the computer. What makes communication with databases are codes usually organized in libraries. Each database provides its way of doing this. Many databases are written in C so it's pretty straightforward. In other languages that usually have some layer of adaptation.
Example in MySQL:
#include <my_global.h>
#include <mysql.h>
int main() {
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);
}
Download .
Yes, Postgresql itself uses many functions in C and has a library for it. Just add #include
You will need to find out where the libpq-fe.h file is, then, when compiling the program, we pass the above directory into the gcc -I option: gcc -o program -I / usr / include / pgsql program.c -lpq
Assuming your library is in / usr / include / pgsql. Change to the directory where your is. You may need to use the -L option and the directory, rather than the -I option.
Further details on: source live linux