C ++ 面向对象 简单写sqlite3 数据库

<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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值