首页 > 数据库 > mysql教程 > Ubuntu下wxWidgets学生公寓管理编程,sqlite3的用法(mysql数据_MySQL

Ubuntu下wxWidgets学生公寓管理编程,sqlite3的用法(mysql数据_MySQL

WBOY
发布: 2016-06-01 13:13:00
原创
1239 人浏览过

Ubuntu

以下是学生公寓信息管理的增加和删除,仅供参考。。
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];
    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());
    if(strcmp("", id) == 0)
    {
       wxLogMessage("the stu's id can not be null");
       return;
    }
     if(strcmp("", name) == 0)
    {
       wxLogMessage("the stu's name can not be null");
       return;
    }
    char st[500];
    sprintf(st, "insert into stu values('%s', '%s', '%s', '%s', '%s');",
            id, name, dorid, phone, qq);
    sqlite3_exec(db,st,NULL,NULL,&errmsg );
    wxLogMessage(wxString(errmsg));
    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) == 1)
    {
       wxLogMessage("the stu's id deleted can not be null");
       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!!");
        return;
    }

}

sqlite3建立数据库


1.先在线安装sqlite3,试用一下命令安装
ubuntu@ubuntu-virtual-machine:~$ sudo apt-get install sqlite3
2.安装成功后测试会出现
ubuntu@ubuntu-virtual-machine:~$ sqlite3
SQLite version 3.7.9 2011-11-01 00:52:41
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .exit
3.查找sqlite3的路径
ubuntu@ubuntu-virtual-machine:~$ which sqlite3
/usr/bin/sqlite3
ubuntu@ubuntu-virtual-machine:~$ ls
core              OSlab1    Public                           Videos
Desktop           OSlab2    Qt                               vmwaretools
Documents         OSlab3    sqlite-amalgamation-3080403      wx
Downloads         OSlab4    sqlite-amalgamation-3080403.zip  wx3.0
examples.desktop  OSlab5    StuMana                          wxlab
Music             Pictures  Templates
4.具体sqlite3操作的实例
ubuntu@ubuntu-virtual-machine:~$ mkdir mydb
ubuntu@ubuntu-virtual-machine:~$ cd mydb
ubuntu@ubuntu-virtual-machine:~/mydb$ ls
ubuntu@ubuntu-virtual-machine:~/mydb$ sqlite3 stu.db
SQLite version 3.7.9 2011-11-01 00:52:41
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table stu(sno int primary key, sname text not null, sage int);
sqlite> insert into stu(1, 'Lisi', 20);
Error: near "1": syntax error
sqlite> insert into stu values(1, 'lisi', 20);
sqlite> insert into stu values(2, 'zhangsan', 18);
sqlite> select * from stu;
1|lisi|20
2|zhangsan|18
sqlite> .exit
ubuntu@ubuntu-virtual-machine:~/mydb$ ls
stu.db
5.具体sqlite3操作的实例
ubuntu@ubuntu-virtual-machine:~$ cd StuMana
ubuntu@ubuntu-virtual-machine:~/StuMana$ sqlite3 stuinfo.db
SQLite version 3.7.9 2011-11-01 00:52:41
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table stuinfo (id char(10),name char(15) not null,dormitoryid char(20) not null,phone char(11),qq char(13));
sqlite> insert into stuinfo values('1115115247','LiYa','15#501','15530061772','614100932');
sqlite> insert into stuinfo values('1115115276','ZhangAihua','15#424','18330000036','627100056');
sqlite> select * from stuinfo;
1115115247|LiYa|15#501|15500761772|614100932
1115115276|ZhangAihua|15#424|18330000036|627100056
sqlite> .exit
ubuntu@ubuntu-virtual-machine:~/StuMana$ ls
bin           stuinfo.db      StuMana.depend   WxWizFrame.fbp
GUIFrame.cpp  StuManaApp.cpp  StuMana.layout   WxWizFrame.fbp.bak
GUIFrame.h    StuManaApp.h    StuManaMain.cpp
obj           StuMana.cbp     StuManaMain.h
ubuntu@ubuntu-virtual-machine:~/StuMana$ 

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板