首页 > 代码库 > c++操作sqllite

c++操作sqllite

项目中需要使用的sqllite,有想过使用内存的结果,好像都不大使用,最接近的算是vector了,但是查询方式不大好,而且数据有好几个字段,所以考虑了数据库


sqllite目前已经到了3了,好快,好像这个数据库也不弱!就先用着吧!

==================================================================


其实挺简单的,主要也就是一个class


main函数里面有对应的测试函数

 

暂时没有发现bug,如果出现,请务必提醒本人!谢啦!

 

详情请看main函数

 

测试项目的链接:

http://download.csdn.net/detail/zengraoli/7457843



sql_lite_helper.h:

#ifndef  __INCLUDE_SQL_LITE_HELPER_H__
#define  __INCLUDE_SQL_LITE_HELPER_H__


#include "sqlite3.h"

#pragma comment(lib, "sqlite3.lib")


class SQLiteHelper
{
public:
	SQLiteHelper() { }
	virtual ~SQLiteHelper() { CloseDB(); }

	// 打开数据库
	int OpenDB(const char *path);
	// 关闭数据库
	int CloseDB();
	// 创建一张表
	int CreateTable(const char *create_table_state);
	// 删除一张表
	int DropTable(const char *table_name);
	// 查询操作
	int Select(const char *select_state);
	// 插入操作
	int Insert(const char *insert_state);
	// 删除操作
	int Delete(const char *delete_state);
	// 更新操作
	int Update(const char *update_state);
	
private:
	sqlite3 *sqlite_db_;// 数据库的指针
	char* err_msg_;		// 错误信息
	bool is_close_;		// 关闭数据的标识

	// 主要用在selece操作中的显示数据元素
	static int CallBackFunc(void *NotUsed, int argc, char **argv, char **azColName);
	// 执行sql语句
	int SqlStateExec(const char *sql_state);
};


#endif


sql_lite_helper.cpp:

#include "iostream"
using namespace std;

#include "sql_lite_helper.h"


int SQLiteHelper::OpenDB(const char *path)
{
	int res = sqlite3_open(path, &sqlite_db_);
	if(res)
	{
		cout << "can't open database: " << sqlite3_errmsg(sqlite_db_);
		sqlite3_close(sqlite_db_);
		return -1;
	}

	is_close_ = false;

	return 0;
}


int SQLiteHelper::CloseDB()
{
	if (!is_close_)
	{
		int res = sqlite3_close(sqlite_db_);
		if(res)
		{
			cout << "can't close database: " << sqlite3_errmsg(sqlite_db_);
			return -1;
		}
	}

	return 0;
}


int SQLiteHelper::CreateTable(const char *table_name_and_field)
{
	string create_table_state = "create table ";
	create_table_state += table_name_and_field;
	create_table_state += ";";
	int res = SqlStateExec(create_table_state.c_str());
	if (res != SQLITE_OK)
	{ 
		cout << "create table failed." << err_msg_ << endl;
		return -1;
	}
	else
	{
		cout << "create table successed." << endl;
	}

	return 0;
}


int SQLiteHelper::DropTable(const char *table_name)
{
	string sql_state = "drop table ";
	sql_state += table_name;
	sql_state += ";";
	int res = SqlStateExec(sql_state.c_str());
	if (res != SQLITE_OK)
	{ 
		cout << "drop table failed." << err_msg_ << endl;
		return -1;
	}
	else
	{
		cout << "drop table successed." << endl;
	}

	return 0;
}


int SQLiteHelper::Select(const char *select_state)
{
	int res = SqlStateExec(select_state);
	if (res != SQLITE_OK)
	{
		cout << "select operate failed." << err_msg_ << endl;
		return -1;
	}
	else
	{
		cout << "select operate successed." << endl;
	}

	return 0;
}


int SQLiteHelper::Insert(const char *insert_state)
{
	int res = sqlite3_exec(sqlite_db_, "begin transaction;", CallBackFunc, 0, &err_msg_);
	res = SqlStateExec(insert_state);
	if (res != SQLITE_OK) 
	{
		cout << "insert operate failed." << err_msg_ << endl;
		return -1;
	} 

	res = sqlite3_exec(sqlite_db_, "commit transaction;", 0, 0, &err_msg_);

	cout << "insert operate successed." << endl;

	return 0;
}


int SQLiteHelper::Delete(const char *delete_state)
{
	int res = SqlStateExec(delete_state);
	if (res != SQLITE_OK)
	{
		cout << "delete operate failed." << err_msg_ << endl;
		return -1;
	}
	else
	{
		cout << "delete operate successed." << endl;
	}

	return 0;
}


int SQLiteHelper::Update(const char *update_state)
{
	int res = SqlStateExec(update_state);
	if (res != SQLITE_OK)
	{
		cout << "update operate failed." << err_msg_ << endl;
		return -1;
	}
	else
	{
		cout << "update operate successed." << endl;
	}

	return 0;
}


int SQLiteHelper::CallBackFunc(void *not_used, int element_count, char **element, char **col_name)
{
	for(int index = 0 ; index < element_count ; index++)
	{
		cout << col_name[index] << " = " << (element[index] ? element[index] : "NULL") << ", ";
	}

	cout << "\n";

	return 0; 
}


int SQLiteHelper::SqlStateExec(const char *sql_state)
{
	return sqlite3_exec(sqlite_db_, sql_state, CallBackFunc, 0, &err_msg_);
}


main:

// test_use_sqlite.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "iostream"
#include "sstream"
using namespace std; 

#include "sql_lite_helper.h"


SQLiteHelper sql_lite_helper;


// Test:创建一个数据库表
int TestCreateTable()
{
	return sql_lite_helper.CreateTable("test_table (id int, name varchar, age int)");
}


// Test:测试插入数据
int TestInsert()
{
	for (int i= 1; i < 10; ++i)
	{
		std::stringstream str_sql;
		str_sql << "insert into test_table values(";
		str_sql << i << ","<< (i + 10) << "," << 23 << ");";
		std::string str = str_sql.str();
		sql_lite_helper.Insert(str.c_str());
	}

	return 0;
}


// Test:测试删除某个元素
int TestDelete()
{
	string str_sql= "delete from test_table where id=4;";

	return sql_lite_helper.Delete(str_sql.c_str());
}


// Test:测试更新某个元素
int TestUpdate()
{
	string str_sql= "update test_table set name='SQLite3' where name='17';";

	return sql_lite_helper.Update(str_sql.c_str());
}


// Test:测试查询
int TestSelect()
{
	string str_sql= "select * from test_table;";

	return sql_lite_helper.Select(str_sql.c_str());
}


// Test:测试删除表
int TestDropTable()
{
	return sql_lite_helper.DropTable("test_table");
}


int main()
{
	int res = sql_lite_helper.OpenDB("./Test.db3");

	res = TestCreateTable();
	if (res != 0)
	{
		return 0;
	}

	res = TestInsert();
	if (res != 0)
	{
		return 0;
	}

	TestSelect();

	res = TestDelete();
	if (res != 0)
	{
		return 0;
	}

	res = TestUpdate();
	if (res != 0)
	{
		return 0;
	}

	TestSelect();

	TestDropTable();

	TestSelect();


	return 0;
}


显示结果: