Start of SQLite database application under VC6

Source: Internet
Author: User
Tags sql error
SQLite is a database suitable for embedded applications. It is small, fast, and reliable. Real open-source resources are free of charge and do not retain any copyright (PublicDomain ). It does not need to run additional server processes. It is simpler than MSAccess to use it to develop desktop database applications. [1. Generate the SQLite library file] Create "C: mylibs"

SQLite is a database suitable for embedded applications. It is small, fast, and reliable. Real open-source resources are free of charge and do not retain any copyright (Public Domain ). It does not need to run additional server processes, it is used to develop desktop database applications, almost more concise than MS Access. [1. Generate the SQLite library file] Create "C: \ mylibs \


SQLite is a database suitable for embedded applications. It is small, fast, and reliable. Real open-source resources are free of charge and do not retain any copyright (Public Domain ). It does not need to run additional server processes. It seems more concise than MS Access to use it to develop desktop database applications.


[1. Generate the SQLite library file]
Create the "C: \ mylibs \ libSQLite3" directory and use it as our current working directory.

(1) download from the official SQLite website

Source code SQLite 3.8.0.2
Http://www.sqlite.org/2013/sqlite-amalgamation-3080002.zip

DLL library file

Http://www.sqlite.org/2013/sqlite-dll-win32-x86-3080002.zip

Command Line Control Platform

Http://www.sqlite.org/2013/sqlite-shell-win32-x86-3080002.zip


Decompress the "sqlite3.def" and "sqlite3.dll" files in the library file to "C: \ mylibs \ libSQLite3 \";
Open the "command prompt" window and use the LIB command to generate the lib file used for connection:
Cd c: \ mylibs \ libSQLite3
LIB/DEF: sqlite3.def/machine: IX86
The "sqlite3.lib" and "sqlite3.exp" files are generated.

(2) Check the file list. The "C: \ mylibs \ libSQLite3 \" Directory should contain the following five files:
Sqlite3.def
Sqlite3.dll
Sqlite3.exp
Sqlite3.h
Sqlite3.lib
[2. Compile the sample program]
(1) Open VC6.0 and create an empty "Win32 console application" project named "sqlitedemo" under the "D: \ VCStudio \ sqlitedemo" directory.

(2) Project → Settings. On the Link tab, select "General" for "Category" and enter "sqlite3.lib" at the end of "Object/library modules". Note that each item is separated by spaces.

(3) copy the "libSQLite3" directory and its files to our project directory. Then, move the "sqlite3.lib" and "sqlite3.dll" files under the "libSQLite3" directory to the project directory.

In this way, add a line to the top of the CPP file that requires the sqlite Library:
# Include "sqlite3.h"
Then, you can call all functions in sqlite3.dll in the file.

(4) create a sample database. Open sqlite3.exe on the console and generate app. db:
Sqlite> create table t1 (c1 TEXT );
Sqlite> insert into t1 VALUES ('Hello World! ');
Sqlite> SELECT * FROM t1;
Hello World!
Sqlite>. exit

Copy app. db to the project directory.

(5) create a "C ++ source file" named "sqlitedemo. cpp", select "add Project", and write the code.
# Include
# Include

# Include ". \ libSQLite3 \ sqlite3.h"

Static int _ callback_exec (void * notused, int argc, char ** argv, char ** aszColName)
{
Int I;
For (I = 0; I
{
Printf ("% s = % s \ n", aszColName [I], argv [I] = 0? "NUL": argv [I]);
}

Return 0;
}

Int main (int argc, char * argv [])
{
Const char * sSQL = "select * from t1 ;";
Char * pErrMsg = 0;
Int ret = 0;
Sqlite3 * db = 0;

Ret = sqlite3_open ("./app. db", & db );

If (ret! = SQLITE_ OK)
{
Fprintf (stderr, "cocould not open database: % s", sqlite3_errmsg (db ));
Exit (1 );
}

Printf ("Successfully connected to database \ n ");

Sqlite3_exec (db, sSQL, _ callback_exec, 0, & pErrMsg );
If (ret! = SQLITE_ OK)
{
Fprintf (stderr, "SQL error: % s \ n", pErrMsg );
Sqlite3_free (pErrMsg );
}

Sqlite3_close (db );
Db = 0;

Return 0;
}

Press Ctrl + F5. The result is as follows:
Successfully connected to database
C1 = Hello World!
Press any key to continue

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.