Using SQLite in C++ with Code::blocks
来源:互联网 发布:燕十八 mysql 百度云 编辑:程序博客网 时间:2024/04/30 10:03
What you will need:
-Basic C++ and SQL knowledge
-SQLite (Download)
Introduction
SQLite is an embedded SQL database engine. Unlike most other SQL databases, SQLite does not have a separate server process, but instead reads and writes directly to ordinary disk files. This makes SQLite an easy and good solution to store data for your application.
Opening a database
The first thing you need to do, is open a database. If the database does not exist yet, it will be created.
The first argument is the filename, the second is the sqlite3 database handle.
If everything goes right, SQLITE_OK is returned.
Query's
Once the database is opened, you can actually start doing something. The following code shows how:
The first thing you have to do is prepare the statement using sqlite3_prepare_v2(), if everything goes right, SQLITE_OK will be returned.
Then we actually have to execute the statement using sqlite3_step(). This function will return a value which we will need to determine our next action.
If the query is not supposed to return anything, like with CREATE TABLE and INSERT, we just have to finalize the statement using sqlite3_finalize() to avoid a memory leak. If the query returns colums of data, like with SELECT, the function will return SQLITE_ROW. If that happens, we will need read the data.
First we will need to know the amount of columns it has returned. We can do this usingsqlite3_column_count(). Then all we have to do is request the columns usingsqlite3_column_text().
Closing the database
Now all we have to do is close the database, so the data is saved.
Examples
http://www.dreamincode.net/forums/topic/122300-sqlite-in-c/
-Basic C++ and SQL knowledge
-SQLite (Download)
Introduction
SQLite is an embedded SQL database engine. Unlike most other SQL databases, SQLite does not have a separate server process, but instead reads and writes directly to ordinary disk files. This makes SQLite an easy and good solution to store data for your application.
Opening a database
The first thing you need to do, is open a database. If the database does not exist yet, it will be created.
1
sqlite3 *database;
2
sqlite3_open(
"Database.sqlite"
, &database);
The first argument is the filename, the second is the sqlite3 database handle.
If everything goes right, SQLITE_OK is returned.
Query's
Once the database is opened, you can actually start doing something. The following code shows how:
01
sqlite3_stmt *statement;
02
03
if
(sqlite3_prepare_v2(database,
"CREATE TABLE a (b INTEGER, c INTEGER);"
, -1, &statement, 0) == SQLITE_OK)
04
{
05
int
cols = sqlite3_column_count(statement);
06
int
result = 0;
07
while
(
true
)
08
{
09
result = sqlite3_step(statement);
10
11
if
(result == SQLITE_ROW)
12
{
13
for
(
int
col = 0; col < cols; col++)
14
{
15
string s = (
char
*)sqlite3_column_text(statement, col);
16
//do something with it
17
}
18
}
19
else
20
{
21
break
;
22
}
23
}
24
25
sqlite3_finalize(statement);
26
}
The first thing you have to do is prepare the statement using sqlite3_prepare_v2(), if everything goes right, SQLITE_OK will be returned.
Then we actually have to execute the statement using sqlite3_step(). This function will return a value which we will need to determine our next action.
If the query is not supposed to return anything, like with CREATE TABLE and INSERT, we just have to finalize the statement using sqlite3_finalize() to avoid a memory leak. If the query returns colums of data, like with SELECT, the function will return SQLITE_ROW. If that happens, we will need read the data.
First we will need to know the amount of columns it has returned. We can do this usingsqlite3_column_count(). Then all we have to do is request the columns usingsqlite3_column_text().
Closing the database
Now all we have to do is close the database, so the data is saved.
1
sqlite3_close(database);
Examples
I have written the following class to make it some simpler:
#include "Database.h"#include <iostream>Database::Database(char* filename){ database = NULL; open(filename);}Database::~Database(){}bool Database::open(char* filename){ if(sqlite3_open(filename, &database) == SQLITE_OK) return true; return false;}vector<vector<string> > Database::query(char* query){ sqlite3_stmt *statement; vector<vector<string> > results; if(sqlite3_prepare_v2(database, query, -1, &statement, 0) == SQLITE_OK) { int cols = sqlite3_column_count(statement); int result = 0; while(true) { result = sqlite3_step(statement); if(result == SQLITE_ROW) { vector<string> values; for(int col = 0; col < cols; col++) { std::string val; char * ptr = (char*)sqlite3_column_text(statement, col); if(ptr) { val = ptr; } else val = ""; // this can be commented out since std::string val; // initialize variable 'val' to empty string anyway values.push_back(val); // now we will never push NULL } results.push_back(values); } else { break; } } sqlite3_finalize(statement); } string error = sqlite3_errmsg(database); if(error != "not an error") cout << query << " " << error << endl; return results;}void Database::close(){ sqlite3_close(database);}Header:
01
#ifndef __DATABASE_H__
02
#define __DATABASE_H__
03
04
#include <string>
05
#include <vector>
06
#include <sqlite3.h>
07
08
using
namespace
std;
09
10
class
Database
11
{
12
public
:
13
Database(
char
* filename);
14
~Database();
15
16
bool
open(
char
* filename);
17
vector<vector<string> > query(
char
* query);
18
void
close();
19
20
private
:
21
sqlite3 *database;
22
};
23
24
#endif
And the following piece of code shows how to use it:
#include <iostream>#include "Database.h"using namespace std;int main(){ Database *db; db = new Database("Database.sqlite"); db->query("CREATE TABLE a (a INTEGER, b INTEGER);"); db->query("INSERT INTO a VALUES(1, 2);"); db->query("INSERT INTO a VALUES(5, 4);"); vector<vector<string> > result = db->query("SELECT a, b FROM a;"); for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it) { vector<string> row = *it; cout << "Values: (A=" << row.at(0) << ", B=" << row.at(1) << ")" << endl; } db->close();}
http://www.dreamincode.net/forums/topic/122300-sqlite-in-c/
稍作修改
- Using SQLite in C++ with Code::blocks
- my code using SQlite module with perl
- Using C code in symbian
- Code::blocks + OpenMP using minGW
- Packet Sniffer Code in C using sockets
- Using CSharp (C#) code in Powershell scripts
- Multiple locks && using Synchronized code blocks
- Using Python in Sqlite !
- Using R — Callling C code with Rcpp
- 用Code::Blocks和MinGW编译了第一个基于SQLite C API的小程序
- Blocks基础 Using Blocks in iOS 4: The Basics
- Blocks基础 Using Blocks in iOS 4: The Basics
- Using Blocks in iOS 4: The Basics
- Using Blocks in iOS 4: The Basics
- Functions with Variable Argumet lists in C using va_list
- Programming in iOS with Objective-C Using MVC
- Using SQLite in Android Application
- C/C++——Code::Blocks使用手册
- WEBconfig 自定义配置节(.net) (转)
- 错误 Could not get vmx86 driver version: 句柄无效
- 贪心算法解决部分背包问题
- 写出健壮的Bash脚本
- android学习——Android的系统架构简介
- Using SQLite in C++ with Code::blocks
- 背包问题
- linux设备模型之uart驱动架构分析
- linux程序设计笔记9:make,makefile
- 必填验证控件RequireFieldValidator的 用法
- 根据查看Class文件的了解jdk版本
- World Wind遵循的协议
- 提高效率 心无杂念 攻克c和linux
- Qt中的多线程编程