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$