/***************************************************************
* Name:StuManaMain.cpp
* Purpose:code for application Frame
* Author:zhangaihua (62********* @qq. com)
* created:2014-05-20
* Copyright:zhangaihua ()
* License:
**************************************************************/
#ifdef Wx_precomp
#include "wx_pch.h"
#endif
#ifdef __borlandc__
#pragma hdrstop
#endif//__borlandc__
#include "StuManaMain.h"
#include <sqlite3.h>
//helper Functions
enum Wxbuildinfoformat {
short_f, long_f};
wxstring wxbuildinfo (wxbuildinfoformat format)
{
wxstring Wxbuild (wxversion_string);
if (format = = Long_f)
{
#if defined (__wxmsw__)
wxbuild << _t ("-windows");
#elif defined (__wxmac__)
wxbuild << _t ("-mac");
#elif defined (__unix__)
wxbuild << _t ("-linux");
#endif
#if Wxuse_unicode
wxbuild << _t ("-unicode build");
#else
wxbuild << _t ("-ansi build");
#endif//Wxuse_unicode
}
return wxbuild;
}
stumanaframe::stumanaframe (wxframe *frame)
: Guiframe (frame)
{
#if Wxuse_statusbar
Statusbar->setstatustext (_ ("Hello code::blocks user!"), 0);
Statusbar->setstatustext (Wxbuildinfo (Short_f), 1);
#endif
}
Stumanaframe::~stumanaframe ()
{
}
void Stumanaframe::onclose (wxcloseevent &event)
{
Destroy ();
}
void Stumanaframe::onquit (wxcommandevent &event)
{
Destroy ();
}
void Stumanaframe::onabout (wxcommandevent &event)
{
wxstring msg = Wxbuildinfo (long_f);
//Wxmessagebox (MSG, _ ("Welcome to ...");
}
void Stumanaframe::onadd (wxcommandevent &event)
{
//add student ' s dormitory infomation
sqlite3 *db=null;
int flag;
Char *errmsg;
flag = Sqlite3_open ("./stuinfo.db", &db);
if (SQLITE_OK! = flag)
{
wxlogmessage ("Database connect failed!");
exit ( -1);
}
Char id[20], name[20], dorid[20], phone[20], qq[20],sex[10];
strcpy (ID, M_id->getvalue (). MB_STR ());
strcpy (name, M_name->getvalue (). MB_STR ());
strcpy (Dorid, M_dormitoryid->getvalue (). MB_STR ());
strcpy (Phone, M_phone->getvalue (). MB_STR ());
strcpy (QQ, M_qq->getvalue (). MB_STR ());
strcpy (Sex, M_sex->getvalue (). MB_STR ());
if (strcmp ("", id) = = 1)
{
wxlogmessage ("The Stu ' s ID can is not null");
return;
}
if (strcmp ("", name) = = 1)
{
wxlogmessage ("The Stu ' s name can not be null");
return;
}
Char st[500];
sprintf (St, "insert into Stu values ('%s ', '%s ', '%s ', '%s ', '%s ', '%s ');",
ID, name, dorid, phone, qq,sex);
if (sqlite3_exec (db,st,null,null,&errmsg)! = SQLITE_OK)
{
wxlogmessage ("Error");
wxlogmessage (errmsg);
return;
}
Else
{
wxlogmessage ("Add success!!");
wxstring str= "";
M_id->setvalue (str);
M_name->setvalue (str);
M_dormitoryid->setvalue (str);
M_phone->setvalue (str);
M_qq->setvalue (str);
M_sex->setvalue (str);
return;
}
sqlite3_close (db);
}
void Stumanaframe::ondelete (wxcommandevent &event)
{
//delete student ' s dormitory infomation
sqlite3 *db=null;
int flag;
Char *errmsg;
Char stuid[20];
flag = Sqlite3_open ("./stuinfo.db", &db);
if (SQLITE_OK! = flag)
{
wxlogmessage ("Database connect failed!");
exit ( -1);
}
strcpy (Stuid, M_deleteid->getvalue (). MB_STR ());
if (strcmp ("", stuid) = = 0)
{
wxlogmessage ("The Stu ' s ID deleted can not be null");
return;
}
Char sql1[500];
sprintf (SQL1, "select * from stu where id = '%s ';", stuid);
if (sqlite3_exec (db,sql1,null,null,&errmsg)!=SQLITE_OK)
{
wxlogmessage ("error! The student not exists!\n ");
return;
}
Char *sql=sqlite3_mprintf ("delete from Stu where id = '%s ';", stuid);
if (sqlite3_exec (db,sql,null,null,&errmsg)! = SQLITE_OK)
{
wxlogmessage ("Error");
wxlogmessage (errmsg);
return;
}
Else
{
wxlogmessage ("delete success!!");
wxstring str= "";
M_deleteid->setvalue (str);
return;
}
sqlite3_close (db);
}
void Stumanaframe::onmodify (wxcommandevent &event)
{
//modify student ' s dormitory infomation
sqlite3 *db=null;
int flag;
Char *errmsg;
flag = Sqlite3_open ("./stuinfo.db", &db);
if (SQLITE_OK! = flag)
{
wxlogmessage ("Database connect failed!");
exit ( -1);
}
Char modifyid[20], modifyname[20], modifydorid[20], modifyphone[20], modifyqq[20],modifysex[10];
strcpy (Modifyid, M_modifyid->getvalue (). MB_STR ());
strcpy (Modifyname, M_modifyname->getvalue (). MB_STR ());
strcpy (Modifydorid, M_modifydorid->getvalue (). MB_STR ());
strcpy (Modifyphone, M_modifyphone->getvalue (). MB_STR ());
strcpy (MODIFYQQ, M_modifyqq->getvalue (). MB_STR ());
strcpy (Modifysex, M_modifysex->getvalue (). MB_STR ());
if (strcmp ("", modifyid) = = 0)
{
wxlogmessage ("The Stu ' s ID can is not null");
return;
}
Char st[500];
Char *sql=sqlite3_mprintf ("SELECT count (*) from Stu where id= '%s ';", Modifyid);
sqlite3_stmt *pstmt;
sqlite3_prepare (DB, SQL, strlen (SQL), &pstmt, NULL);
Sqlite3_step (pstmt);
int Count=sqlite3_column_int (pstmt,0);
sqlite3_finalize (pstmt);
if (count<0)
{
Wxlogmessage ("The Student Not exist.\n");
wxlogmessage (errmsg);
return;
}
sprintf (St, "Update stu set Name= '%s ', dormitoryid= '%s ', phone= '%s ', qq= '%s ', sex= '%s ' where id= '%s ';", Modifyname,mo Difydorid,modifyphone,modifyqq,modifysex,modifyid);
if (sqlite3_exec (db,st,null,null,&errmsg)! = SQLITE_OK)
{
wxlogmessage ("Error");
wxlogmessage (errmsg);
return;
}
Else
{
wxlogmessage ("Modify success!");
wxstring str= "";
M_modifyid->setvalue (str);
M_modifyname->setvalue (str);
M_modifydorid->setvalue (str);
M_modifyphone->setvalue (str);
M_modifyqq->setvalue (str);
M_modifysex->setvalue (str);
return;
}
sqlite3_close (db);
}
void Stumanaframe::onsearch (wxcommandevent &event)
{
//search student ' s dormitory infomation
sqlite3 *db=null;
int flag;
Char *errmsg;
Char **dbresult;
int Nrow,ncolumn;//search Jilushu and Ziduanshu
int i,j,index;
flag = Sqlite3_open ("./stuinfo.db", &db);
if (SQLITE_OK! = flag)
{
wxlogmessage ("Database connect failed!");
exit ( -1);
}
Char searchid[20],searchname[20];
strcpy (Searchid, M_searchid->getvalue (). MB_STR ());
strcpy (Searchname, M_searchname->getvalue (). MB_STR ());
if (strcmp ("", searchid) = = 0 | | strcmp ("", searchid) = = 0)
{
wxlogmessage ("The Stu's search ID and name can not is null at the same time!");
return;
}
Char *sql=sqlite3_mprintf ("select * from stu where id = '%s ' or name= '%s ';", searchid,searchname);
wxstring str1;
if (sqlite3_get_table (db,sql,&dbresult,&nrow,&ncolumn,&errmsg)! = SQLITE_OK)
{
wxlogmessage ("Error");
wxlogmessage (errmsg);
return;
}
Else
{
Index=ncolumn;
For (i=0;i<nrow;i++)
{
For (j=0;j<ncolumn;j++)
{
str1=str1+ "\ n" +dbresult[j]+ ":" +dbresult[index];
++index;
}
wxputs ("\ n");
}
wxlogmessage ("The search result is:\n%s.\n", str1);
sqlite3_free_table (dbresult);
wxstring str= "";
M_searchid->setvalue (str);
M_searchname->setvalue (str);
return;
}
sqlite3_close (db);
}
void Stumanaframe::onexit (wxcommandevent &event)
{
//exit the student system
Destroy ();
}