main.cxx
#include "soci.h"
#include "soci-sqlite3.h"
using namespace soci;
#include <iostream>
using namespace std;
int main()
{
session sql;
sql.open(sqlite3, "./stu.db");
/*method1*/
/*rowset<row> rs = (sql.prepare<<"select * from stu");
for(rowset<row>::const_iterator it= rs.begin(); it != rs.end(); it++)
{
row const& r = *it;
cout<<r.get<int>(0)<<''<<r.get<string>(1)<<''<<r.get<int>(2)<<endl;
}*/
/*method2*/
row r;
statement st = (sql.prepare<<"select * from stu", into(r));
st.execute();
while(st.fetch())
{
cout<<r.get<int>(0)<<''<<r.get<string>(1)<<''<<r.get<int>(2)<<endl;
}
return 0;
}
makefile
INCLUDEDIR=-I/usr/local/include/soci -I/usr/local/include/soci/sqlite3 LIBDIR= -L/usr/local/lib
LIB = -lsoci_core -lsoci_sqlite3
all: soci.o g++ soci.o -o soci.exe ${LIBDIR} ${LIB}
soci.o:soci.cxx g++ -c soci.cxx -o soci.o ${INCLUDEDIR}
.PHONY:clean clean: rm -f soci.exe soci.o
关于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 6.记着在CodeBlocks的link 那里添加 libsqlite3.so和libsqlite3.so.0的路径(usr/local/lib)
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$ 6.在CodeBlocks的link 那里添加 libsqlite3.so和libsqlite3.so.0的路径(usr/local/lib)