From: Javi <ja...@gs...> - 2004-03-08 01:23:21
|
Hi list. It's the first mail I send to the list, so I salute all :) It's the first time I write a programa for a public project, so please forgive if the code is dirty. I want to develop a pluging to connect to a MySQL server, execute a query and show on the LCD the number of afected rows. A very simple C program for doing tha could be like: --------------------------------------------- #include <mysql/mysql.h> #include <stdio.h> int main() { MYSQL *conex; MYSQL_RES *result; char *consulta = "select * from categories"; mysql_init(conex); mysql_real_connect(conex,"192.168.0.12","user","pass","databasename",0,NULL,0); mysql_real_query(conex,consulta,(unsigned int) strlen(consulta)); result=mysql_use_result(conex); printf("%d",mysql_field_count(conex)); mysql_close(conex); return 0; } --------------------------------------------- That program compiles and runs perfect by itselft on my PC. The only thing that I should done is just to include that simple program into a plugin. The plugin will be named plugin_sql.c I've followed the README.plugins and I can copile succesfully my plugin, but when running lcd4linux I can see a "beautifull" Viloacion de Segmento (which is the spanish term for SEGMENTATION FAULT) Here is the code. Hope someone could help me. A very sort description of the changes I've made on the plugin_sample.c file is included on the code. It's only a 50 lines' file so it's very easy to read. Thanks in advice. Stolz. ------------------------------------------ /* $Id: plugin_sql.c,v 0.1 2004/03/07 Exp $ * * plugin for exec SQL queries into a MySQL DB. * * Javier Garcia <ja...@NO...> * * This file is part of LCD4Linux. * */ /* * exported functions: * * int plugin_init_sql (void) * adds various functions * Remember to add -lmysqlclient option into Makefile. I.E: CC = gcc -lmysqlclient or run * #gcc -I/usr/include/mysql -L/usr/lib/mysql plugin_sql.c -lmysqlclient -o plugin_sql.o * This is an initial version for testing. my_consulta is basically the mult3 function * included on the plugin_sample.c file. I only added: * #include <mysql/mysql.h> t include the mysql API. * MYSQL *conex; to store the conection data. * Two lines to connect and disconnect to the DBSM. * mysql_init(conex); * mysql_close(conex); * */ #include <mysql/mysql.h> // define the include files you need #include "config.h" #include <stdlib.h> #include <string.h> #include <ctype.h> // these should always be included #include "debug.h" #include "plugin.h" #ifdef WITH_DMALLOC #include <dmalloc.h> #endif #include <mysql/mysql.h> #include <stdio.h> static void my_consulta (RESULT *result, RESULT *arg1) { double value=R2N(arg1)*3.0; MYSQL *conex; // MYSQL_RES *result; // MYSQL_ROW fila; // char *consulta = "select * from categories"; // int numero; // mysql_init(conex); // mysql_real_connect(conex,"192.168.0.2","myUser","myPass","myDB",0,NULL,0); // mysql_real_query(conex,consulta,(unsigned int) strlen(consulta)); // result=mysql_use_result(conex); // numero=mysql_field_count(conex); // printf("%d",numero); mysql_close(conex); // store result SetResult(&result, R_NUMBER, &value); } int plugin_init_sql (void) { AddFunction ("consulta", 1, my_consulta); return 0; } void plugin_exit_sql(void) { } |