assiging makes pointer integer witout cast - mysql

hello all i have write a c program which connects to a mysql server and executes a sql query from a text file which has only one query.
#include <mysql.h>
#include <stdio.h>
main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "127.0.0.1";
char *user = "root";
char *password = "PASSWORD"; /* set me first */
char *database = "har";
conn = mysql_init(NULL);
char ch, file_name[25];
char *ch1;
FILE *fp;
printf("Enter the name of file you wish to see ");
gets(file_name);
fp = fopen(file_name,"r"); // read mode
if( fp == NULL )
{
perror("Error while opening the file.\n");
exit(0);
}
while( ( ch = fgetc(fp) ) != EOF )
printf("%c",ch);
ch1=ch;
/* Connect to database */
if (!mysql_real_connect(conn, server,
NULL , NULL, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(0);
}
printf("%c",ch);
/* send SQL query */
if (mysql_query(conn, ch1)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(0);
}
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);
fclose(fp);
}
i am unable to understand where i have gone wrong....
thanks in advance...

This is the line causing problem:
ch1=ch;
ch1 is a pointer to a character, whereas ch is a character.
Do you intend to store the bytes read from fp in a char array pointed by ch1? What you are doing is, every time in the while loop you are reading a character using fgetc storing it in ch and printing it.
Then, when while loop gets over, you are assigning a char to a char pointer. I am not sure what you are trying to do with this. But this definitely causes the problem.

You're going wrong in a lot of ways:
You don't declare the return type or arguments for main.
You're using gets. Never ever use gets, don't even think about. Use fgets instead.
fgetc returns an int, not a char so your ch should be an int. You won't be able to recognize EOF until you fix this.
You're declaring char ch and char *ch1 but assigning ch to ch1. That's where the error in your title is coming from.
Your code appears to be trying feed your SQL to MySQL one byte at a time and that's not going to do anything useful. I think you're meaning to use fgets to read the SQL file one line a time so that you can feed each line to MySQL as a single SQL statement.
You should spend some time reading about your compiler's warning switches

Related

How to connect and code a C Program with a MySQL database in Windows?

I have recently learned MySQL and I want to implement the knowledge on how to build a C Program using MySQL database on windows. Can anyone provide me with a detailed description on which files to download and whether I need XAMPP running or not?
I have tried to read the documentation here : https://dev.mysql.com/doc/refman/8.0/en/c-api-implementations.html . However , I couldn't figure out for my life how to do it on Windows.(Seems pretty easy for Linux, unfortunately I do not have a Linux machine)
It would be great if someone would provide me with a detailed and step by step explanation. Thanks in advance.
I know how the code would look like.
#include <stdio.h>
#include <stdlib.h>
#include <my_global.h>
#include <mysql.h>
typedef struct
{
char host[20];
char user[25];
char pass[50];
}DB_CONN_PARAMS;
MYSQL * connect_db(DB_CONN_PARAMS *params)
{
MYSQL *connection = mysql_init(NULL);//init connection
if (connection == NULL)
{//check init worked
fprintf(stderr, "%s\n", mysql_error(connection));
exit(EXIT_FAILURE):
}
//connect:
if (mysql_real_connect(
connection,
params->host,
params->user,
params->pass,
NULL,0,NULL,0)
==NULL)
{//connection failed?
fprintf(stderr, "%s\n", mysql_error(connection));
mysql_close(connection);
exit(EXIT_FAILURE):
}
return connection;
}
int main()
{
MYSQL *db;
DB_CONN_PARAMS *params = calloc(1,sizeof(DB_CONN_PARAMS));
//just an alternative way of passing connection params, find a struct easier
params->host = "127.0.0.1";
params->user = "root";
params->pass = "mySuperSecretPass";
MYSQL * connect_db(DB_CONN_PARAMS *params);
db = connect_db(params);
//we don't need the struct anymore
free(params);
params = NULL;
//do stuff
mysql_close(db);//close connection, of course!
return EXIT_SUCCESS;
}
I just need help with the setup.

GetLastError() return value 2

A very basic question, I cannot understand why I get error 2 with the simplest example of windows system programming book.
I post the source:
#include <Windows.h>
#include <stdio.h>
#define BUF_SIZE 65536
int main(int argc, LPTSTR argv[]){
HANDLE fileIN, fileOUT;
DWORD nIn, nOut; //numero di byte LETTI/SCRITTI
CHAR buffer[BUF_SIZE];
if(argc!=3){
printf("Usage: %s file1 file2\n", argv[0]);
return 1;
}
//printf("%s\n",argv[0]);
//printf("%s\n",argv[1]);
//printf("%s\n",argv[2]);
fileIN = CreateFileW(argv[1],GENERIC_READ,FILE_SHARE_READ,NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(fileIN == INVALID_HANDLE_VALUE){
printf("Invalid handle value, error %x\n", GetLastError());
return 2;
}
fileOUT = CreateFileW(argv[2], GENERIC_WRITE,0, NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL, NULL);
if(fileOUT == INVALID_HANDLE_VALUE){
printf("Invalid handle value, error %x\n", GetLastError());
return 3;
}
while(ReadFile(fileIN, buffer, BUF_SIZE, &nIn, NULL) && nIn > 0){
WriteFile(fileOUT, buffer, nIn, &nOut, NULL);
if(nIn != nOut){
printf("During copy, error %x\n", GetLastError());
return 4;
}
}
CloseHandle(fileIN);
CloseHandle(fileOUT);
return 0;
}
Can you help me please? I'm using MS VSc++ 2010 SP 1
Thank you all in advange
N.
Please, forgive me if I am wrong but I believe that you are trying to create files with functions unicode and, may be, you are providing paths ansi.
It is easy to test if I am wrong changing the terminating W into A.
Please, forgive my possible error.

MySQL C API: Need example for initiating DB on *Embedded* mode

I work on a C tool, in which I need to manipulate and query process-internal data intensively.
Hence, I've decided to use the MySQL C API on Embedded mode, so I can have an embedded DB for each running process of my tool, and use all the SQL features on it.
All the MySQL C API tutorials I've found over the web, deal with connecting to a running server, which is not my case.
There are some examples for working with Embedded mode in Oracle's MySQL website, but they are not simple, and it's hard to make them work.
Q: Can someone please point me or write me a short example for initiating a DB using MySQL C API on Embedded mode?
Thanks!
After a lot of tries, finally answering to myself and sharing it with you:
Create a central data directory that would be used for storing DBs. In this example, I'll use /tmp/mysql_embedded_data
> mkdir /tmp/mysql_embedded_data
Create a C file, as in the following example:
#include <my_global.h>
#include <mysql.h>
int main(int argc, char **argv) {
static char *server_options[] = {
"mysql_test", // An unused string
"--datadir=/tmp/mysql_embedded_data", // Your data dir
NULL };
int num_elements = (sizeof(server_options) / sizeof(char *)) - 1;
static char *server_groups[] = { "libmysqld_server",
"libmysqld_client", NULL };
// Init MySQL lib and connection
mysql_library_init(num_elements, server_options, server_groups);
MYSQL *con = mysql_init(NULL);
if (con == NULL) {
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}
mysql_options(con, MYSQL_READ_DEFAULT_GROUP, "libmysqld_client");
mysql_options(con, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);
// Connect to no host/port -> Embedded mode
if (mysql_real_connect(con, NULL, NULL, NULL, NULL, 0, NULL, 0) == NULL) {
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
// Create a sample empty DB, named "aNewDatabase"
if (mysql_query(con, "CREATE DATABASE aNewDatabase")) {
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
// Close connection
mysql_close(con);
exit(0);
}
Compile your file (I assume in this example that my C filename is /tmp/mysql_try.c)
gcc /tmp/mysql_try.c -o /tmp/mysql_try -lz `mysql_config --include --libmysqld-libs`
Run your compiled program:
> /tmp/mysql_try
When your program returnes, ensure that the DB creation succeeded. We named our sample DB aNewDatabase, so we'll check if it now has a directory inside the data directory we created -> We'll check if the directory /tmp/mysql_embedded_data/aNewDatabase was created:
> ls /tmp/mysql_embedded_data/aNewDatabase
db.opt
Enjoy!

segmentation fault when connecting to mysql database with c

Does anyone know why the following would cause a segmentation fault when run?
#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>
void main(void)
{
printf("MySQL client version : %s\n", mysql_get_client_info());
MYSQL *conn=NULL;
mysql_init(conn);
char *server = "localhost";
char *user = "root";
char *password = "pass";
char *database = "weather";
char *table ="room_temp";
char *tst_qry="INSERT INTO `weather`.`room_temp` (`idx`, `date`, `temperature`) VALUES (NULL, CURRENT_TIMESTAMP, '10')";
mysql_real_connect(conn, server, user, password, database, 0, NULL, 0);
}
I complied as follows
gcc -o mysql $(mysql_config --cflags) mysql.c $(mysql_config --libs)
The output was as follows,
MySQL client version : 5.5.31
Segmentation fault
Please help!
The allocated new object isn't stored in your code. Hence you are passing the NULL to mysql_real_connect().
Change this line:
mysql_init(conn);
to:
conn = mysql_init(conn);
or rather directly:
conn = mysql_init(NULL);
You're passing a NULL-pointer to mysql_real_connect. According to the documentation mysql_init returns an initialized MYSQL object (when passed a NULL-pointer). Change your code either to this to use the return value:
conn = mysql_init(conn);
or this, to have mysql_init fill the object:
MYSQL conn; /* note that this isn't a pointer */
mysql_init(&conn);
...
mysql_real_connect(&conn, ...);

mysql_real_escape_string() causing segmentation fault

I'm working on a C program that uses the mysql.h library. This is the code:
int newproduct(){
char *name;//nombre del prducto
char *desc;//descripcion del producto
double price;//precio del producto
cprint("Agregar nuevo producto\n\n");
printf("Nombre del producto: ");
scanf("%s", &name);
printf("Descripcion: ");
scanf("%s", &desc);
printf("Precio: ");
scanf("%e", &price);
MYSQL *conn;
conn = mysql_init(NULL);
if (!mysql_real_connect(conn, server,
user, password, database, port, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
char *rname = NULL;//string donde guardar el nombre con caracteres de escape
char *rdesc = NULL;
mysql_real_escape_string(conn,rname,name,strlen(name));//se realiza el real escape
mysql_real_escape_string(conn,rdesc,desc,strlen(desc));
/*
char *query;//donde guardar el query
snprintf(query,1000,"INSERT INTO productos (nombre,descripcion,stock,precio) VALUES( %s,%s, 0, %e)",rname,rdesc,price);//query a enviar
if (mysql_query(conn, query)) {//enviar el query
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}*/
mysql_close(conn);
return 0;
}
And this is the console execution
Agregar nuevo producto
Nombre del producto: a
Descripcion: a
Precio: 1
Segmentation fault (core dumped)
------------------
(program exited with code: 139)
Press return to continue
I'm sure that the mysql_real_escape_string is causing the segmentation fault because when I comment it the code works fine but it's my first time working with MySQL and I have no clue why this is failing. Besides that i was able to retrieve some info from the database so its not a connection error.
PD: cprint is a declared function that adds a header to the printf function containing mysql row data.
rname and rdesc are supposed to be buffers, not pointers to NULL. You must alloc the memory space first.
For instance:
char rname[1024];
char rdesc[1024];
mysql_real_escape_string(conn, rname, name, 1024);
mysql_real_escape_string(conn, rdesc, desc, 1024);
Also, the size of rname cannot be strlen(name), because the escape characters will require extra space. The required buffer may have to be up to four times bigger than the original string.
From the docs the to argument:
unsigned long mysql_real_escape_string(MYSQL *mysql, char *to, const char *from, unsigned long length)
^^^^^^^^
which in your case is rname and rdesc need to have space allocated to them. The documents specifically say:
You must allocate the to buffer to be at least length*2+1 bytes long.
You are also not using scanf correctly with name and desc since neither has space allocated to them. Something like the following would work:
char name[80];
scanf("%s", name);
If we go with the a size of 80 for name then following the document, the following would work for rname:
char rname[161] ;