IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> GTK3+Mysql C语言版图形数据库管理系统(五) -> 正文阅读

[大数据]GTK3+Mysql C语言版图形数据库管理系统(五)

数据库表数据展示页面

界面图:
数据库表数据展示页面
插入数据功能我还没有思路,不太好整,大作业下个星期就该交了,就先搞到这吧!以后有时间再回来研究。
整个系统完整代码

#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句柄
MYSQL_RES* res;
MYSQL_ROW row;


/*初始化mysql句柄*/
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));
}
//typedef enum {
//    GTK_TREE_VIEW_GRID_LINES_NONE,
//    GTK_TREE_VIEW_GRID_LINES_HORIZONTAL,
//    GTK_TREE_VIEW_GRID_LINES_VERTICAL,
//    GTK_TREE_VIEW_GRID_LINES_BOTH
//} GtkTreeViewGridLines;

// 全局变量
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; // 创建表的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_widget_set_size_request(window_login, 800, 500);*/
    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));
    // printf("%s", username);
    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"); // 设置编码格式为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); // 将列表装入box容器中
    db_label = gtk_label_new("");
    gtk_box_pack_start(GTK_BOX(db_box), db_label, false, false, 5); // 将文本标签装入box容器
    gtk_tree_view_set_grid_lines(GTK_TREE_VIEW(db_list), GTK_TREE_VIEW_GRID_LINES_BOTH);
    init_dblist(db_list);
    //add_to_dblist(db_list, _2("河南大学"));
    //add_to_dblist(db_list, _2("龙亭区"));
    //add_to_dblist(db_list, _2("开封市"));

    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)
{
    // 要想视图显示出数据,必须建立GtkCellRender与GtkTreeViewColumn
    GtkCellRenderer* renderer;
    GtkTreeViewColumn* column;
    GtkListStore* store;

    // 建立一个GtkCellRenderer
    renderer = gtk_cell_renderer_text_new();
    //建立一个带标题的列 并且将renderer放入其中使其能显示内容
    column = gtk_tree_view_column_new_with_attributes(_2("数据库"), renderer, "text", DBLIST_ITEM, NULL);
    //将列加入gtk_tree_view
    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++)
        {
            // printf("%s\t", row[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;
    
    //获得treeview中选中的一行的GtkTreeIter
    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;

    //获得treeview中选中的一行的GtkTreeIter
    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_widget_set_size_request(toolbar, 100, 50);*/
    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); // 将文本标签装入box容器
    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); // 将文本标签装入box容器
    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); // 将列表装入box容器
    tb_label = gtk_label_new("");
    gtk_box_pack_start(GTK_BOX(tb_box), tb_label, false, false, 5); // 将文本标签装入box容器
    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)
{
    // 要想视图显示出数据,必须建立GtkCellRender与GtkTreeViewColumn
    GtkCellRenderer* renderer;
    GtkTreeViewColumn* column;
    GtkListStore* store;

    // 建立一个GtkCellRenderer
    renderer = gtk_cell_renderer_text_new();
    //建立一个带标题的列 并且将renderer放入其中使其能显示内容
    column = gtk_tree_view_column_new_with_attributes(_2("表"), renderer, "text", TBLIST_ITEM, NULL);
    //将列加入gtk_tree_view
    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++)
        {
            // printf("%s\t", row[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;

    //获得treeview中选中的一行的GtkTreeIter
    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;

    //获得treeview中选中的一行的GtkTreeIter
    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_widget_set_size_request(toolbar, 100, 50);*/
    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); // 将列表装入box容器
   
    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)
{
    // 要想视图显示出数据,必须建立GtkCellRender与GtkTreeViewColumn
    GtkCellRenderer* renderer;
    GtkTreeViewColumn* column;
    GtkListStore* store;

    // 建立一个GtkCellRenderer
    renderer = gtk_cell_renderer_text_new();
    //建立一个带标题的列 并且将renderer放入其中使其能显示内容
    column = gtk_tree_view_column_new_with_attributes(_2("Field"), renderer, "text", TBDESC_FIELD, NULL);
    //将列加入gtk_tree_view
    gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);


    // 建立一个GtkCellRenderer
    renderer = gtk_cell_renderer_text_new();
    //建立一个带标题的列 并且将renderer放入其中使其能显示内容
    column = gtk_tree_view_column_new_with_attributes(_2("Type"), renderer, "text", TBDESC_TYPE, NULL);
    //将列加入gtk_tree_view
    gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);

    // 建立一个GtkCellRenderer
    renderer = gtk_cell_renderer_text_new();
    //建立一个带标题的列 并且将renderer放入其中使其能显示内容
    column = gtk_tree_view_column_new_with_attributes(_2("Null"), renderer, "text", TBDESC_NULL, NULL);
    //将列加入gtk_tree_view
    gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);

    // 建立一个GtkCellRenderer
    renderer = gtk_cell_renderer_text_new();
    //建立一个带标题的列 并且将renderer放入其中使其能显示内容
    column = gtk_tree_view_column_new_with_attributes(_2("Key"), renderer, "text", TBDESC_KEY, NULL);
    //将列加入gtk_tree_view
    gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);

    // 建立一个GtkCellRenderer
    renderer = gtk_cell_renderer_text_new();
    //建立一个带标题的列 并且将renderer放入其中使其能显示内容
    column = gtk_tree_view_column_new_with_attributes(_2("Default"), renderer, "text", TBDESC_DEFAULT, NULL);
    //将列加入gtk_tree_view
    gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);

    // 建立一个GtkCellRenderer
    renderer = gtk_cell_renderer_text_new();
    //建立一个带标题的列 并且将renderer放入其中使其能显示内容
    column = gtk_tree_view_column_new_with_attributes(_2("Extra"), renderer, "text", TBDESC_EXTRA, NULL);
    //将列加入gtk_tree_view
    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_widget_set_size_request(toolbar, 100, 50);*/
    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); // 将列表装入box容器

    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)
{
    // 要想视图显示出数据,必须建立GtkCellRender与GtkTreeViewColumn
    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)
	{
		// printf("%s\t", my_field[j].name);
        // 建立一个GtkCellRenderer
        renderer = gtk_cell_renderer_text_new();
        //建立一个带标题的列 并且将renderer放入其中使其能显示内容
        column = gtk_tree_view_column_new_with_attributes(_2(my_field[j].name), renderer, "text", j, NULL);
        //将列加入gtk_tree_view
        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;

    //获得treeview中选中的一行的GtkTreeIter
    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;

    //获得treeview中选中的一行的GtkTreeIter
    if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(widget), &model, &iter))
    {
        gtk_tree_model_get(model, &iter, 0, &tbinfocolvalue, -1);
    }
}
  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2021-11-14 21:47:10  更:2021-11-14 21:47:47 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 6:08:04-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码