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) { } |
From: Michael R. <re...@eu...> - 2004-03-08 05:50:23
|
Hi Javi, > 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. Fine! > 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 Please call it "plugin_mysql" > 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. The code looks quite clean, I can't see any bugs here. But I could not test it, as I don't have MySQL installed anywhere. But I did a test with the plugin_sample.c, this one works fine without segfaulting. How did you test it? Are you shure the segfault is caused by your plugin? (The current CVS code isn't very stable at the moment) I'm always testing plugins with the "interactive" mode (lcd4linux -i). Are you familiar with gdb? gdb is good for locating such segfaults. bye, Michael -- Michael Reinelt Tel: +43 676 3079941 Geisslergasse 4 Fax: +43 316 692343 A-8045 Graz, Austria e-mail: re...@eu... |
From: Javi <ja...@gs...> - 2004-03-08 10:06:29
|
=2D----- El Lunes, 8 de Marzo de 2004 05:33, Michael Reinelt escribi=F3: --= =2D--- > How did you test it? Are you shure the segfault is caused by your=20 > plugin? (The current CVS code isn't very stable at the moment) >=20 > I'm always testing plugins with the "interactive" mode (lcd4linux -i). >=20 > Are you familiar with gdb? gdb is good for locating such segfaults. >=20 >=20 > bye, Michael >=20 I've installed gdb and these are the results: # gdb --args lcd4linux -Fq GNU gdb 6.0 Copyright 2003 Free Software Foundation, Inc. (gdb) run Starting program: /home/javi/programacion/lcd4linux/lcd4linux -Fqf Program received signal SIGSEGV, Segmentation fault. 0x40020995 in mysql_init () from /usr/lib/libmysqlclient.so.12 (gdb) bt #0 0x40020995 in mysql_init () from /usr/lib/libmysqlclient.so.12 #1 0xbfffee18 in ?? () #2 0x4100b480 in _dl_runtime_resolve () from /lib/ld-linux.so.2 #3 0x080544a7 in my_consulta (result=3D0x806ac80, arg1=3D0x0) at plugin_sq= l.c:57 #4 0x0804d5d8 in EvalTree (Root=3D0x1) at evaluator.c:964 #5 0x0804d6e9 in Eval (root=3D0x806ac60, result=3D0xbfffef60) at evaluator= =2Ec:1170 #6 0x0804f7aa in widget_text_update (Self=3D0x80694f4) at widget_text.c:238 #7 0x0804ec89 in timer_process (delay=3D0xbffff000) at timer.c:160 #8 0x0804a730 in main (argc=3D-1073745920, argv=3D0x805e580) at lcd4linux.= c:627 The problem seems to be the mysql_init () function, from /usr/lib/libmysqlc= lient.so.12 =BFIs there something I can do? or is better I forgets the plugin and wait = untill the "exec" plugin is ported to the NextGen. Thanks. Bye |
From: Michael R. <re...@eu...> - 2004-03-08 10:27:20
|
Hi Javi, > I've installed gdb and these are the results: >=20 > # gdb --args lcd4linux -Fq > GNU gdb 6.0 > Copyright 2003 Free Software Foundation, Inc. > (gdb) run > Starting program: /home/javi/programacion/lcd4linux/lcd4linux -Fqf >=20 > Program received signal SIGSEGV, Segmentation fault. > 0x40020995 in mysql_init () from /usr/lib/libmysqlclient.so.12 > (gdb) bt > #0 0x40020995 in mysql_init () from /usr/lib/libmysqlclient.so.12 > The problem seems to be the mysql_init () function, from /usr/lib/libmy= sqlclient.so.12 You are right. Strange.... I know nothing about MySQL, but the following from your code looks=20 strange to me: MYSQL *conex; mysql_init(conex); conex seems to be something like a 'Context'. As it is a pointer, it=20 points to nowhere, and no memory is allocated for the data structure. Maybe this should be: MYSQL conex; mysql_init(&conex); Note that here the structure is allocated on the stack, and the address=20 is passed to mysql_init(). > =BFIs there something I can do? or is better I forgets the plugin and w= ait untill the "exec" plugin is ported to the NextGen. No, I don't want you to forget this plugin. This looks cool! I think=20 there are a lot of uses for such a database plugin! What would be even cooler are some status informations from MySQL. Any=20 ideas where to get them from? But I see another problem here, same as with the (not yet ported, for=20 exactly this reason) mail plugin: These plugins can take some time to=20 deliver a result (if, for example, the query is quite complicated, or=20 lots of rows are affected; or if the mailbox must be polled via a slow=20 internet connection). This would distrub lcd4linux's quite tight timings. Such plugins should work asynchronously. Which in turn is quite complex=20 to implement. The plugin should fork() on initialization, the child=20 would be responsible for fetching the data, the parent takes the results=20 via shared memory (and locked by a mutex) and delivers them to the=20 evaluator. Or is there another (probably easier) way to achieve this? Anyone out there with experience with such things? bye, Michael --=20 Michael Reinelt Tel: +43 676 3079941 Geisslergasse 4 Fax: +43 316 692343 A-8045 Graz, Austria e-mail: re...@eu... |
From: Michael R. <re...@eu...> - 2004-03-09 13:42:09
|
I'm resending this mail because i got several "relaying denied" errors from my provider. Sorry if you get this twice... Hi Javi, >> MYSQL conex; mysql_init(&conex); > :) That was the problem!. I was using the code that the oficial > tutorial shows as an exaple so I supposed it to be correct. Great! I love it when I can fix bugs with both eyes blind :-) > Ok. I just was disappointed because I thoght there could be a problem > in the mysql_init() out of my programing level . I'll continue > developing the plugin, adding and improveing new features. Fine! So you are the maintainer of the MySQL-plugin! There are several things to manage: - you should be subscribed to the lcd4linux-devel mailing list - you will get a developer access for the SourceForge CVS. Do you already have a account on SourceForge? If not, please get one, and tell me your username. >> But I see another problem here: These plugins can take some time to >> deliver a result (if, for example, the query is quite complicated, >> or lots of rows are affected) This would distrub lcd4linux's quite >> tight timings. > > Because of its simplicity, MySQL is a very quick DB manager but I > don't know if it can satisfy lcd4lnux's time requirements. I'm afraid it will not. Especially if the monitored database is on another server (which is possible, isn't it?) >> Such plugins should work asynchronously. Which in turn is quite >> complex to implement. The plugin should fork() on initialization, >> the child would be responsible for fetching the data, the parent >> takes the results via shared memory (and locked by a mutex) and >> delivers them to the evaluator. > > hummmmm, my programing level still can't reach these stuffs Ok, lets go for it the following way: You cevelop and maintain the plugin in a "normal" way, and I try to implement the async stuff with the mail plugin, and finally we put things together. bye, Michael -- Michael Reinelt Tel: +43 676 3079941 Geisslergasse 4 Fax: +43 316 692343 A-8045 Graz, Austria e-mail: re...@eu... |
From: Javi <ja...@gs...> - 2004-03-09 17:06:16
Attachments:
plugin_mysql.c
|
Hi. > Fine! So you are the maintainer of the MySQL-plugin! There are several > things to manage: > - you should be subscribed to the lcd4linux-devel mailing list Done :) > - you will get a developer access for the SourceForge CVS. Do you > already have a account on SourceForge? If not, please get one, and tell > me your username. My SF username is "javistolz". I've never used CVS, so I don't know how it works. I'll read the official docs. I attach the first release of plugin_mysql. A sample layout could be: ################ Variables { minute 60000 } Layout testMySQL { Row1 { Col1 'mySQLtest1' } Row2 { Col1 'mySQLtest2' } } Widget mySQLtest1 { class 'Text' #expression mySQLquery('server','login',password','database','query') expression mySQLquery('192.168.0.25','johnSmith','topSecret','myShop','SELECT orders_id FROM orders WHERE orders_status = 1') width 20 align 'R' prefix 'New Orders:' update minute } Widget mySQLtest2 { class 'Text' expression mySQLstatus('192.168.0.25','johnSmith','topSecret') width 20 align 'M' prefix 'Server:' update minute } ################ Please send comments,feedback and corrections. Bye Javier. |
From: M. R. <mr2...@gm...> - 2004-03-09 20:35:16
|
Hi Javi, On Tuesday 09 March 2004 18:59, Javi wrote: > I've never used CVS, so I don't know how it works. I'll read the official > docs. Since you are using KMail, I guess you might have Cervisia installed too. If so, you may find this articel "http://dot.kde.org/1077262834/" -> OSNews.com: Learning CVS Using KDE's Cervisia quite useful. Cheers, Moppel |
From: Xavier V. <xav...@fr...> - 2004-03-09 21:46:36
|
Hello list, Buenos dias Javier :) > Please send comments,feedback and corrections. I've read the code of the plugin and I have one suggestion : I think the plugin will be mostly used with a single MySQL server. Maybe the user can set his server, user and pass once in the Variables, and then call the mySQLquery() with only two arguments. There's a big argument for this : Using one persistent connection to the server will use less CPU and will keep the plugin faster. Connecting to the server and disconnecting at each refresh loop isn't clever and will make it lag. I propose you to connect to the server in plugin_mysql_init and don't forget to disconnect in plugin_mysql_quit. Maybe you should introduce some connection tracking : if the connection is lost (bad link, overload, timeout), reconnect to the server. What about this ? Jerry ? Michael ? Adios ! -- Xavier VELLO <xav...@fr...> |
From: Michael R. <re...@eu...> - 2004-03-10 07:53:52
|
Hi Xavier, Nice to hear from you! > I've read the code of the plugin and I have one suggestion : I think the > plugin will be mostly used with a single MySQL server. > Maybe the user can set his server, user and pass once in the Variables, > and then call the mySQLquery() with only two arguments. There's a big > argument for this : > Using one persistent connection to the server will use less CPU and will > keep the plugin faster. Connecting to the server and disconnecting at > each refresh loop isn't clever and will make it lag. > > I propose you to connect to the server in plugin_mysql_init and don't > forget to disconnect in plugin_mysql_quit. Maybe you should introduce > some connection tracking : if the connection is lost (bad link, > overload, timeout), reconnect to the server. Good idea! Especially the persistent connection! For the variables I would choose a different approach: specify server, user and password directly in the config file, e.g. MySQL { Server 'foo.bar' User 'michael' Password 'swordfish' } To allow even more flexibility, the query() function could allow either two or five arguments, if there are two, use the values from the config file, if there are five, act like now. bye, Michael -- Michael Reinelt Tel: +43 676 3079941 Geisslergasse 4 Fax: +43 316 692343 A-8045 Graz, Austria e-mail: re...@eu... |
From: Nico W. <Nic...@po...> - 2004-03-10 09:02:37
|
Hello, > I propose you to connect to the server in plugin_mysql_init and don't > forget to disconnect in plugin_mysql_quit. Maybe you should introduce > some connection tracking : if the connection is lost (bad link, > overload, timeout), reconnect to the server. As far as I know does the mechanism _quit function only exists for drivers and widgets and not for the plugins. But I would appreciate if the plugins supports this too. Bye, Nico |
From: Michael R. <re...@eu...> - 2004-03-10 09:10:38
|
Hi Nico, > As far as I know does the mechanism _quit function only exists for drivers > and widgets and not for the plugins. But I would appreciate if the plugins > supports this too. There is, thanks to Martin. I hope that someday in the near future, when we have dynamic loading of plugins, that this quit function can be called transparently by a dl_unload() or something similar. bye, Michael -- Michael Reinelt Tel: +43 676 3079941 Geisslergasse 4 Fax: +43 316 692343 A-8045 Graz, Austria e-mail: re...@eu... |
From: Michael R. <re...@eu...> - 2004-03-10 07:49:53
|
Hi Javier, > I attach the first release of plugin_mysql. Thanks, I did check it into CVS As for your questions regarding CVS, you can of course use Cervisia or something similar, but the 'basic' usage is quite simple, too: you need ssh installed, and you have to tell CVS to use SSH: export CVS_RSH=ssh (maybe add this to your .profile) First-Time check out: (all in one line!) cvs -z3 -d:ext:dev...@cv...:/cvsroot/lcd4linux co lcd4linux (substitute 'developername' with your SF account) will create you a new directory 'lcd4linux' Updates from the CVS (should be done very regularly, especially before you start working on the code): cvs -z3 update (in the lcd4linux directory) I want you not to do any check ins at this time (I want this from every new developer), rather send me a patch with the following command: cvs -z3 diff -u >patch.txt As for your plugin, I added a quite simple autoconf check for the mysql.h, and depending on this parts of the plugin will not be compiled (and will not produce errors for the missing mysql.h) As I don't have mysql installed here, I could net test it, but I'm shure it will not work: The library is not specified anywhere. We need to create a better test for MySQL in the configure.in, which detects not only the existence of the header, but the library, too, and sets the LFLAGS accordingly. Anybody has a ready-to-use autoconf macro for this? bye, Michael -- Michael Reinelt Tel: +43 676 3079941 Geisslergasse 4 Fax: +43 316 692343 A-8045 Graz, Austria e-mail: re...@eu... |