1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| #include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include "sqlite3.h"
//FUNCTIONS
static int callback(void *data, int argc, char **argv, char **azColName){
int i;
fprintf(stderr, "%s: ", (const char*)data);
for(i=0; i<argc; i++){
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
static void
print_hello (GtkWidget *widget,
gpointer data)
{
g_print ("Hello World\n");
}
int main(int argc,char *argv[])
{
//GTK
GtkBuilder *builder;
GObject *mainWindow;
GObject *button;
GObject *entry;
//SQLITE3
sqlite3 *db=NULL;
int rc;
//Open database
rc = sqlite3_open("test.db", &db);
if(rc)
{
fprintf(stderr,"Can't open database: %s\n",sqlite3_errmsg(db));
exit(0);
}
else
{
fprintf(stderr,"Opened database successfully\n");
}
gtk_init(&argc,&argv);
// Construct a GtkBuilder instance and load our UI description
builder=gtk_builder_new();
gtk_builder_add_from_file(builder,"test.ui",NULL);
//Connect signal handlers to the constructed widgets
mainWindow=gtk_builder_get_object(builder,"mainWindow");
g_signal_connect(mainWindow,"destroy",G_CALLBACK(gtk_main_quit),NULL);
//Buttons
button=gtk_builder_get_object(builder,"buttonTest");
g_signal_connect(button,"clicked",G_CALLBACK(print_hello),NULL);
//Entries
entry=gtk_builder_get_object(builder,"entryTest");//mon champ texte
char *zErrMsg=0;
char sql[250]="";
const char *data = "";
snprintf(sql, 249,"SELECT (Description) FROM Fruits WHERE (Id) = 1;");//ma requête
rc=sqlite3_exec(db,sql,callback,(void*)data,&zErrMsg);
if(rc!=SQLITE_OK){
fprintf(stderr,"SQL error: %s\n",zErrMsg);
sqlite3_free(zErrMsg);
}else{
fprintf(stdout,"Operation done successfully\n");//la requête m'affiche le résultat dans le terminal
}
gtk_entry_set_text(GTK_ENTRY(entry),G_CALLBACK(callback)); //c'est ici que je veux envoyer la valeur de mon enregistrement dans mon champ texte
//j'obtient évidemment ce warning : main.c:82:2: warning: passing argument 2 of gtk_entry_set_text from incompatible pointer type [enabled by default] ; /usr/include/gtk-3.0/gtk/gtkentry.h:176:12: note: expected const gchar * but argument is of type void (*)(void)
gtk_main();
return 0;
} |
Partager