C + + using SQLite steps and examples
Development environment: Windows 10+vs2013.
Development language: C + +.
1. Download SQLite file.
Download URL: http://www.sqlite.org/download.html.
The SQLite version is SQLite 3.11.1, related files are as follows.
Sqlite-dll-win32-x86-3110100.zip: Contains Sqlite3.def, sqlite3.dll files.
Sqlite-amalgamation-3110100.zip: Contains the sqlite3.h file.
Sqlite-tools-win32-x86-3110100.zip: Contains the Sqlite3.exe file.
2, Generate Sqlite3.lib.
? Extract the Sqlite-dll-win32-x86-3110100.zip file to D:\ SQLite.
? Run the visual Studio. lib command-line program.
? Execute the console command in turn.
- CD D:\sqlite\sqlite-dll-win32-x86-3110100
- D:
- E:\Microsoft Visual Studio 12.0\vc\bin\lib.exe/def:sqlite3.def/machine:ix86
You can generate the Sqlite3.lib file.
3. Create test data.
? Extract the Sqlite-tools-win32-x86-3110100.zip file to D:\ SQLite.
? Start the command line and enter the D:\ SQLite directory.
The commands are in turn:
- CD D:\sqlite
- D:
? Create a test.db test file.
Create a user table.
field code |
Field type |
Field description |
Id |
Integer |
Primary key, self-increment |
Name |
VARCHAR (64) |
User name |
Age |
Integer |
Age |
The Create command is as follows.
- D:\sqlite>sqlite3.exe test.db
- SQLite version 3.7.13 2012-06-11 02:05:22
- Enter '. Help ' for instructions
- Enter SQL statements terminated with a ";"
- Sqlite> CREATE TABLE User
- ...> (
- ...> ID Integer PRIMARY key autoincrement,
- ...> name varchar (64),
- ...> Age Integer
- ...>);
- Sqlite> . Quit
4. Create a sample project
? Create a Win32 console engineering Sqlitetest.
? Sqlite3.h (in the Sqlite-amalgamation-3071300.zip compression package) is added to the project.
? The sqlite3.lib is copied to the project folder.
? Add the Sqlite3.lib library dependency to the project properties.
Configuration properties->linker->input->additional dependencies Add Sqlite3.lib.
? The program code is:
[CPP]View PlainCopy
/*
@brief This program test SQLite database additions and deletions to change
@date 2012-09-03
*/
SQLiteTest.cpp:Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Sqlite3.h"
#include <iostream>
Using namespace std;
Sqlite3 * PDB = NULL;
Add Users
BOOL AddUser (const string& sName, const string& sAge);
Delete User
BOOL DeleteUser (const string& sName);
Modify User
BOOL ModifyUser (const string& sName, const string& sAge);
Find a user
BOOL Selectuser ();
int _tmain (int argc, _tchar* argv[])
{
//Open path with UTF-8 encoding
//If the path contains Chinese, encoding conversion is required
int nres = Sqlite3_open ("d:\\sqlite\\test.db", &pdb);
if (nres! = SQLITE_OK)
{
cout<<"Open database fail:" <<sqlite3_errmsg (PDB);
Goto QUIT;
}
//Add "Zhao Xiansun Lee"
if (! AddUser ("Zhao", " up")
|| ! AddUser ("Qian", " +")
|| ! AddUser ("Sun", " the")
|| ! AddUser ("Li", "+"))
{
Goto QUIT;
}
//delete "Zhao"
if (! DeleteUser ("Zhao"))
{
Goto QUIT;
}
//Modify "Sun"
if (! ModifyUser ("Sun", " the"))
{
Goto QUIT;
}
//Find Users
if (! Selectuser ())
{
Goto QUIT;
}
QUIT:
Sqlite3_close (PDB);
return 0;
}
BOOL AddUser (const string& sName, const string& sAge)
{
String strSQL = "";
strSQL + = "INSERT into User (name,age)";
strSQL + = "values ('";
strSQL + = SName;
strSQL + = "',";
strSQL + = SAge;
strSQL + = ");";
char* cerrmsg;
int nres = sqlite3_exec (PDB, Strsql.c_str (), 0, 0, &cerrmsg);
if (nres! = SQLITE_OK)
{
cout<<"Add user fail:" <<cErrMsg<<endl;
return false;
}
Else
{
cout<<"Add User success:" <<sname.c_str () <<"\ T" <<sage.c_str () <<endl;
}
return true;
}
BOOL DeleteUser (const string& sName)
{
String strSQL = "";
strSQL + = "Delete from user where name= '";
strSQL + = SName;
strSQL + = "';";
char* cerrmsg;
int nres = sqlite3_exec (PDB, Strsql.c_str (), 0, 0, &cerrmsg);
if (nres! = SQLITE_OK)
{
cout<<"Delete user fail:" <<cErrMsg<<endl;
return false;
}
Else
{
cout<<"Delete user success:" <<sname.c_str () <<endl;
}
return true;
}
BOOL ModifyUser (const string& sName, const string& sAge)
{
String strSQL = "";
strSQL + = "Update user set age =";
strSQL + = SAge;
strSQL + = "where name= '";
strSQL + = SName;
strSQL + = "';";
char* cerrmsg;
int nres = sqlite3_exec (PDB, Strsql.c_str (), 0, 0, &cerrmsg);
if (nres! = SQLITE_OK)
{
cout<<"Modify user fail:" <<cErrMsg<<endl;
return false;
}
Else
{
cout<<"Modify user success:" <<sname.c_str () <<"\ T" <<sage.c_str () <<endl;
}
return true;
}
static int Userresult (void *notused, int argc, char **argv, char **azcolname)
-