main.cxx
#include <sqlite3.h> #include <cstdlib> #include <iostream> #include <iomanip> using namespace std; int main() { sqlite3* file; int flag; flag = sqlite3_open("./stu.db", &file);
if(SQLITE_OK != flag) { cout<<"Database connect failed!"<<endl; exit(-1); } cout<<"Database connect successes!"<<endl;
char** r; int row, col; char* errmsg; string sql = "select * from stu"; flag = sqlite3_get_table(file, sql.c_str(), &r, &row, &col, &errmsg); if(SQLITE_OK == flag) { cout<<"Statement executes sucesses!"<<endl; int i, j;
for(i = 0; i <= row; i++) { for(j = 0; j < col; j++) { cout<<r[i*col+j]<<' '; } cout<<endl; }
} sqlite3_close(file); return 0; }
makefile
all: main.o g++ main.o -o t.exe -lsqlite3 main.o: main.cxx g++ -c main.cxx -o main.o
.PHONY:clean
clean: rm main.o t.exe
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)