C Language Use SQLite3 database

Source: Internet
Author: User
Tags sql error sqlite win32

Original: C language using SQLITE3 database

SQLite is a well-known free database for both commercial and private use, and has provided support to a wide range of companies such as Adobe, Airbus, Apple, Google, GE, Microsoft, etc.

Unlike most other SQL databases, SQLite does not have a separate service process, it is a standalone, non-process database (it is also like a language library), and its read and write operations to the data are direct to the disk.

Below we use C language to try a SQLite

1 go to http://www.sqlite.org/download.html to download the corresponding files for your operating system

The other side of the C language operation, only need the source code can be:

Source Code

Sqlite-autoconf-3080701.tar.gz
(1.91 MiB)

In general, we also need to use the command line to make the data in the database to be added and checked, so we also download the following two files:

Precompiled Binaries for Windows

Sqlite-shell-win32-x86-3080701.zip
(303.20 KiB)

Sqlite-dll-win32-x86-3080701.zip
(335.37 KiB)

2 Create a new pure C project in Qt creator (I'm called sqlite3_test here), and then modify the MAIN.C to look like this

#include <stdio.h>#include<sqlite3.h>Static intCallbackvoid*notused,intargcChar**ARGV,Char**azcolname) {    inti;  for(i=0; i<argc; i++) {printf ("%s =%s\n", Azcolname[i], argv[i]? Argv[i]:"NULL"); } printf ("\ n"); return 0;}intMainintargcChar**argv) {Sqlite3*DB; Char*zerrmsg =0; intRC; if(argc!=3) {fprintf (stderr,"Usage:%s DATABASE sql-statement\n", argv[0]); return(1); } RC= Sqlite3_open (argv[1], &db); if(RC) {fprintf (stderr,"Can ' t Open database:%s\n", sqlite3_errmsg (db));        Sqlite3_close (DB); return(1); } RC= Sqlite3_exec (db, argv[2], Callback,0, &zerrmsg); if(rc!=SQLITE_OK) {fprintf (stderr,"SQL Error:%s\n", zerrmsg);    Sqlite3_free (ZERRMSG);    } sqlite3_close (db); return 0;}

3

Extract the sqlite-autoconf-3080701.tar.gz and put the sqlite3.h copy out of the QT project root directory.

Extract the Sqlite-dll-win32-x86-3080701.zip and copy the Sqlite3.dll into the QT project directory.

Extract the Sqlite-shell-win32-x86-3080701.zip and copy the Sqlite3.ext into the C:\Windows\System32 below. This allows you to run sqlite3 (shell) directly under the CMD command-line window.

Open a CMD window, then get to your project root, then run sqlite3 to open the SQLite Shell, create a new database and insert the data as follows to facilitate the C-language read operation:

Sqlite>. Tablessqlite>. Open Test.dbsqlite>. Tablessqlite> CREATE TABLE TBL1 (one varchar (Ten), smallint); SQLite> INSERT into TBL1 values ('hello!',Ten); SQLite> INSERT into TBL1 values ('Goodbye', -); SQLite>Select*From Tbl1;hello!|TenGoodbye| -

4 setting compilation options for QT Engineering

The 3rd step is to copy the required files to the corresponding directory, and now the compilation has no way to pass, need some settings.

Modify the Sqlite3_test.pro file (red for new)

TEMPLATE =+= = = =main.c+ =  sqlite3.dll include (DEPLOYMENT.PRI) qtcadddeployment ()

Add run parameters to the application, that is, run from the command line, you need to execute sqlite3_test test.db "select * from TBL1"

" SELECT * from Tbl1 "

5 Compiling and running

Reference:

Command line Shell for SQLite http://www.sqlite.org/cli.html

C-language interface to SQLite http://www.sqlite.org/c3ref/intro.html

SQL as understood by SQLite http://www.sqlite.org/lang.html

Getting started with SQLite3 C language API http://www.blogjava.net/xylz/archive/2012/09/25/388519.html

SQLite3 API Programming Manual http://www.cnblogs.com/hnrainll/archive/2011/09/08/2170506.html

C Language Use SQLite3 database

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.