Ubuntu under Codeblocks+wxwidgets programming, student apartment management system, based on forms (using Wxformbuilder pull controls). C++,sqlite3

Source: Internet
Author: User
Tags sprintf strcmp wxwidgets

/***************************************************************
* 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, 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 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 ();
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.