Ubuntu下Codeblocks+wxWidgets编程,学生公寓管理系统,基于窗体(使用wxFormbuilder拉取控件)。C++,sqlite3
/***************************************************************
* 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 not be 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 not be 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,modifydorid,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 be 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();
}