Use C ++ in vc6.0 to access the SQLite database)

Source: Internet
Author: User

Download the Windows version from sqlite.org
Sqliteand suffix are provided in the sqlitedll.zip file, including sqlite3.def and
Sqlite3.dll file, of course, you can directly use WIN32API such as loadlibrary to operate the DLL, find the functions contained in it, and use these functions, but generally
The reason for not doing this is very simple: This is too troublesome. Therefore, we usually use the Lib command to generate the Lib for link, and then include the SQLite header file sqlite3.h into the program,
Directly call
Sqlite's apiis more convenient. Of course, sqlite3.h files must be provided from sqlitesource code (provided by sqlite-source-3_3_4.zip ).
To.

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. Now create a Win32 console in msvc6
Application project, copy the SQLite. dll, SQLite. H, and SQLite. Lib files to the project folder, and add the SQLite. h file to the item
Then add the SQLite. Lib file to the object library module in the link of Project setting.
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 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;
}


It is really easy to use SQLite in msvc6. The above program is easy to understand. Create a database, insert a record, and omit error handling. SQLite is actually a data storage.

"Swiss Army Knife". Unlike some databases, You need to configure ODBC and package a lot of DLL together into the end user program. You also need to use tools such as depends to check which database to package.
Some. dll.

 

Address: http://hi.baidu.com/lossless1009/blog/item/4715800e3c76f0e236d122ae.html

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.