Use SQLite in C ++

Source: Internet
Author: User

1.Generate a. Lib File

Download the sqlite3.dll and sqlite3.def files from the http://www.sqlite.org. Take VC ++ 6.0 as an example:

Step 1: Find the directory of lib.exe

Generally, under X: \ Program Files \ Microsoft Visual Studio \ vc98 \ bin, Enter cmd in "run" and switch to the directory

Step 2: Use the Lib command to generate the. Lib File

As described on many web pages, lib/DEF: sqlite3.def/machine: ix86 can be used for generation, but I encountered some minor problems when using it.

I will not talk about it here. Let's talk about some points that should be paid attention. First, if your sqlite3.def is not in X: \ Program Files \ Microsoft Visual Studio \ vc98 \ bin, you need to write the full path. Second, you need to specify it for clarity. lib file output path. The following is a complete command line: X: \ Program Files \ Microsoft Visual Studio \ vc98 \ bin> lib/out: D: \ test \ sqlite3.lib/machine: ix86/DEF: d: \ test \ sqlite3.def, and then sqlite3.lib and sqlite3.exp can be found in X: \ test \ e.

If there are few files missing during the process, go to the vcss installation directory to search for the files and copy them to the lib.exe file.

2. Use SQLite in C ++

# Include <iostream> # Include < String > Using   Namespace  STD; # include  "  SQLite/sqlite3.h  "  # Pragma Comment (Lib, "SQLite/sqlite3.lib ") Int  Main () {sqlite3 * DB;  Int Nresult = sqlite3_open ( " Test. DB  " ,& DB );  If (Nresult! = Sqlite_ OK) {cout < "  Failed to open database:  " <Sqlite3_errmsg (db) < Endl;  Return   0  ;}  Else  {Cout <"  Database opened successfully  " < Endl ;}  Char * Errmsg; nresult = Sqlite3_exec (dB, "  Create Table fuck (ID integer primary key autoincrement, name varchar (100 ))  " , Null, null ,& Errmsg );  If (Nresult! = Sqlite_ OK) {sqlite3_close (db); cout <"  Failed to create table:  " <Sqlite3_errmsg (db) < Endl;  Return   0  ;}  String  Strsql;  For ( Int I = 0 ; I < 100 ; I ++ ) {Strsql + = " Insert into fuck values (null, 'heh ');  "  ;} Cout <Strsql < Endl; nresult = Sqlite3_exec (dB, strsql. c_str (), null, null ,& Errmsg );  If (Nresult! = Sqlite_ OK) {sqlite3_close (db); cout < "  Failed to insert data:  " <Sqlite3_errmsg (db) < Endl;  Return  0  ;}  Return   0  ;} 
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.