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] ;
Related
I want to add the mysql connection with the pcap loop which I am using in the code
MYSQL *con;
u_char *my_arguments = con;
pcap_loop(handle, total_packet_count, my_packet_handler, my_arguments);
but it is giving error
warning: initialization from incompatible pointer type
[-Wincompatible-pointer-types]
u_char *my_arguments = con;^~~
SO what should I do help is needed please
When I am directly putting value of con in pcap loop like pcap_loop(handle, total_packet_count, my_packet_handler, con); it is showing new error
Error is
passing argument 4 of ‘pcap_loop’ from incompatible pointer type
[-Wincompatible-pointer-types]
pcap_loop(handle, total_packet_count, my_packet_handler, con);
note: expected ‘u_char * {aka unsigned char *}’ but argument is of type ‘MYSQL * {aka struct st_mysql *}’
PCAP_API int pcap_loop(pcap_t *, int, pcap_handler, u_char *);
but I want it to be pushed in pcap loop
The "user" argument to pcap_loop(), pcap_dispatch(), and the callback probably should have been defined to be a void *, as it's a pointer to some arbitrary data that the callback understands, but it was defined to be a u_char * instead.
So you should cast the MYSQL * to u_char *:
pcap_loop(handle, total_packet_count, my_packet_handler, (u_char *)con);
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.
Here is my code:
#include<stdio.h>
#include<pcap.h>
void pcapdump(u_char* argument,const struct pcap_pkthdr* packet_header,const u_char* packet_content);
int main()
{
int i=0, devid,ret;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t *handle;
bpf_u_int32 mask;
bpf_u_int32 net;
int num_packets=500;
pcap_dumper_t *p;
pcap_if_t *alldevs;
pcap_if_t *pdev;
const struct pcap_pkthdr *packet_header;
const u_char *packet_content;
ret=pcap_findalldevs(&alldevs,errbuf);
if(ret=-1)
{
printf("%s",errbuf);
};
for (pdev = alldevs;pdev;pdev=pdev->next)
printf("#%d: %s %s %s\n",++i,pdev->name,pdev->description,pdev->description);
printf("select a device: ");
scanf("%d", &devid);
pdev=alldevs;
while (--devid)
pdev=pdev->next;
printf("Selected %s \n", pdev->name);
if (pcap_lookupnet(pdev->name,&net,&mask,errbuf)==-1)
{
printf("Couldn't get netmask for device %s: %s\n", pdev->name, errbuf);
net = 0;
mask = 0;
};
handle=pcap_open_live(pdev->name,BUFSIZ,1,0,errbuf);
printf("Number of packets: %d\n", num_packets);
pcap_dump_open(handle,"/home/jiangzhongbai/capturefiles/10.pcapng");
pcap_loop(handle,num_packets,pcap_dump,NULL);
pcap_dump_close(p);
pcap_freealldevs(alldevs);
pcap_close(handle);
printf("\nCapture complete.\n");
return 0;
}
The result is
eth0 (null) (null)
wlan0 (null) (null)
nflog Linux netfilter log (NFLOG) interface Linux netfilter log (NFLOG) interface
nfqueue Linux netfilter queue (NFQUEUE) interface Linux netfilter queue (NFQUEUE) interface
any Pseudo-device that captures on all interfaces Pseudo-device that captures on all interfaces
lo (null) (null)
select a device: 2
Selected wlan0
Number of packets: 500
Segmentation fault (core dumped)
I think there is something wrong with the functionpcap_dump_open.But I don't know how to solve the problem of Segmentation fault (core dumped).Please help me.
How to solve Segmentation fault (core dumped)
If pcap_findalldevs() returns -1, don't just print an error message, quit, because alldevs isn't necessarily set to a valid value or to NULL.
Do not assume that pdev->description is non-null - only print it if it's non-null.
Assign the result of pcap_dump_open() to the variable p.
Pass p, rather than NULL, as the fourth argument to pcap_loop().
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, ...);
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