473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

mysql C API calls hangs with no apparent reason

I cannot make the following program call the MYSQL C API when called
from the function process_token and I dont know why. I checked the WEB
and I did found nothing related to this bug. I am using slack 10, on a
x86 box with gcc. Mysql ver. 4.0.21.

Thanks in advance. Leonardo.

/************************************************** ************************/
#include <stdio.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <mysql.h>
/************************************************** ***
* Errores
************************************************** ***/
#define ERR_BAD_FILE -1
#define ERR_NO_MEM -2
#define ERR_DB -3
int bDebug = 0;
char *num;
char *type;
char *year;
char *estado;
char *asunto;
char *org;
char *fecha;
char *materia;
char *pub_cod;
char *cant_art;

xmlDoc *doc = NULL;
xmlNode *rnode = NULL;
xmlNode *tag = NULL;

MYSQL *searchdb;
MYSQL *actdb;
MYSQL_RES *searchres;
MYSQL_ROW searchrow;
/************************************************** ****
* substring: simplemente saca substrings de una cadena
* inicial, pero no reasigna memoria
************************************************** ****/

char *
substring (char *orig, char *dest, int offset, int lenght)
{
sprintf (dest, "%.*s", lenght, orig + offset);
return dest;
}

int
process_token (char *token)
{
int rc = 0;
int tlen = strlen(token);

xmlChar *query = malloc (400);
sprintf (query, " SELECT id FROM keys_id LIMIT 1");

if (bDebug)
printf ("query:\n%s\n\n", query);
rc = mysql_query(searchdb, query);
if (rc != 0)
{
fprintf (stderr, "No puedo buscar en la base de datos. Saliendo
\n"
"error %s\n", mysql_error (searchdb));
exit (ERR_DB);
}

searchres = mysql_use_result (searchdb);

searchrow = mysql_fetch_row (searchres);
if (searchrow[0])
{
xmlChar actquery[400];
memset (&actquery, 0, 400);
sprintf (actquery, "INSERT INTO keys_rel(doctype, emisor,
docnum, "
"emitted, docversion, key_id) VALUES('%s', '%s', "
"'%s', '%s','%d','%s');", type, org, num, fecha, 0,
searchrow[0]);
if (bDebug)
printf ("query de actualizacion: %s\n", actquery);

rc = mysql_real_query (actdb, actquery, strlen (actquery));
if (rc != 0)
{
fprintf (stderr, "No puedo actualizar la base de datos.
Saliendo.\n"
"query: %s\nerror:%s\n", actquery, mysql_error (actdb));
exit (ERR_DB);
}
if (bDebug)
printf ("se actualizo el descriptor con el número %s\n",
searchrow[0]);
}
mysql_free_result (searchres);
free (query);
return 0;
}
int
process_dsc (xmlNode * secnode)
{
xmlNode *tag = secnode->xmlChildrenNode;
while (tag != NULL)
{
if ((!xmlStrcmp (tag->name, (const xmlChar *) "DESCRIPT")))
{
xmlChar *token;
xmlChar *descr =
xmlNodeListGetString (doc, tag->xmlChildrenNode, 1);
if (bDebug)
printf ("%s\n", descr);
token = strtok (descr, "-");

while (token)
{
process_token (token);
token = strtok (descr, "-");
}
xmlFree (descr);
}
tag = tag->next;
}
xmlFree (tag);
return 0;
}
void my_setconn (void)
{
int rc = 0;
searchdb = mysql_init (NULL);
actdb = mysql_init (NULL);

searchdb = mysql_real_connect (searchdb, "localhost", "web",
NULL, "be", 0, NULL, 0);
if (!searchdb)
{
printf ("Error conectando a mysql. Saliendo.\n");
exit (ERR_DB);
}
else if (bDebug)
printf ("Estoy conectado a la primera base de datos.\n"
"el error dice %s\n", mysql_error (searchdb));

rc = mysql_select_db (searchdb, "be");
if (rc != 0)
{
printf ("Error conectando a la base de datos 'be'.
Saliendo.\n");
exit (ERR_DB);
}

actdb = mysql_real_connect (actdb, "localhost", "web",
NULL, "be", 0, NULL, 0);
if (!actdb)
{
printf ("Error conectando a mysql. Saliendo.\n");
exit (ERR_DB);
}
else if (bDebug)
printf ("Estoy conectado a la segunda base de datos.\n"
"el error dice %s\n", mysql_error (actdb));

rc = mysql_select_db (actdb, "be");
if (rc != 0)
{
printf ("Error conectando a la base de datos 'be'.
Saliendo.\n");
exit (ERR_DB);
}
}

/************************************************** ****
* Procesamos las gen
************************************************** ****/
int
process_gen (xmlNode * gennode)
{
xmlNode *tag = gennode->xmlChildrenNode;
while (tag != NULL)
{
if ((!xmlStrcmp (tag->name, (const xmlChar *) "OBS_GEN")))
{
xmlNode *obs = tag->xmlChildrenNode;
if ((!xmlStrcmp (obs->name, (const xmlChar *) "OBS")))
{
cant_art = malloc (7);
xmlChar *cont = xmlNodeListGetString (doc,
obs->xmlChildrenNode, 1);
xmlChar *temp = strstr (cont, "ACN");
if (temp)
{
sprintf (cant_art, "%.*s", 4, temp + 4);
if (bDebug)
printf ("La cantidad de artículos de la norma es %s\n",
cant_art);
}
xmlFree (temp);
xmlFree (cont);
}
xmlFree (obs);
}
tag = tag->next;
}

xmlFree (tag);
return 0;
}
int
process_vi (xmlNode * secnode)
{

return 0;
}
static inline int
process_co (xmlNode * secnode)
{

return 0;
}
int
process_fi (xmlNode * secnode)
{

return 0;
}
int
process_an (xmlNode * secnode)
{

return 0;
}
int
process_ar (xmlNode * secnode)
{

return 0;
}

int
process_re (xmlNode * secnode)
{

return 0;
}

int
main (int argc, char **argv)
{
/* asignamos varibles de trabajo */
char *ptype = NULL;
char *ptype_rg;
/* primero checkeamos que nos hayan pasado el archivo a procesar */
if (!argv[1])
{
printf ("Uso %s <<nombre de archivo hve>> <<debug>>\n\n",
argv[0]);
exit (0);
}
if (argv[2])
{
if (strcmp (argv[2], "debug") == 0)
bDebug = 1;
}

/*my_setconn ();*/

/* ahora parseamos el documento que tenemos como parámetro */
doc = xmlParseFile (argv[1]);
if (!doc)
{
printf ("No es posible parsear el documento %s, checkee y vuelva
"
"a intentar. Saliendo.\n\n", argv[1]);
exit (ERR_BAD_FILE);
}

if (bDebug)
printf ("se logró parsear el documento\n");

/* obtenemos el root node */
rnode = xmlDocGetRootElement (doc);

my_setconn();

if (rnode == NULL)
printf ("El documento esta vacío. Saliendo.\n\n");
else if (bDebug)
printf ("El nombre del root es %s\n", rnode->name);

/* ahora vamos por el primer field */
tag = rnode->xmlChildrenNode;

while (tag != NULL)
{
if (ptype && ptype_rg)
{
type = strdup ("REG");
xmlFree (ptype);
xmlFree (ptype_rg);
ptype = NULL;
ptype_rg = NULL;
}

if ((!xmlStrcmp (tag->name, (const xmlChar *) "FIELD")))
{
xmlChar *fieldname = xmlGetProp (tag, "NAME");
if ((!xmlStrcmp (fieldname, "STANDARD_NORMATIVO")))
{
if (bDebug)
printf ("se encontró el campo %s, con el valor %s\n",
fieldname, xmlNodeListGetString (doc,
tag->xmlChildrenNode,
1));
}
if ((!xmlStrcmp (fieldname, "NUMERO")))
{
num = xmlNodeListGetString (doc, tag->xmlChildrenNode, 1);
if (bDebug)
printf ("se encontró el campo %s, con el valor %s\n",
fieldname, num);
}
if ((!xmlStrcmp (fieldname, "ESTADO")))
{
estado = xmlNodeListGetString (doc, tag->xmlChildrenNode, 1);
if (bDebug)
printf ("se encontró el campo %s, con el valor %s\n",
fieldname, estado);
}
if ((!xmlStrcmp (fieldname, "ASUNTO")))
{
asunto = xmlNodeListGetRawString (doc, tag->xmlChildrenNode,
0);
if (bDebug)
printf ("se encontró el campo %s, con el valor %s\n",
fieldname, asunto);
}
if ((!xmlStrcmp (fieldname, "TIPO_NORMA")))
{
ptype = xmlNodeListGetString (doc, tag->xmlChildrenNode, 1);
if (bDebug)
printf ("se encontró el campo %s, con el valor %s\n",
fieldname, ptype);
}
if ((!xmlStrcmp (fieldname, "TIPO_RESOLUCION")))
{
ptype_rg = xmlNodeListGetString (doc, tag->xmlChildrenNode, 1);
if (bDebug)
printf ("se encontró el campo %s, con el valor %s\n",
fieldname, ptype_rg);
}
if ((!xmlStrcmp (fieldname, "ORGANISMO_EMISOR")))
{
org = xmlNodeListGetString (doc, tag->xmlChildrenNode, 1);
if (bDebug)
printf ("se encontró el campo %s, con el valor %s\n",
fieldname, org);
}
if ((!xmlStrcmp (fieldname, "FECHA")))
{
xmlChar *tmp =
xmlNodeListGetString (doc, tag->xmlChildrenNode, 1);
if (tmp)
{
/* alojamos espacio para un formato de fecha iso */
fecha = malloc (11);
if (!fecha)
{
perror ("imposible asignar memoria. Saliendo.\n");
exit (ERR_NO_MEM);
}
sprintf (fecha, "%.*s-%.*s-%.*s", 4, tmp, 2, tmp + 4, 2,
tmp + 6);
if (bDebug)
printf ("se encontró el campo %s, con el valor %s\n",
fieldname, fecha);
xmlFree (tmp);
}
}
if ((!xmlStrcmp (fieldname, "MAT")))
{
materia = xmlNodeListGetString (doc, tag->xmlChildrenNode, 1);
if (bDebug)
printf ("se encontró el campo %s, con el valor %s\n",
fieldname, materia);
}
if ((!xmlStrcmp (fieldname, "PUBLICACION_CODIFICADA")))
{
pub_cod = xmlNodeListGetRawString (doc, tag->xmlChildrenNode,
1);
if (bDebug)
printf ("se encontró el campo %s, con el valor %s\n",
fieldname, pub_cod);
}
if ((!xmlStrcmp (fieldname, "TEXTO")))
{
xmlNode *seccion = tag->xmlChildrenNode;
while (seccion != NULL)
{
if ((!xmlStrcmp (seccion->name, "S")))
{
xmlChar *tipo_seccion = xmlGetProp (seccion, "T");
if ((!xmlStrcmp (tipo_seccion, "GEN")))
process_gen (seccion);
else if ((!xmlStrcmp (tipo_seccion, "DSC")))
process_dsc (seccion);
else if ((!xmlStrcmp (tipo_seccion, "VI")))
process_vi (seccion);
else if ((!xmlStrcmp (tipo_seccion, "CO")))
process_co (seccion);
else if ((!xmlStrcmp (tipo_seccion, "RE")))
process_re (seccion);
else if ((!xmlStrcmp (tipo_seccion, "FI")))
process_fi (seccion);
else if ((xmlStrstr (tipo_seccion, "AR")))
process_ar (seccion);
else if ((xmlStrstr (tipo_seccion, "AN")))
process_an (seccion);
}
seccion = seccion->next;
}
if (bDebug)
printf ("se encontró el campo texto\n\n");

}
xmlFree (fieldname);
xmlFree (fecha);
}
tag = tag->next;
}

xmlFreeDoc (doc);

xmlCleanupParser ();

return (0);
}
Jul 20 '05 #1
3 1792
Leonardo Javier Bel?n wrote:
I cannot make the following program call the MYSQL C API when called
from the function process_token and I dont know why. I checked the WEB
and I did found nothing related to this bug. I am using slack 10, on a
x86 box with gcc. Mysql ver. 4.0.21.

Your quoted program is very long.

For debugging a problem like this start with a very short program.

ie a progrma that connects to the db, reports whether successful
connection then closes.

As for the mysql calls, have you loaded the correct libraries?
eg MySQL-shared-4.0.21 or MySQL-shared-compat-4.0.21.

Beyond this I cant help much because *I* am having problems with the
different variations of the mysql-real-connect function!
Jul 20 '05 #2
2metre,
I have built MySQL myself from the sources, so I realized that the
libraries should be OK. The problem is that the calls on the function
process_token hang the program, while the same call in the main
function is perfectly fine, and thats why I sent such a long snipplet.
I dont know if this is a bug or a problem in my code....

Leonardo.
2metre <2m****@xxxhersham.net> wrote in message news:<cl**********@sparta.btinternet.com>...
Leonardo Javier Bel?n wrote:
I cannot make the following program call the MYSQL C API when called
from the function process_token and I dont know why. I checked the WEB
and I did found nothing related to this bug. I am using slack 10, on a
x86 box with gcc. Mysql ver. 4.0.21.

Your quoted program is very long.

For debugging a problem like this start with a very short program.

ie a progrma that connects to the db, reports whether successful
connection then closes.

As for the mysql calls, have you loaded the correct libraries?
eg MySQL-shared-4.0.21 or MySQL-shared-compat-4.0.21.

Beyond this I cant help much because *I* am having problems with the
different variations of the mysql-real-connect function!

Jul 20 '05 #3
Thanks for your help but the problem was solved. The matter is that I
free a pointer I must not.
Leonardo.

2metre <2m****@xxxhersham.net> wrote in message news:<cl**********@sparta.btinternet.com>...
Leonardo Javier Bel?n wrote:
I cannot make the following program call the MYSQL C API when called
from the function process_token and I dont know why. I checked the WEB
and I did found nothing related to this bug. I am using slack 10, on a
x86 box with gcc. Mysql ver. 4.0.21.

Your quoted program is very long.

For debugging a problem like this start with a very short program.

ie a progrma that connects to the db, reports whether successful
connection then closes.

As for the mysql calls, have you loaded the correct libraries?
eg MySQL-shared-4.0.21 or MySQL-shared-compat-4.0.21.

Beyond this I cant help much because *I* am having problems with the
different variations of the mysql-real-connect function!

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ian.H | last post by:
Hi all, I've taken on a massive project for a client recently and now I've hit some bug fixes that are required (3rd party code). and was just wondering about peoples opinions / personal...
0
by: David H | last post by:
Background. I'm running on WinXP w/ MS Services for Unix installed (to give rsh/rlogin ability), both Python 2.3 and 2.4 version. In linux, I'm running RHEE with python2.3 version. The code...
0
by: Arthur Perry | last post by:
Hello, I am having this problem that is just driving me crazy.. When using Bugzilla with Mysql (all Mysql, Perl, and modules all supplied by RedHat for RHEL 3.0), MYSQL hangs frequently. I have...
175
by: Sai Hertz And Control Systems | last post by:
Dear all, Their was a huge rore about MySQL recently for something in java functions now theirs one more http://www.mysql.com/doc/en/News-5.0.x.html Does this concern anyone. What I...
39
by: Mairhtin O'Feannag | last post by:
Hello, I have a client (customer) who asked the question : "Why would I buy and use UDB, when MySql is free?" I had to say I was stunned. I have no experience with MySql, so I was left sort...
8
by: Scott | last post by:
Hi guys, If I try to call read(), readline(), readtoend() and there is nothing to read (from a never ending loop for example) the program seems to continue but it exits the loop for no apparent...
3
by: Grumpy Aero Guy | last post by:
I am upgrading an app from 6.0 to VB. Have a couple of radio buttons that gave me the "may fire when form is initialized" upgrade message. (no problem) Added an Isinitializing property...
6
by: onnodb | last post by:
Hi all, While working on an Access UI to a MySQL database (which should be a reasonable, low-cost, flexible interface to the DB, better than web-based, much less costly than a full-fledged .NET...
14
by: Ben | last post by:
I don't know whether anyone can help, but I have an odd problem. I have a PSP (Spyce) script that makes many calls to populate a database. They all work without any problem except for one...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.