学生信息管理系统的构建2--Sqlite3的使用

来源:互联网 发布:python 判断类型相同 编辑:程序博客网 时间:2024/05/20 10:15

1.sqlit3数据库特点简介
(1)关系型数据库(数据存储在多张二维数据表中)
(2)单用户数据库
(3)所有的嵌入式设备中
(4)c语言编写,运行速度快
(5)支持所有的sql92语句
2.常用命令
字段 中的 主键 是一种约束,1必填,2不允许重复
insert into stutable values (1,’A’,90,90);
insert into stutable values (2,’B’,10,10);
select * from stutable;
select * from stutable where stuId = 1;
select stuId,stuName,(chineseScore+englishScore) as score from stuTable order by score desc;
select * from stutable order by stuId;
delete from stutable where stuId = 1;
update stutable set stuname = ‘bb’,chinesescore = 0 where stuid = 2;
3.创建流程
1空白处右键-》数据库-》添加数据库,在文件中输入 路径+文件名【比如C:\Users\drilistbox\Desktop\test2.db】,软件会自动在Name中显示 文件名【test2.db】,点击确定。
这里写图片描述
双击test2, 右键Tables-》新建表 ,将 Table name设为stuTable
这里写图片描述
在单击 Add column,在字段名下写stuId,在数据类型下选INT,并 勾选 主键。相应的添加如下列表
这里写图片描述

======================================
数据库的引用有两种方式,通过静态或动态链接库加载,本节采用静态链接库加载,则需要调用SQLITE3.lib文件,程序中还需包含或能够访问到CppSQLite3.h、CppSQLite3.cpp、sqlite3.h文件,上一节的程序中只需要修改FileDao.h及FileDao.cpp文件即可,本例中将其换成MyDataBaseDAO.h及MyDataBaseDAO.cpp,

//MyDataBaseDAO.h#pragma once#include"AbstractDAO.h"class MyDataBaseDAO:public CAbstractDAO{public:    MyDataBaseDAO(void);    ~MyDataBaseDAO(void);        void Save(list<CStudentDTO> &list);    void Read(list<CStudentDTO> &list);};
//MyDataBaseDAO.cpp#include "MyDataBaseDAO.h"#include "CppSQLite3.h"#include <iostream>#include  <string>#include  <sstream>  #include <tchar.h>#include<Windows.h>using namespace std;MyDataBaseDAO::MyDataBaseDAO(void){}MyDataBaseDAO::~MyDataBaseDAO(void){}void MyDataBaseDAO::Save(list<CStudentDTO> &mylist){       try{        CppSQLite3DB db;        cout << "SQLite Version: " << db.SQLiteVersion() << endl;        /*读取当前生成的exe文件所在路径的test.db文件*/        TCHAR path[255] = {0};        GetModuleFileName(NULL,path,255);        (_tcsrchr(path,'\\'))[1]='\0';        string sd = path;        sd = sd + _T("test.db");        string ssss(sd.begin(),sd.end());        cout<<"SQLiteVersion:"<<db.SQLiteVersion()<<endl;        char *pppp = (char*)sd.c_str();        db.open(ssss.c_str());        /*读取指定路径中的test.db文件*/        //db.open("C:\\Users\\drilistbox\\Desktop\\test.db");        string cmd;        int nRows;        stringstream strstream;        strstream << "delete from stutable;";        cmd = strstream.str();        list<CStudentDTO>::iterator p;        for(p = mylist.begin();p!=mylist.end();p++)          {               strstream << "insert into stutable values (" << p->GetID() << ",'"<< p->GetName() <<"',"<< p->GetChineseScore() <<","<<p->GetEnglishScore()<< ");";            cmd = strstream.str();        }        nRows = db.execDML(cmd.c_str());//nRows:表示影响的行数        cout << nRows << " rows updated" << endl;        db.close();    }    catch (CppSQLite3Exception& e)    {        cerr << e.errorCode() << ":" << e.errorMessage() << endl;    }}void MyDataBaseDAO::Read(list<CStudentDTO> &mylist){    try{        CppSQLite3DB db;        TCHAR path[255] = {0};        GetModuleFileName(NULL,path,255);        (_tcsrchr(path,'\\'))[1]='\0';        string sd = path;        sd = sd + _T("test.db");        string ssss(sd.begin(),sd.end());        //cout<<"SQLiteVersion:"<<db.SQLiteVersion()<<endl;        char *pppp = (char*)sd.c_str();        db.open(ssss.c_str());        //db.open("C:\\Users\\drilistbox\\Desktop\\test.db");        CppSQLite3Buffer bufSQL;        bufSQL.format("select * from stutable;");        CppSQLite3Query query = db.execQuery(bufSQL);        while (!query.eof())        {    mylist.push_back(CStudentDTO(query.getIntField(0),query.getStringField(1),query.getFloatField(2),query.getFloatField(3)));            query.nextRow();        }        query.finalize(); //清除query的储存空间,否则内存泄漏        db.close();    }    catch (CppSQLite3Exception& e)    {        cerr << e.errorCode() << ":" << e.errorMessage() << endl;    }}
原创粉丝点击