<pre name="code" class="cpp">#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <conio.h>
#include <windows.h>
#include <cstdlib>
#include <string>
using namespace std;
extern "C"
{
#include "sqlite3.h"
};
class stds
{
public:
stds() : sql("create table if not exists inof(id int,m_name char(255),results decimal(5,2),unique(id))"), count(0), db(0), stmt(0), erroe(0), rs(0){}
bool deleteinof(int num)
{
char temp[255] = { 0 };
sprintf(temp, "inof\\student%d.db",num);
if (init(temp))
{
memset(temp, 0, 255);
sprintf(temp, "delete from inof");
if (SQLITE_OK == sqlite3_exec(db, temp, 0, 0, &erroe))
{
sqlite3_close(db);
return true;
}
else
{
cout << erroe << endl;
sqlite3_free(erroe);
erroe = 0;
}
}
else
cout << "打开失败"<<endl;
return false;
}
bool ADDstuden(int num)
{
char temp[255] = { 0 };
sprintf(temp, "inof\\student%d.db", num);
if (init(temp))
{
sqlite3_close(db);
return true;
}
return false;
}
bool insert(int num)
{
char temp[255] = { 0 };
sprintf(temp, "inof\\student%d.db", num);
if (init(temp))
{
cout << "请输入学生的编号:(学号为唯一)" << endl;
cin >> st.id;
fflush(stdin);
cout << "请输入姓名:" << endl;
cin.getline(st.name, 128);
cout << "请输入成绩:" << endl;
cin >> st.results;
char temp[255] = { 0 };
sprintf(temp, "insert into inof(id,m_name,results)values(%d,' %s ',%f) ", st.id, st.name, st.results);
if (SQLITE_OK == sqlite3_exec(db, temp, 0, 0, &erroe))
{
cout << "添加学生OK" << endl;
sqlite3_close(db);
return true;
}
else
{
cout << erroe << endl;
sqlite3_free(erroe);
erroe = 0;
sqlite3_close(db);
cout << "添加学生失败或学号重复,请查看一下学生统计" << endl;
return false;
}
sqlite3_close(db);
return true;
}
return false;
}
bool select_shoew(int num)
{
char temp[255] = { 0 };
sprintf(temp, "inof\\student%d.db", num);
if (init(temp))
{
while (1)
{
cout << "【1】查看统计【2】修改【3】删除单个学生【4】查看学生人数总和" << endl;
fflush(stdin);
cin >> rs;
switch (rs)
{
case 1:
memset(temp, 0, 255);
sprintf(temp, "select *from inof");
show_ALL(temp);
break;
case 2:
update_one(temp, 255);
break;
case 3:
delete_one(temp, 255);
break;
case 4:
sprintf(temp, "select *from inof");
cout << "班级人数为:" << AllSUm(temp) << endl;
default:
break;
}
sqlite3_close(db);
return true;
break;
}
}
return false;
}
~stds(){ sqlite3_close(db); }
private:
int AllSUm(const char *temp)
{
if (SQLITE_OK == sqlite3_prepare_v2(db, temp, -1, &stmt,0))
{
if ((count = sqlite3_column_count(stmt)))
{
if (SQLITE_OK == sqlite3_exec(db, temp, 0, 0, &erroe))
{
sqlite3_finalize(stmt);
stmt = 0;
return count;
}
else
{
cout << erroe
C ++ 面向对象 简单写sqlite3 数据库
最新推荐文章于 2024-01-24 13:52:38 发布