数据库表数据展示页面
界面图: 插入数据功能我还没有思路,不太好整,大作业下个星期就该交了,就先搞到这吧!以后有时间再回来研究。 整个系统完整代码:
#include <gtk/gtk.h>
#include <stdio.h>
#include <string.h>
#include <mysql.h>
#include<stdlib.h>
#pragma comment (lib, "libmysql.lib")
MYSQL* conn_prt;
MYSQL_RES* res;
MYSQL_ROW row;
void self_init()
{
conn_prt = mysql_init(NULL);
}
char* _(char* c)
{
return(g_locale_to_utf8(c, -1, 0, 0, 0));
}
char* _2(const char* c)
{
return(g_locale_to_utf8(c, -1, 0, 0, 0));
}
GtkWidget* window_login;
GtkWidget* entry_login_username;
GtkWidget* entry_login_password;
GtkWidget* window_database;
GtkWidget* window_tb;
GtkWidget* entrydb;
GtkWidget* window_tbdesc;
GtkWidget* window_tbinfo;
GtkWidget* tb_name;
GtkWidget* tb_sql;
gchar* dbvalue;
gchar* tbvalue;
gchar* tbinfocolname;
gchar* tbinfocolvalue;
void login_submit(GtkButton* button, gpointer user_data);
void login_reset(GtkButton* button, gpointer user_data);
void create_db_window();
void init_dblist(GtkWidget* list);
void add_to_dblist(GtkWidget* list, const gchar* str);
void on_dbchanged(GtkWidget* widget, gpointer label);
void db_clicked_delete(GtkWidget* widget, gpointer select);
void db_clicked_new(GtkWidget* widget, gpointer entry);
void db_clicked_choose(GtkWidget* widget, gpointer list);
void create_tb_window();
void init_tblist(GtkWidget* list);
void add_to_tblist(GtkWidget* list, const gchar* str);
void on_tbchanged(GtkWidget* widget, gpointer label);
void tb_clicked_back(GtkWidget* widget, gpointer data);
void tb_clicked_delete(GtkWidget* widget, gpointer select);
void tb_clicked_desc(GtkWidget* widget, gpointer data);
void tb_clicked_new(GtkWidget* widget, gpointer data);
void tb_clicked_choose(GtkWidget* widget, gpointer data);
void create_tbdesc_window();
void init_tbdesclist(GtkWidget* list);
void create_tbinfo_window();
void init_tbinfolist(GtkWidget* list);
void add_to_tbdesclist(GtkWidget* list, const gchar* str1, const gchar* str2, const gchar* str3, const gchar* str4, const gchar* str5, const gchar* str6);
void tbdesc_clicked_back(GtkWidget* widget, gpointer data);
void add_to_tbinfolist(GtkWidget* list, const gchar* str1, const gchar* str2, const gchar* str3, const gchar* str4, const gchar* str5);
void tbinfo_clicked_back(GtkWidget* widget, gpointer data);
void on_tbinfochanged(GtkWidget* widget, gpointer data);
void tbinfo_clicked_delete(GtkWidget* widget, gpointer select);
int main(int argc, char* argv[]) {
gtk_init(&argc, &argv);
self_init();
window_login = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window_login), _2("登录"));
GtkWidget* table_login = gtk_table_new(3, 3, true);
gtk_container_add(GTK_CONTAINER(window_login), table_login);
GtkWidget* label_login_username = gtk_label_new(_2("用户名"));
gtk_table_attach_defaults(GTK_TABLE(table_login), label_login_username, 0, 1, 0, 1);
entry_login_username = gtk_entry_new();
gtk_table_attach_defaults(GTK_TABLE(table_login), entry_login_username, 1, 3, 0, 1);
GtkWidget* label_login_password = gtk_label_new(_2("密码"));
gtk_table_attach_defaults(GTK_TABLE(table_login), label_login_password, 0, 1, 1, 2);
entry_login_password = gtk_entry_new();
gtk_table_attach_defaults(GTK_TABLE(table_login), entry_login_password, 1, 3, 1, 2);
gtk_entry_set_visibility(GTK_ENTRY(entry_login_password), false);
GtkWidget* button_login = gtk_button_new_with_label(_2("登录"));
gtk_table_attach_defaults(GTK_TABLE(table_login), button_login, 0, 1, 2, 3);
g_signal_connect(button_login, "pressed", G_CALLBACK(login_submit), NULL);
GtkWidget* button_reset = gtk_button_new_with_label(_2("重置"));
gtk_table_attach_defaults(GTK_TABLE(table_login), button_reset, 2, 3, 2, 3);
g_signal_connect(button_reset, "pressed", G_CALLBACK(login_reset), NULL);
g_signal_connect(window_login, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_window_set_position(GTK_WINDOW(window_login), GTK_WIN_POS_CENTER_ALWAYS);
gtk_container_set_border_width(GTK_CONTAINER(window_login), 10);
gtk_widget_show(window_login);
gtk_widget_show_all(window_login);
gtk_main();
return 0;
}
void login_submit(GtkButton* button, gpointer user_data)
{
const char* username = gtk_entry_get_text(GTK_ENTRY(entry_login_username));
const char* password = gtk_entry_get_text(GTK_ENTRY(entry_login_password));
if (!mysql_real_connect(conn_prt, "localhost", username, password, "mysql", 3306, NULL, 0))
{
printf("failed to connect:%s\n", mysql_error(conn_prt));
}
else {
mysql_set_character_set(conn_prt, "gbk");
printf("connect success!\n");
gtk_widget_hide(window_login);
create_db_window();
}
}
void login_reset(GtkButton* button, gpointer user_data)
{
gtk_entry_set_text(GTK_ENTRY(entry_login_username), "");
gtk_entry_set_text(GTK_ENTRY(entry_login_password), "");
}
void create_db_window()
{
GtkWidget* db_list;
GtkWidget* db_box;
GtkWidget* db_label;
GtkTreeSelection* selection;
window_database = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window_database), _2("数据库展示页面"));
g_signal_connect(window_database, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_window_set_position(GTK_WINDOW(window_database), GTK_WIN_POS_CENTER_ALWAYS);
gtk_container_set_border_width(GTK_CONTAINER(window_database), 10);
GtkWidget* toolbar = gtk_toolbar_new();
GtkToolItem* btn1 = gtk_tool_button_new(NULL, _2("新建"));
gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(btn1), GTK_STOCK_ADD);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), btn1, 0);
GtkToolItem* btn2 = gtk_tool_button_new(NULL, _2("删除"));
gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(btn2), GTK_STOCK_DELETE);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), btn2, 1);
GtkToolItem* btn3 = gtk_tool_button_new(NULL, _2("选择"));
gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(btn3), GTK_STOCK_CONNECT);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), btn3, 2);
db_list = gtk_tree_view_new();
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(db_list), true);
db_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_box_pack_start(GTK_BOX(db_box), toolbar, true, true, 5);
entrydb = gtk_entry_new();
gtk_box_pack_start(GTK_BOX(db_box), entrydb, true, true, 5);
gtk_box_pack_start(GTK_BOX(db_box), db_list, true, true, 5);
db_label = gtk_label_new("");
gtk_box_pack_start(GTK_BOX(db_box), db_label, false, false, 5);
gtk_tree_view_set_grid_lines(GTK_TREE_VIEW(db_list), GTK_TREE_VIEW_GRID_LINES_BOTH);
init_dblist(db_list);
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(db_list));
g_signal_connect(selection, "changed",
G_CALLBACK(on_dbchanged), db_label);
g_signal_connect(btn2, "clicked", G_CALLBACK(db_clicked_delete), selection);
g_signal_connect(btn1, "clicked", G_CALLBACK(db_clicked_new), db_list);
g_signal_connect(btn3, "clicked", G_CALLBACK(db_clicked_choose), NULL);
gtk_widget_set_size_request(window_database, 400, 500);
gtk_container_add(GTK_CONTAINER(window_database), db_box);
gtk_widget_show(window_database);
gtk_widget_show_all(window_database);
}
enum
{
DBLIST_ITEM=0,
DB_COLUMNS
};
void init_dblist(GtkWidget* list)
{
GtkCellRenderer* renderer;
GtkTreeViewColumn* column;
GtkListStore* store;
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes(_2("数据库"), renderer, "text", DBLIST_ITEM, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
store = gtk_list_store_new(DB_COLUMNS, G_TYPE_STRING);
gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
g_object_unref(store);
int t;
char query[50] = "show databases";
t = mysql_real_query(conn_prt, query, strlen(query));
if (t)
{
printf("failed to query:%s\n", mysql_error(conn_prt));
return;
}
res = mysql_store_result(conn_prt);
while (row = mysql_fetch_row(res))
{
for (t = 0; t < mysql_num_fields(res); t++)
{
add_to_dblist(list, _2(row[t]));
}
}
}
void add_to_dblist(GtkWidget* list, const gchar* str)
{
GtkListStore* store;
GtkTreeIter iter;
store = GTK_LIST_STORE(gtk_tree_view_get_model
(GTK_TREE_VIEW(list)));
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, DBLIST_ITEM, str, -1);
}
void on_dbchanged(GtkWidget* widget, gpointer label)
{
GtkTreeIter iter;
GtkTreeModel* model;
if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(widget), &model, &iter))
{
gtk_tree_model_get(model, &iter, DBLIST_ITEM, &dbvalue, -1);
gtk_label_set_text(GTK_LABEL(label), dbvalue);
}
}
void db_clicked_delete(GtkWidget* widget, gpointer select)
{
GtkTreeIter iter;
GtkTreeModel* model;
if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(select), &model, &iter))
{
int t;
char query[50];
sprintf(query, "drop database %s", dbvalue);
t = mysql_real_query(conn_prt, query, strlen(query));
if (t)
{
printf("failed to drop database:%s\n", mysql_error(conn_prt));
return;
}
gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
}
}
void db_clicked_new(GtkWidget* widget, gpointer list)
{
const char* name = gtk_entry_get_text(GTK_ENTRY(entrydb));
int t;
char query[50];
sprintf(query, "create database %s", name);
t = mysql_real_query(conn_prt, query, strlen(query));
if (t)
{
printf("failed to create database:%s\n", mysql_error(conn_prt));
return;
}
add_to_dblist(GTK_WIDGET(list), _2(name));
}
void db_clicked_choose(GtkWidget* widget, gpointer list)
{
gtk_widget_hide(window_database);
int t;
char query[50];
sprintf(query, "use %s", dbvalue);
t = mysql_real_query(conn_prt, query, strlen(query));
if (t)
{
printf("failed to use database:%s\n", mysql_error(conn_prt));
return;
}
create_tb_window();
}
void create_tb_window()
{
GtkWidget* tb_list;
GtkWidget* tb_box;
GtkWidget* tb_label;
GtkTreeSelection* selection;
window_tb = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window_tb), _2("数据库表展示页面"));
g_signal_connect(window_tb, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_window_set_position(GTK_WINDOW(window_tb), GTK_WIN_POS_CENTER_ALWAYS);
gtk_container_set_border_width(GTK_CONTAINER(window_tb), 10);
tb_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
GtkWidget* toolbar = gtk_toolbar_new();
gtk_box_pack_start(GTK_BOX(tb_box), toolbar, true, true, 5);
GtkToolItem* btn0 = gtk_tool_button_new(NULL, _2("返回"));
gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(btn0), GTK_STOCK_CLEAR);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), btn0, 0);
GtkToolItem* btn1 = gtk_tool_button_new(NULL, _2("新建"));
gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(btn1), GTK_STOCK_ADD);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), btn1, 1);
GtkToolItem* btn2 = gtk_tool_button_new(NULL, _2("删除"));
gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(btn2), GTK_STOCK_DELETE);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), btn2, 2);
GtkToolItem* btn3 = gtk_tool_button_new(NULL, _2("查看表数据"));
gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(btn3), GTK_STOCK_CONNECT);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), btn3, 3);
GtkToolItem* btn4 = gtk_tool_button_new(NULL, _2("查看表结构"));
gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(btn4), GTK_STOCK_INFO);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), btn4, 4);
GtkWidget* label1 = gtk_label_new(_2("表名"));
gtk_box_pack_start(GTK_BOX(tb_box), label1, true, true, 5);
tb_name = gtk_entry_new();
gtk_box_pack_start(GTK_BOX(tb_box), tb_name, true, true, 5);
GtkWidget* label2 = gtk_label_new(_2("建表语句"));
gtk_box_pack_start(GTK_BOX(tb_box), label2, true, true, 5);
tb_sql = gtk_entry_new();
gtk_box_pack_start(GTK_BOX(tb_box), tb_sql, true, true, 5);
tb_list = gtk_tree_view_new();
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tb_list), true);
gtk_box_pack_start(GTK_BOX(tb_box), tb_list, true, true, 5);
tb_label = gtk_label_new("");
gtk_box_pack_start(GTK_BOX(tb_box), tb_label, false, false, 5);
gtk_tree_view_set_grid_lines(GTK_TREE_VIEW(tb_list), GTK_TREE_VIEW_GRID_LINES_BOTH);
init_tblist(tb_list);
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tb_list));
g_signal_connect(selection, "changed",
G_CALLBACK(on_tbchanged), tb_label);
g_signal_connect(btn0, "clicked", G_CALLBACK(tb_clicked_back), NULL);
g_signal_connect(btn2, "clicked", G_CALLBACK(tb_clicked_delete), selection);
g_signal_connect(btn4, "clicked", G_CALLBACK(tb_clicked_desc), NULL);
g_signal_connect(btn1, "clicked", G_CALLBACK(tb_clicked_new), tb_list);
g_signal_connect(btn3, "clicked", G_CALLBACK(tb_clicked_choose), NULL);
gtk_widget_set_size_request(window_tb, 470, 100);
gtk_container_add(GTK_CONTAINER(window_tb), tb_box);
gtk_widget_show(window_tb);
gtk_widget_show_all(window_tb);
}
enum
{
TBLIST_ITEM = 0,
TB_COLUMNS
};
void init_tblist(GtkWidget* list)
{
GtkCellRenderer* renderer;
GtkTreeViewColumn* column;
GtkListStore* store;
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes(_2("表"), renderer, "text", TBLIST_ITEM, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
store = gtk_list_store_new(TB_COLUMNS, G_TYPE_STRING);
gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
g_object_unref(store);
int t;
char query[50] = "show tables";
t = mysql_real_query(conn_prt, query, strlen(query));
if (t)
{
printf("failed to query:%s\n", mysql_error(conn_prt));
return;
}
res = mysql_store_result(conn_prt);
while (row = mysql_fetch_row(res))
{
for (t = 0; t < mysql_num_fields(res); t++)
{
add_to_tblist(list, _2(row[t]));
}
}
}
void add_to_tblist(GtkWidget* list, const gchar* str)
{
GtkListStore* store;
GtkTreeIter iter;
store = GTK_LIST_STORE(gtk_tree_view_get_model
(GTK_TREE_VIEW(list)));
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, TBLIST_ITEM, str, -1);
}
void on_tbchanged(GtkWidget* widget, gpointer label)
{
GtkTreeIter iter;
GtkTreeModel* model;
if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(widget), &model, &iter))
{
gtk_tree_model_get(model, &iter, DBLIST_ITEM, &tbvalue, -1);
gtk_label_set_text(GTK_LABEL(label), tbvalue);
}
}
void tb_clicked_back(GtkWidget* widget, gpointer data)
{
gtk_widget_hide(window_tb);
gtk_widget_show(window_database);
gtk_widget_show(window_database);
}
void tb_clicked_delete(GtkWidget* widget, gpointer select)
{
GtkTreeIter iter;
GtkTreeModel* model;
if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(select), &model, &iter))
{
int t;
char query[50];
sprintf(query, "drop table %s", tbvalue);
t = mysql_real_query(conn_prt, query, strlen(query));
if (t)
{
printf("failed to drop database:%s\n", mysql_error(conn_prt));
return;
}
gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
}
}
void tb_clicked_new(GtkWidget* widget, gpointer list)
{
int t;
char query[50];
sprintf(query, "%s", gtk_entry_get_text(GTK_ENTRY(tb_sql)));
t = mysql_real_query(conn_prt, query, strlen(query));
if (t)
{
printf("failed to create table:%s\n", mysql_error(conn_prt));
return;
}
add_to_tblist(GTK_WIDGET(list), gtk_entry_get_text(GTK_ENTRY(tb_name)));
}
void tb_clicked_desc(GtkWidget* widget, gpointer data)
{
gtk_widget_hide(window_tb);
create_tbdesc_window();
}
void create_tbdesc_window()
{
GtkWidget* tbdesc_list;
GtkWidget* tbdesc_box;
GtkTreeSelection* selection;
window_tbdesc = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window_tbdesc), _2("数据库表结构展示页面"));
g_signal_connect(window_tbdesc, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_window_set_position(GTK_WINDOW(window_tbdesc), GTK_WIN_POS_CENTER_ALWAYS);
gtk_container_set_border_width(GTK_CONTAINER(window_tbdesc), 10);
tbdesc_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
GtkWidget* toolbar = gtk_toolbar_new();
gtk_box_pack_start(GTK_BOX(tbdesc_box), toolbar, true, true, 5);
GtkToolItem* btn0 = gtk_tool_button_new(NULL, _2("返回"));
gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(btn0), GTK_STOCK_CLEAR);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), btn0, 0);
tbdesc_list = gtk_tree_view_new();
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tbdesc_list), true);
gtk_box_pack_start(GTK_BOX(tbdesc_box), tbdesc_list, true, true, 5);
gtk_tree_view_set_grid_lines(GTK_TREE_VIEW(tbdesc_list), GTK_TREE_VIEW_GRID_LINES_BOTH);
init_tbdesclist(tbdesc_list);
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tbdesc_list));
g_signal_connect(btn0, "clicked", G_CALLBACK(tbdesc_clicked_back), NULL);
gtk_widget_set_size_request(window_tbdesc, 400, 100);
gtk_container_add(GTK_CONTAINER(window_tbdesc), tbdesc_box);
gtk_widget_show(window_tbdesc);
gtk_widget_show_all(window_tbdesc);
}
enum
{
TBDESC_FIELD = 0,
TBDESC_TYPE,
TBDESC_NULL,
TBDESC_KEY,
TBDESC_DEFAULT,
TBDESC_EXTRA,
TBDESC_COLUMNS
};
void init_tbdesclist(GtkWidget* list)
{
GtkCellRenderer* renderer;
GtkTreeViewColumn* column;
GtkListStore* store;
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes(_2("Field"), renderer, "text", TBDESC_FIELD, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes(_2("Type"), renderer, "text", TBDESC_TYPE, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes(_2("Null"), renderer, "text", TBDESC_NULL, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes(_2("Key"), renderer, "text", TBDESC_KEY, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes(_2("Default"), renderer, "text", TBDESC_DEFAULT, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes(_2("Extra"), renderer, "text", TBDESC_EXTRA, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
store = gtk_list_store_new(TBDESC_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
g_object_unref(store);
int t;
char query[50];
sprintf(query, "desc %s", tbvalue);
t = mysql_real_query(conn_prt, query, strlen(query));
if (t)
{
printf("failed to query:%s\n", mysql_error(conn_prt));
return;
}
res = mysql_store_result(conn_prt);
while (row = mysql_fetch_row(res))
{
add_to_tbdesclist(list, _2(row[0]), row[1], row[2], row[3], row[4], row[5]);
}
}
void add_to_tbdesclist(GtkWidget* list, const gchar* str1, const gchar* str2, const gchar* str3, const gchar* str4, const gchar* str5,const gchar* str6)
{
GtkListStore* store;
GtkTreeIter iter;
store = GTK_LIST_STORE(gtk_tree_view_get_model
(GTK_TREE_VIEW(list)));
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, TBDESC_FIELD, str1, TBDESC_TYPE, str2, TBDESC_NULL, str3, TBDESC_KEY, str4, TBDESC_DEFAULT, str5, TBDESC_EXTRA, str6,-1);
}
void tbdesc_clicked_back(GtkWidget* widget, gpointer data)
{
gtk_widget_hide(window_tbdesc);
gtk_widget_show(window_tb);
gtk_widget_show(window_tb);
}
void tb_clicked_choose(GtkWidget* widget, gpointer data)
{
gtk_widget_hide(window_tb);
create_tbinfo_window();
}
void create_tbinfo_window()
{
GtkWidget* tbinfo_list;
GtkWidget* tbinfo_box;
GtkTreeSelection* selection;
window_tbinfo = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window_tbinfo), _2("数据库表数据展示页面"));
g_signal_connect(window_tbinfo, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_window_set_position(GTK_WINDOW(window_tbinfo), GTK_WIN_POS_CENTER_ALWAYS);
gtk_container_set_border_width(GTK_CONTAINER(window_tbinfo), 10);
tbinfo_box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
GtkWidget* toolbar = gtk_toolbar_new();
gtk_box_pack_start(GTK_BOX(tbinfo_box), toolbar, true, true, 5);
GtkToolItem* btn0 = gtk_tool_button_new(NULL, _2("返回"));
gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(btn0), GTK_STOCK_CLEAR);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), btn0, 0);
GtkToolItem* btn1 = gtk_tool_button_new(NULL, _2("删除"));
gtk_tool_button_set_stock_id(GTK_TOOL_BUTTON(btn1), GTK_STOCK_DELETE);
gtk_toolbar_insert(GTK_TOOLBAR(toolbar), btn1, 1);
tbinfo_list = gtk_tree_view_new();
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tbinfo_list), true);
gtk_box_pack_start(GTK_BOX(tbinfo_box), tbinfo_list, true, true, 5);
gtk_tree_view_set_grid_lines(GTK_TREE_VIEW(tbinfo_list), GTK_TREE_VIEW_GRID_LINES_BOTH);
init_tbinfolist(tbinfo_list);
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tbinfo_list));
g_signal_connect(btn0, "clicked", G_CALLBACK(tbinfo_clicked_back), NULL);
g_signal_connect(btn1, "clicked", G_CALLBACK(tbinfo_clicked_delete), selection);
g_signal_connect(selection, "changed",
G_CALLBACK(on_tbinfochanged), NULL);
gtk_widget_set_size_request(window_tbinfo, 470, 100);
gtk_container_add(GTK_CONTAINER(window_tbinfo), tbinfo_box);
gtk_widget_show(window_tbinfo);
gtk_widget_show_all(window_tbinfo);
}
enum
{
TBINFO_ID = 0,
TBINFO_USERNAME,
TBINFO_PASSWORD,
TBINFO_SEX,
TBINFO_EMAIL,
TBINFO_COLUMNS
};
void init_tbinfolist(GtkWidget* list)
{
GtkCellRenderer* renderer;
GtkTreeViewColumn* column;
GtkListStore* store;
int t;
char query[50];
sprintf(query, "select* from %s", tbvalue);
t = mysql_real_query(conn_prt, query, strlen(query));
if (t)
{
printf("failed to query:%s\n", mysql_error(conn_prt));
return;
}
res = mysql_store_result(conn_prt);
MYSQL_FIELD* my_field;
my_field = mysql_fetch_fields(res);
for (int j = 0; j < 5; ++j)
{
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes(_2(my_field[j].name), renderer, "text", j, NULL);
gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
}
tbinfocolname = my_field[0].name;
store = gtk_list_store_new(TBINFO_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
g_object_unref(store);
while (row = mysql_fetch_row(res))
{
add_to_tbinfolist(list, _2(row[0]), _2(row[1]), _2(row[2]), _2(row[3]), _2(row[4]));
}
}
void add_to_tbinfolist(GtkWidget* list, const gchar* str1, const gchar* str2, const gchar* str3, const gchar* str4, const gchar* str5)
{
GtkListStore* store;
GtkTreeIter iter;
store = GTK_LIST_STORE(gtk_tree_view_get_model
(GTK_TREE_VIEW(list)));
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter, TBINFO_ID, str1, TBINFO_USERNAME, str2, TBINFO_PASSWORD, str3, TBINFO_SEX, str4, TBINFO_EMAIL, str5,-1);
}
void tbinfo_clicked_back(GtkWidget* widget, gpointer data)
{
gtk_widget_hide(window_tbinfo);
gtk_widget_show(window_tb);
gtk_widget_show(window_tb);
}
void tbinfo_clicked_delete(GtkWidget* widget, gpointer select)
{
GtkTreeIter iter;
GtkTreeModel* model;
if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(select), &model, &iter))
{
int t;
char query[50];
sprintf(query, "delete from %s where %s = %s", tbvalue, tbinfocolname, tbinfocolvalue);
t = mysql_real_query(conn_prt, query, strlen(query));
if (t)
{
printf("failed to delete row:%s\n", mysql_error(conn_prt));
return;
}
gtk_list_store_remove(GTK_LIST_STORE(model), &iter);
}
}
void on_tbinfochanged(GtkWidget* widget, gpointer data)
{
GtkTreeIter iter;
GtkTreeModel* model;
if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(widget), &model, &iter))
{
gtk_tree_model_get(model, &iter, 0, &tbinfocolvalue, -1);
}
}
|