C + + using SQLite steps and examples

Source: Internet
Author: User
Tags goto sqlite sqlite database win32



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.


    1. CD D:\sqlite\sqlite-dll-win32-x86-3110100
    2. D:
    3. 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:


    1. CD D:\sqlite
    2. 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.


    1. D:\sqlite>sqlite3.exe test.db
    2. SQLite version 3.7.13 2012-06-11 02:05:22
    3. Enter '. Help ' for instructions
    4. Enter SQL statements terminated with a ";"
    5. Sqlite> CREATE TABLE User
    6. ...> (
    7. ...> ID Integer PRIMARY key autoincrement,
    8. ...> name varchar (64),
    9. ...> Age Integer
    10. ...>);
    11. 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)



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.