使用GTK+2.0、sqlite3数据库、c语言 的简易的学生管理系统
实现了基本的增删查改
效果图:
代码:
1 #include <gtk/gtk.h> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <unistd.h> 5 #include "sqlite3.h" 6 7 GtkWidget *entry11 = NULL; 8 GtkWidget *entry22 = NULL; 9 GtkWidget *entry33 = NULL; 10 sqlite3 *db = NULL; 11 12 //回调 13 14 void callback_1(GtkButton *button,gpointer user_data) 15 { char **resultp ; 16 int nrow; 17 int ncolumn; 18 const char * entry_1; 19 const char * entry_2; 20 char sql[100]; 21 entry_1 = gtk_entry_get_text(GTK_ENTRY(entry11)); 22 entry_2 = gtk_entry_get_text(GTK_ENTRY(entry22)); 23 sprintf(sql,"select * from root_info where user_name = '%s' and password = '%s';",entry_1,entry_2); 24 printf("%s ",sql); 25 int result = 0; 26 char *errmsg = NULL; 27 result = sqlite3_open("test.db",&db); 28 if(result!=SQLITE_OK) 29 { 30 printf("open error "); 31 return -1; 32 } 33 else printf("succes "); 34 sqlite3_get_table(db,sql,&resultp,&nrow,&ncolumn,&errmsg); 35 printf("%d ",nrow); 36 printf("%d ",ncolumn); 37 //int q1=nrow; 38 //int q2=ncolumn; 39 40 41 //if(q1 == 1 && q2 == 2) 42 if(nrow == 1 && ncolumn ==2) 43 //if(nrow ==1 && ncolumn ==2) 44 { 45 printf("====%d ",nrow); 46 printf("====%d ",ncolumn); 47 gtk_widget_hide_all(user_data); 48 menu(); 49 printf("登陆 "); 50 sqlite3_close(db); 51 } 52 else 53 { printf("%d ",nrow); 54 printf("%d ",ncolumn); 55 printf("登陆失败"); 56 login_warning(); 57 58 sqlite3_close(db); 59 } 60 61 62 } 63 void callback_2(GtkButton *button,gpointer user_data) 64 { 65 insert_view(); 66 67 68 } 69 void callback_3(GtkButton *button,gpointer user_data) 70 { 71 select_view(); 72 73 74 } 75 void callback_5(GtkButton *button,gpointer user_data) 76 { 77 update_view(); 78 79 80 } 81 void callback_4(GtkButton *button,gpointer user_data) 82 { 83 delete_view(); 84 85 86 } 87 88 89 void callback_insert(GtkButton *button,gpointer user_data) 90 { 91 printf("insert "); 92 const char * entry_1; 93 const char * entry_2; 94 const char * entry_3; 95 char sql[100]; 96 entry_1 = gtk_entry_get_text(GTK_ENTRY(entry11)); 97 entry_2 = gtk_entry_get_text(GTK_ENTRY(entry22)); 98 entry_3 = gtk_entry_get_text(GTK_ENTRY(entry33)); 99 int n = atoi(entry_1); 100 int j = atoi(entry_3); 101 sprintf(sql,"insert into user_info values(%d,'%s',%d);",n,entry_2,j); 102 printf("%s ",sql); 103 int result = 0; 104 char *errmsg = NULL; 105 result = sqlite3_open("test.db",&db); 106 if(result!=SQLITE_OK) 107 { 108 printf("open error "); 109 return -1; 110 } 111 else printf("succes"); 112 int ret=sqlite3_exec(db,sql,NULL,NULL,&errmsg); 113 if(ret == SQLITE_OK) 114 { 115 printf("录入成功 "); 116 } 117 sqlite3_close(db); 118 } 119 void callback_delete(GtkButton *button,gpointer user_data) 120 { 121 122 const char * entry_1; 123 char sql[100]; 124 entry_1 = gtk_entry_get_text(GTK_ENTRY(entry11)); 125 int n =atoi(entry_1); 126 sprintf(sql,"delete from user_info where id = %d;",n); 127 printf("%s ",sql); 128 int result = 0; 129 char *errmsg = NULL; 130 result = sqlite3_open("test.db",&db); 131 if(result!=SQLITE_OK) 132 { 133 printf("open error "); 134 return -1; 135 } 136 else printf("succes"); 137 int ret=sqlite3_exec(db,sql,NULL,NULL,&errmsg); 138 if(ret == SQLITE_OK) 139 { 140 printf("删除成功 "); 141 } 142 sqlite3_close(db); 143 144 } 145 void callback_update(GtkButton *button,gpointer user_data) 146 { 147 148 const char * entry_1; 149 const char * entry_2; 150 const char * entry_3; 151 char sql[100]; 152 entry_1 = gtk_entry_get_text(GTK_ENTRY(entry11)); 153 entry_2 = gtk_entry_get_text(GTK_ENTRY(entry22)); 154 entry_3 = gtk_entry_get_text(GTK_ENTRY(entry33)); 155 int n = atoi(entry_1); 156 int j = atoi(entry_3); 157 sprintf(sql,"update user_info set grade = %d,name = '%s' where id =%d ;",j,entry_2,n); 158 printf("%s ",sql); 159 int result = 0; 160 char *errmsg = NULL; 161 result = sqlite3_open("test.db",&db); 162 if(result!=SQLITE_OK) 163 { 164 printf("open error "); 165 return -1; 166 } 167 else printf("succes"); 168 int ret=sqlite3_exec(db,sql,NULL,NULL,&errmsg); 169 if(ret == SQLITE_OK) 170 { 171 printf("更新成功 "); 172 } 173 sqlite3_close(db); 174 } 175 int result_find(void *para,int ncolumn,char ** columnvalue,char *columnname[]) 176 { 177 GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 178 GtkWidget *table = gtk_table_new(4, 6, TRUE); 179 gtk_widget_set_size_request(window,600,500); 180 gtk_window_set_title(GTK_WINDOW(window), "Student Manger System"); 181 gtk_container_add(GTK_CONTAINER(window), table); 182 183 int i; 184 char nlable[100]; 185 186 GtkWidget *label_1; 187 GtkWidget *label_2; 188 for(i = 0;i < ncolumn; i++) 189 { 190 label_1 = gtk_label_new("123 "); 191 gtk_label_set_text(GTK_LABEL(label_1),columnname[i] ); 192 gtk_table_attach_defaults(GTK_TABLE(table),label_1, 2,3, i+1, i+2); 193 label_2 = gtk_label_new("123 "); 194 gtk_label_set_text(GTK_LABEL(label_2),columnvalue[i]); 195 gtk_table_attach_defaults(GTK_TABLE(table),label_2, 3,4, i+1, i+2); 196 197 } 198 199 g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL); 200 gtk_widget_show_all(window); 201 gtk_main(); 202 203 204 205 return 0; 206 207 } 208 void callback_select_view(GtkButton *button,gpointer user_data) 209 { 210 const char * entry_1; 211 char sql[100]; 212 entry_1 = gtk_entry_get_text(GTK_ENTRY(entry11)); 213 int n =atoi(entry_1); 214 sprintf(sql,"select * from user_info where id = %d;",n); 215 printf("%s ",sql); 216 int result = 0; 217 char *errmsg = NULL; 218 result = sqlite3_open("test.db",&db); 219 if(result!=SQLITE_OK) 220 { 221 printf("open error "); 222 return -1; 223 } 224 else printf("succes"); 225 int ret=sqlite3_exec(db,sql,result_find,NULL,&errmsg); 226 if(ret == SQLITE_OK) 227 { 228 printf("查询成功 "); 229 } 230 sqlite3_close(db); 231 //select_result_view(); 232 233 234 } 235 int main() 236 { gtk_init(&argc,&argv); 237 login(); 238 return 0; 239 } 240 241 void login(int argc,char *argv[]) 242 { 243 244 GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 245 gtk_window_set_title(GTK_WINDOW(window), "Student Manger System"); 246 GtkWidget *table = gtk_table_new(5,5,TRUE); 247 gtk_widget_set_size_request(window,500,300); 248 gtk_window_set_resizable(GTK_WINDOW(window), FALSE); 249 250 GtkWidget *label1 = gtk_label_new("学 生 管 理 系 统"); 251 GtkWidget *label2 = gtk_label_new("版本:awsl 1.0"); 252 GtkWidget *label3 = gtk_label_new("user"); 253 GtkWidget *label4 = gtk_label_new("password"); 254 255 256 entry11 = gtk_entry_new(); 257 entry22 = gtk_entry_new(); 258 GtkWidget *button1 = gtk_button_new_with_label("登陆"); 259 260 gtk_entry_set_text(GTK_ENTRY(entry11),"请输入用户名"); 261 gtk_entry_set_text(GTK_ENTRY(entry22),"请输入密码"); 262 gtk_entry_set_visibility(GTK_ENTRY(entry22),FALSE); 263 264 265 266 gtk_container_add(GTK_CONTAINER(window),table); 267 gtk_table_attach_defaults(GTK_TABLE(table),label1,0,5,0,1); 268 gtk_table_attach_defaults(GTK_TABLE(table),label3,0,1,1,2); 269 gtk_table_attach_defaults(GTK_TABLE(table),label4,0,1,2,3); 270 gtk_table_attach_defaults(GTK_TABLE(table),label2,0,5,4,5); 271 272 gtk_table_attach_defaults(GTK_TABLE(table),entry11,1,4,1,2); 273 gtk_table_attach_defaults(GTK_TABLE(table),entry22,1,4,2,3); 274 275 gtk_table_attach_defaults(GTK_TABLE(table),button1,1,4,3,4); 276 277 278 g_signal_connect(button1,"pressed",G_CALLBACK(callback_1),window); 279 280 g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL); 281 gtk_widget_show_all(window); 282 gtk_main(); 283 } 284 void menu(int argc,char *argv[]) 285 { 286 287 GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 288 GtkWidget *table = gtk_table_new(7,7,TRUE); 289 gtk_widget_set_size_request(window,700,500); 290 gtk_window_set_title(GTK_WINDOW(window), "Student Manger System"); 291 292 GtkWidget *label1 = gtk_label_new("功能列表"); 293 GtkWidget *label2 = gtk_label_new("版本:awsl 1.0"); 294 //GtkWidget *label3 = gtk_label_new("haha"); 295 296 GtkWidget *button2 = gtk_button_new_with_label("增添学生信息"); 297 GtkWidget *button3 = gtk_button_new_with_label("查询学生信息"); 298 GtkWidget *button4 = gtk_button_new_with_label("删除学生信息"); 299 GtkWidget *button5 = gtk_button_new_with_label("更新学生信息"); 300 301 gtk_container_add(GTK_CONTAINER(window),table); 302 gtk_table_attach_defaults(GTK_TABLE(table),label1,0,7,0,1); 303 //gtk_table_attach_defaults(GTK_TABLE(table),label3,1,7,1,7); 304 305 gtk_table_attach_defaults(GTK_TABLE(table),button2,0,7,2,3); 306 gtk_table_attach_defaults(GTK_TABLE(table),button3,0,7,3,4); 307 gtk_table_attach_defaults(GTK_TABLE(table),button4,0,7,4,5); 308 gtk_table_attach_defaults(GTK_TABLE(table),button5,0,7,5,6); 309 gtk_table_attach_defaults(GTK_TABLE(table),label2,0,7,6,7); 310 311 312 g_signal_connect(button2,"pressed",G_CALLBACK(callback_2),window); 313 g_signal_connect(button3,"clicked",G_CALLBACK(callback_3),window); 314 g_signal_connect(button4,"clicked",G_CALLBACK(callback_4),window); 315 g_signal_connect(button5,"clicked",G_CALLBACK(callback_5),window); 316 317 318 g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL); 319 gtk_widget_show_all(window); 320 gtk_main(); 321 } 322 //添加 323 void insert_view(int argc,char *argv[]) 324 { 325 GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 326 GtkWidget *table = gtk_table_new(5,6,TRUE); 327 gtk_widget_set_size_request(window,600,500); 328 gtk_window_set_title(GTK_WINDOW(window), "Student Manger System"); 329 gtk_window_set_resizable(GTK_WINDOW(window), FALSE); 330 331 entry11 = gtk_entry_new(); 332 entry22 = gtk_entry_new(); 333 entry33 = gtk_entry_new(); 334 335 //GtkWidget entry_s [4] = {entry1,entry2,entry3}; 336 337 gtk_entry_set_text(GTK_ENTRY(entry11),"10086"); 338 gtk_entry_set_text(GTK_ENTRY(entry22),"ayao"); 339 gtk_entry_set_text(GTK_ENTRY(entry33),"99"); 340 341 GtkWidget *label1 = gtk_label_new("id"); 342 GtkWidget *label2 = gtk_label_new("name"); 343 GtkWidget *label3 = gtk_label_new("grade"); 344 GtkWidget *label4 = gtk_label_new("输入添加信息"); 345 GtkWidget *label5 = gtk_label_new("awsl 1.0"); 346 347 GtkWidget *button1 = gtk_button_new_with_label("提交"); 348 349 gtk_container_add(GTK_CONTAINER(window),table); 350 gtk_table_attach_defaults(GTK_TABLE(table),label4,0,5,0,1); 351 gtk_table_attach_defaults(GTK_TABLE(table),label1,0,1,1,2); 352 gtk_table_attach_defaults(GTK_TABLE(table),label2,0,1,2,3); 353 gtk_table_attach_defaults(GTK_TABLE(table),label3,0,1,3,4); 354 gtk_table_attach_defaults(GTK_TABLE(table),label5,0,5,5,6); 355 356 gtk_table_attach_defaults(GTK_TABLE(table),entry11,1,4,1,2); 357 gtk_table_attach_defaults(GTK_TABLE(table),entry22,1,4,2,3); 358 gtk_table_attach_defaults(GTK_TABLE(table),entry33,1,4,3,4); 359 360 gtk_table_attach_defaults(GTK_TABLE(table),button1,1,4,4,5); 361 362 363 364 365 366 g_signal_connect(button1,"pressed",G_CALLBACK(callback_insert),window); 367 368 369 g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL); 370 gtk_widget_show_all(window); 371 gtk_main(); 372 373 } 374 //删除 375 void delete_view(int argc,char *argv[]) 376 { 377 GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 378 GtkWidget *table = gtk_table_new(5,4,TRUE); 379 gtk_widget_set_size_request(window,600,400); 380 gtk_window_set_title(GTK_WINDOW(window), "Student Manger System"); 381 gtk_window_set_resizable(GTK_WINDOW(window), FALSE); 382 383 entry11 = gtk_entry_new(); 384 gtk_entry_set_text(GTK_ENTRY(entry11),"1111"); 385 386 GtkWidget *label1 = gtk_label_new("id"); 387 GtkWidget *label2 = gtk_label_new("输入要删除人的id"); 388 GtkWidget *label3 = gtk_label_new("awsl 1.0"); 389 390 GtkWidget *button1 = gtk_button_new_with_label("提交"); 391 392 gtk_container_add(GTK_CONTAINER(window),table); 393 gtk_table_attach_defaults(GTK_TABLE(table),label2,0,5,0,1); 394 gtk_table_attach_defaults(GTK_TABLE(table),label1,0,1,1,2); 395 gtk_table_attach_defaults(GTK_TABLE(table),label3,0,5,3,4); 396 397 gtk_table_attach_defaults(GTK_TABLE(table),entry11,1,4,1,2); 398 399 gtk_table_attach_defaults(GTK_TABLE(table),button1,1,4,2,3); 400 401 g_signal_connect(button1,"clicked",G_CALLBACK(callback_delete),window); 402 403 g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL); 404 gtk_widget_show_all(window); 405 gtk_main(); 406 } 407 //更新 408 void update_view(int argc,char *argv[]) 409 { 410 GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 411 GtkWidget *table = gtk_table_new(5,6,TRUE); 412 gtk_widget_set_size_request(window,600,500); 413 gtk_window_set_title(GTK_WINDOW(window), "Student Manger System"); 414 gtk_window_set_resizable(GTK_WINDOW(window), FALSE); 415 416 entry11 = gtk_entry_new(); 417 entry22 = gtk_entry_new(); 418 entry33 = gtk_entry_new(); 419 420 gtk_entry_set_text(GTK_ENTRY(entry11),"10086"); 421 gtk_entry_set_text(GTK_ENTRY(entry22),"ayao"); 422 gtk_entry_set_text(GTK_ENTRY(entry33),"99"); 423 424 GtkWidget *label1 = gtk_label_new("id"); 425 GtkWidget *label2 = gtk_label_new("name"); 426 GtkWidget *label3 = gtk_label_new("grade"); 427 GtkWidget *label4 = gtk_label_new("输入更新信息"); 428 GtkWidget *label5 = gtk_label_new("awsl 1.0"); 429 430 GtkWidget *button1 = gtk_button_new_with_label("提交"); 431 432 gtk_container_add(GTK_CONTAINER(window),table); 433 gtk_table_attach_defaults(GTK_TABLE(table),label4,0,5,0,1); 434 gtk_table_attach_defaults(GTK_TABLE(table),label1,0,1,1,2); 435 gtk_table_attach_defaults(GTK_TABLE(table),label2,0,1,2,3); 436 gtk_table_attach_defaults(GTK_TABLE(table),label3,0,1,3,4); 437 gtk_table_attach_defaults(GTK_TABLE(table),label5,0,5,5,6); 438 439 gtk_table_attach_defaults(GTK_TABLE(table),entry11,1,4,1,2); 440 gtk_table_attach_defaults(GTK_TABLE(table),entry22,1,4,2,3); 441 gtk_table_attach_defaults(GTK_TABLE(table),entry33,1,4,3,4); 442 443 gtk_table_attach_defaults(GTK_TABLE(table),button1,1,4,4,5); 444 445 g_signal_connect(button1,"clicked",G_CALLBACK(callback_update),window); 446 447 g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL); 448 gtk_widget_show_all(window); 449 gtk_main(); 450 451 } 452 //查询 453 select_view(int argc,char *argv[]) 454 { 455 GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 456 GtkWidget *table = gtk_table_new(5,4,TRUE); 457 gtk_widget_set_size_request(window,600,400); 458 gtk_window_set_title(GTK_WINDOW(window), "Student Manger System"); 459 gtk_window_set_resizable(GTK_WINDOW(window), FALSE); 460 461 entry11 = gtk_entry_new(); 462 gtk_entry_set_text(GTK_ENTRY(entry11),"10086"); 463 464 GtkWidget *label1 = gtk_label_new("id"); 465 GtkWidget *label2 = gtk_label_new("输入要查询人的id"); 466 GtkWidget *label3 = gtk_label_new("awsl 1.0"); 467 468 GtkWidget *button1 = gtk_button_new_with_label("查询"); 469 470 gtk_container_add(GTK_CONTAINER(window),table); 471 gtk_table_attach_defaults(GTK_TABLE(table),label2,0,5,0,1); 472 gtk_table_attach_defaults(GTK_TABLE(table),label1,0,1,1,2); 473 gtk_table_attach_defaults(GTK_TABLE(table),label3,0,5,3,4); 474 475 gtk_table_attach_defaults(GTK_TABLE(table),entry11,1,4,1,2); 476 477 gtk_table_attach_defaults(GTK_TABLE(table),button1,1,4,2,3); 478 479 g_signal_connect(button1,"clicked",G_CALLBACK(callback_select_view),window); 480 481 g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL); 482 gtk_widget_show_all(window); 483 gtk_main(); 484 } 485 //提示 486 login_warning(int argc,char *argv[]) 487 { 488 GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 489 gtk_widget_set_size_request(window,400,400); 490 gtk_window_set_title(GTK_WINDOW(window), "Student Manger System"); 491 gtk_window_set_resizable(GTK_WINDOW(window), FALSE); 492 GtkWidget *label = gtk_label_new("用户名或者密码错误!"); 493 gtk_container_add(GTK_CONTAINER(window),label); 494 g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL); 495 gtk_widget_show_all(window); 496 gtk_main(); 497 }