Access the sqlite database using C ++ in VC6.0

Source: Internet
Author: User

The functions file contains the sqlite3.def and sqlite3.dll files. Of course, you can directly use WIN32API such as LoadLibrary to operate the dll, find the functions contained in the dll, and use these functions, however, this is generally not the case because it is very simple: This is too troublesome. Therefore, we usually use the LIB command to generate the lib for the link and then include the sqlite header file sqlite3.h into the program,
In this way, calling sqlite's API is more convenient. Of course, the sqlite3.h file must be provided from the sqlitesource code (provided in the sqlite-source-3_3_4.zip file.

Follow these steps to use the LIB command of VC ++:
(1.exe set the path of lib.exe in vc98:
D:/MyDoc/db/capi> set path = % path %; "D:/Program Files/Microsoft Visual Studio/VC98/Bin"
(2) generate the lib file of SQLite:
D:/MyDoc/db/capi> LIB/DEF: SQLITE3.DEF/MACHINE: IX86

Microsoft (R) Library Manager Version 6.00.8168 Copyright (C) Microsoft Corp 1992-1998. All rights reserved. Creating library SQLITE. lib and object SQLITE. exp
In this way, the library required to access sqlite in the WIN32 program is successfully created and can be used to link the WIN32 program.
All preparations for using sqlite have been completed. create a Win32 Console Application project in MSVC6 and. dll, sqlite. h and sqlite. copy the lib file to the project folder and copy sqlite. add the H file to the Project, and then add sqlite to the object library module in the Link of the Project Setting. lib file.
Add the following code to the modification:

// Use iso c ++ to access SQLite

# Include <iostream>
# Include <string>
# Include <sstream>
# Include "sqlite3.h"

Using namespace std;

Sqlite3 * pDB;

Int createTable ()
{
Char * errMsg;
Std: string dropTab = "drop table test_tab ;";
String strSQL = "create table test_tab (f1 int, f2 long );";
Int res = sqlite3_exec (pDB, dropTab. c_str (), 0, 0, & errMsg );
If (res! = SQLITE_ OK)
{
Std: cout <"SQL Execution error." <errMsg <std: endl;
Return-1;
}
Res = sqlite3_exec (PDB, strsql. c_str (), 0, 0, & errmsg );

If (res! = Sqlite_ OK)
{
STD: cout <"An error occurred while executing the SQL statement for table creation." <errmsg <STD: Endl;
Return-1;
}
Else
{
STD: cout <"the SQL statement for table creation is successfully executed." <STD: Endl;
}

Return 0;
}

Int insert1 ()
{
Char * errmsg;

Int res = sqlite3_exec (PDB, "begin transaction;", 0, & errmsg );

For (INT I = 1; I <10; ++ I)
{
STD: stringstream strsql;
Strsql <"insert into test_tab values (";
Strsql <I <"," <(I + 10) <");";
STD: String STR = strsql. STR ();
Res = sqlite3_exec (PDB, str. c_str (), 0, & errmsg );
If (res! = Sqlite_ OK)
{
STD: cout <"SQL Execution error." <errmsg <STD: Endl;
Return-1;
}
}

Res = sqlite3_exec (PDB, "Commit transaction;", 0, & errmsg );

STD: cout <"SQL executed successfully." <STD: Endl;

Return 0;
}

Static int callback (void * notused, int argc, char ** argv, char ** azcolname ){
Int I;
For (I = 0; I <argc; I ++ ){
Std: cout <azColName [I] <"=" <(argv [I]? Argv [I]: "NULL") <",";
}
Std: cout <"/n ";
Return 0;
}

Int select1 ()
{
Char * errMsg;
String strSQL = "select * from test_tab ;";

Int res = sqlite3_exec (pDB, strSQL. c_str (), callback, 0, & errMsg );

If (res! = SQLITE_ OK)
{
Std: cout <"SQL Execution error." <errMsg <std: endl;
Return-1;
}
Else
{
Std: cout <"SQL executed successfully." <std: endl;
}
 
Return 0;
}

Int main (int argc, char * argv [])
{
If (argc <2)
{
STD: cout <"Enter the command line parameter: SQLite Database Name." <STD: Endl;
Return 0;
}

Int res = sqlite3_open (argv [1], & PDB );

If (RES ){
STD: cout <"can't open database:" <sqlite3_errmsg (PDB );
Sqlite3_close (PDB );
Return-1;
}
Res = createtable ();
If (res! = 0)
{
Return 0;
}
Res = insert1 ();
If (res! = 0)
{
Return 0;
}
Select1 ();
 
Return 0;
}

Using SQLite in msvc6 is really simple. The above program is easy to understand. Create a database and insert a record. error handling is omitted. SQLite is the Swiss army knife for data storage ". unlike some databases, You need to configure ODBC and package a large number of DLL files into the final user program. you have to use tools such as depends to check which ones you want to package. DLL.

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.