help on gtk with mysql.



Hi
 
    I am Sameer.I am using gtk with database as mysql.I have written the c api for connecting to the database.The problem is values typed in the entry are not entering to the database.
 
I am attaching the code which i have done.
please help me.
 

#include <gtk/gtk.h>
#include <stdlib.h>
#include <mysql.h>
#include <glib.h>


char *p;
GtkEntry *entry_box;

gint delete_event(GtkWidget *widget, GdkEvent event, gpointer data)
{
return FALSE;
}
void end_program(GtkWidget *widget, gpointer data)
{
gtk_main_quit();
}

void doSQL(MYSQL *conn,char *command)
{
printf("%s\n",command);
mysql_query(conn,command);
printf("result: %s\n",mysql_error(conn));
}

a()

{
MYSQL *conn;
conn=mysql_init(NULL);
if(mysql_real_connect(conn,"localhost",NULL,NULL,"ravi",0,NULL,0))
{
printf("connection made\n");
g_object_get(entry_box,"text",&p,NULL);
p=strdup(p);

doSQL(conn,"INSERT INTO test(id )VALUES(p)"); /*LINE 65 */

}
}


int main(int argc, char **argv)
{
GtkWidget *window;
GtkWidget *box;
GtkButton *b;

gtk_init(&argc,&argv);
window=g_object_new(GTK_TYPE_WINDOW,"title","dataentry widget","border-width",12,"default-width",800,NULL);
g_signal_connect(window,"delete_event",G_CALLBACK(delete_event),NULL);
g_signal_connect(window,"destroy",G_CALLBACK(end_program),NULL);
box=gtk_vbox_new(FALSE,0);
entry_box=g_object_new(GTK_TYPE_ENTRY,"text","",NULL);

b=g_object_new(GTK_TYPE_BUTTON,"label","INSERT",NULL);
g_signal_connect(b,"clicked",G_CALLBACK(a),NULL);
gtk_container_add(GTK_CONTAINER(window),box);
gtk_box_pack_start_defaults(GTK_BOX(box),entry_box);
gtk_box_pack_start_defaults(GTK_BOX(box),b);
gtk_widget_show_all(GTK_WIDGET(window));
gtk_main();
return 0;
}

1).When I executed the test executive file, a small window with an entry widget and a button opened up and
I put the string bb in the GtkEntry widget and pressed the INSERT button I recieved the folling output
root#] ./test
connection made.
INSERT INTO test(id) VALUES (p)
result: unknown column 'p' in 'field list'.

2). When I changed the code on line 65 into --doSQL(conn,"INSERT INTO test(id)VALUES('p')")--.and recompiled
the program and again put the string bb in the entry widget I got the following
output
connection made
INSERT INTO test(id) VALUES ('p')
result:

This time the string p got inserted into the database.

3). The problem lies with the sql query string "INSERT INTO test(id)VALUES(p)"
I want to know in the following program what form the variable p should take in the parenthesis immediatley
after the keyword VALUES. so that the insertion from the entry widget into the database coult succeed.

 I am using Red hat Linux v9.0.

regards

sameer.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]