登录页面的制作+连接数据库
软工大作业第一天记录 明天对登录页面进行优化加整数据库展示页面
#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;
char select_head[50] = "select * from ";
char desc_head[50] = "desc ";
char insert_head[200] = "insert into ";
char drop_msg_head[50] = "delete from ";
char change_base_head[50] = "use ";
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;
void login_submit(GtkButton* button, gpointer user_data);
void login_reset(GtkButton* button, gpointer user_data);
void create_db_window();
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_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()
{
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_widget_show(window_database);
gtk_widget_show_all(window_database);
}
|