SQLite Analysis (2): Compilation and Application

Source: Internet
Author: User
Tags sql error visual studio 2010

To compile the sqlitedatabase, download a single file named sqlite-amalgamation-3071400.zip. It puts all the source code files of the sqlite3 library into a file sqlite3.c (excluding the management tool shell. c) The file contains more than 110000 lines. If the blank lines and comments are removed, there are more than 65000 lines of code! The advantage of doing so is that it is easy to apply in your project. You only need to copy this source file to your project. In addition, the compiler can perform some extra optimization when compiling a single file, because there is only one compilation unit. The test showed that the performance was improved by about 5%-10%.
The SQLite library can directly copy a single file, sqlite3.c (or sqlite3.h), to your project without compilation. However, as a separate library, it is generally recommended to compile it into a separate binary file format library, such as in Linux. so Dynamic Link Library, DLL dynamic link library in windows, and then use this library in the project through the header file sqlite3.h, which makes the software more modular.
1. Compile SQLite in Linux
(1) Compile the command line management tool: GCC shell. c sqlite3.c-lpthread-LDL-O sqlite3
Sqlit3 command line management tool is generated.
(2) Compile SQLite as a separate Dynamic Link Library: GCC sqlite3.c-lpthread-LDL-FPIC-shared-O libsqlite3.so
-FPIC: indicates that the compiled code is in a separate position. If this option is not used, the compiled code is location-related. Therefore, during dynamic loading, the code is copied to meet the needs of different processes, but cannot achieve the purpose of truly sharing code segments.
-Shared: generate a shared target file (let the connector generate a T-type export symbol table, and sometimes generate a weak-connection W-type export symbol), which is what we call a dynamic link library. It can be connected to other target files to generate executable files. Only some systems support this option.
The pthread system library is used to ensure that SQLite is thread-safe. However, because the command line tool is single-threaded, it can be compiled into a non-thread-safe command line tool to ignore the pthread library. Command: gcc-dsqlite_threadsafe = 0 shell. c sqlite3.c-LDL-O sqlite3. The DL system library is used to support dynamic loading. It is required for the sqlite3_load_extension () interface and the SQL function load_extension. If you do not need these features, you can use the sqlite_omit_load_extension compilation option to ignore them, such as gcc-dsqlite_threadsafe = 0.
-Dsqlite_omit_load_extension shell. c sqlite3.c-O sqlite3.
Use the dynamic library libsqlite3.so: In your program (such as test. c) use functions in the library by including the header file sqlite3.h. The command for compiling the program is GCC test. c-l. -lsqlite3-O test. Where-L. indicates that the search path of the Linked Library contains the current directory.-lsqlite3 indicates that the compiler searches for the dynamic library libsqlite3.so. When the compiler searches for the dynamic Connection Library, there is an implicit naming rule, that is, adding lib before the given name, add. so to determine the library name. You can use LDD test to view how the test program calls functions in the dynamic library.
Several problems are frequently encountered when calling a dynamic library. Sometimes it is clear that the directory where the library header file is located is included through "-I", the file where the library is located is guided by the "-l" parameter, and the database name of "-l" is specified, however, when you run the LDD command, you cannot find the so file with the specified link. In this case, you need to modify LD_LIBRARY_PATH, which indicates that the dynamic connector can load the path of the dynamic library. Or modify the/etc/lD. So. conf file and then call/sbin/ldconfig to achieve the same purpose. This usually solves the problem that the database cannot be linked.
(3) compile it into a static library: gcc-C sqlite3.c-lpthread-LDL-O sqlite3.o to compile it into the target file, ar-r libsqlite3.a sqlite3.o packs the listed target files into a static library libsqlite3.a.
Link to the static library: GCC test. C-L.-lsqlite3-static-O test. Do not add the-static option.
2. Compile SQLite in Windows
(1) Compile SQLite into a dynamic link library:
Open Visual Studio 2010, create a visual C ++ Win32 project named sqlite3, and select "DLL" as the project type on the Project Wizard Page ", and hook the check box for creating an empty project. Use Project ---> Add existing item... to add a single file sqlite3.c to the project. To generate the Lib file required to link sqlite3.dll, you must add a module definition file to the project. Based on the export function name listed in sqlite3.h, we can write the. Def file by ourselves, for example:
Exports
Sqlite3_aggregate_context
Sqlite3_aggregate_count
Sqlite3_auto_extension
Sqlite3_backup_finish
Sqlite3_backup_init
Sqlite3_backup_pagecount
Sqlite3_backup_remaining
Sqlite3_backup_step
Sqlite3_bind_blob
Sqlite3_bind_double
;......
You can also use the already written. def file, download the compiled SQLite dlldatabase sqlite-dll-win32-x86-3071400.zip, which contains sqlite3.def and copy it to our sqlite3 project, enter sqlite3.def In the linker ---> input ---> module definition file of the Project attribute. Set the project to the release version. After compilation, generate sqlite3.dll and sqlite3.lib.
Compile the command line management tool: If you want to compile the sqlite.exe command line program, you need to create an empty Win32 console program, then add the sqlite3.c and shell. c files to the project, and compile them directly.
Use the dynamic link library sqlite3.dll:

This example is organized from http://sqlite.org/quickstart.html. Create an empty project test, copy sqlite3.dll, sqlite3.lib, and sqlite3.h to the project directory, and add sqlite3.h to the project. Create the main program source file test. cpp as follows:


[CPP]
View plaincopyprint?
  1. # Include <stdio. h>
  2. # Include "sqlite3.h"
  3. # Pragma comment (Lib, "sqlite3 ")
  4. /* Print a record from Table outputed by SQL statement */
  5. Static int callback (void * notused,
    Int argc, char ** argv,
    Char ** azcolname ){
  6. Int I;
  7. For (I = 0; I <argc; I ++ ){
  8. Printf ("% s = % s \ n", azcolname [I], argv [I]? Argv [I]: "null ");
  9. }
  10. Printf ("\ n ");
  11. Return 0;
  12. }
  13. Int main (INT argc,
    Char ** argv ){
  14. Sqlite3 * dB;
  15. Char * zerrmsg = 0;
  16. Int RC;
  17. If (argc! = 3 ){
  18. Fprintf (stderr, "Usage: % s Database SQL-STATEMENT \ n", argv [0]);
  19. Return (1 );
  20. }
  21. Rc = sqlite3_open (argv [1], & dB);/* Open Database */
  22. If (RC ){
  23. Fprintf (stderr, "can't open database: % s \ n", sqlite3_errmsg (db ));
  24. Sqlite3_close (db );
  25. Return (1 );
  26. }
  27. Rc = sqlite3_exec (dB, argv [2], callback, 0, & zerrmsg);/* Execute SQL statement */
  28. If (RC! = Sqlite_ OK ){
  29. Fprintf (stderr, "SQL error: % s \ n", zerrmsg );
  30. Sqlite3_free (zerrmsg );
  31. }
  32. Sqlite3_close (db);/* close database */
  33. Return 0;
  34. }
Compile and generate the test.exe program. Its operation depends on sqlite3.dll. Note that you can also import sqlite3.lib without using the Pragma command in the program. Instead, you can add references to the above DLL project sqlit3 in the test project attribute.

The example of this C program shows how to use the C/C ++ interface of SQLite. The database name is obtained by the first parameter, and the second parameter is one or more SQL Execution statements. This program calls sqlite3_open () to open the specified database, and CALLS sqlite3_exec () to execute SQL statements on the database. The callback function will apply to each record in the SQL statement result set. Finally, use sqlite3_close () to close the database connection. You can use the preceding alf. DB database to test: test.exe alf. DB "select * From mytable ".
(2) Compile SQLite into a static Link Library:
Create a new visual C ++ Win32 project named sqlite3static, empty. On the Project Wizard Page, select the project type as "static library", last year "precompiled header" check box, and import the source file sqlite3.c, compile and generate sqlite3static. lib, which is a version of the static Link Library, which is much larger than the dynamic link library sqlite3.dll.
Use static Link Library:
. It can be found that it is an independent executable file and does not rely on the static Link Library. After deleting the static link library, it can still run.
In fact, the static Link Library does not have the concept of exporting. During the link process, all the commands in the Lib of the static Link Library are directly included in the final generated EXE file, therefore, if a static link library is used, there will be no "export a function for users to use. For EXE files using the static library, you can directly use the functions and global variables in Lib, regardless of whether they have exported declarations. In addition, you must note that the static Link Library cannot contain any other dynamic link library or static library, but it can be included in the dynamic link library.
3. Using SQLite in a multi-threaded Environment
The content is organized from http://sqlite.org/threadsafe.html. SQLite supports three different thread modes:
* Single thread. In this mode, all mutex locks are disabled and it is unsafe to use SQLite in multiple threads at the same time.
* Multithreading. In this mode, SQLite can be safely used in a multi-threaded environment as long as no single database connection is used in multiple threads at the same time.
* Serialize. In this mode, SQLite can be safely used in a multi-threaded environment without restrictions.
The thread mode can be used during compilation (when the SQLite source code is compiled into a library), during startup (when the SQLite application is initialized), or during running (when a new SQLite database connection is created). In general, runtime parameters will overwrite the startup parameters, and during startup parameters will overwrite the compilation parameters. However, once a single thread mode is specified, it cannot be overwritten. The default mode is the serial mode.
(1) thread mode options during compilation
Use sqlite_threadsafe to select the thread mode during compilation. If the sqlite_threadsafe parameter is not specified, the serialization mode is used. You can also explicitly use-dsqlite_threadsafe = 1 to specify the serialization mode. -Dsqlite_threadsafe = 0 indicates the single-thread mode, and-dsqlite_threadsafe = 2 indicates the multi-thread mode.
The Return Value of the sqlite3_threadsafe () interface is determined by the thread mode option during compilation. If the single-thread mode is specified during compilation, sqlite3_threadsafe () returns false. Sqlite3_threadsafe () returns true if the multi-threaded or serial mode is specified. Sqlite3_threadsafe () cannot distinguish between multi-threaded and serial modes, nor report mode changes at startup or runtime.
If the single-thread mode is specified during compilation, the key mutex logic during library compilation will be ignored, so it is impossible to activate multithreading or serialization mode at startup or running.
(2) thread mode options at startup
If the single-thread mode is not specified during compilation, the thread mode can be changed during initialization using the sqlite3_config () interface. Sqlite_config_singlethread predicate SQLite to single-thread mode, sqlite_config_multithread to set multi-thread mode, and sqlite_config_serialized to set the serialization mode.
(3) Running thread mode options
If the single-thread mode is not specified during compilation or startup, a single data connection can be created in multi-thread or serial mode, and it is impossible to downgrade a single database connection to the single-thread mode. If the single-thread mode is specified during compilation or startup, it is impossible to upgrade a single database connection to the multi-thread or serial mode.
The thread mode for a single database connection is determined by the flag given by the third parameter of sqlite3_open_v2. The sqlite_open_nomutex flag indicates that the database connection mode is multithreading, and sqlite_open_fullmutex indicates that the connection is serialized. If no flag is specified or sqlite3_open (), sqlite3_open16 () is used instead of sqlite3_open_v2 (), the thread mode specified during compilation or startup is used.

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.