Sqlite3 Getting started précis-writers

Source: Internet
Author: User
Tags sqlite import database

One, install Sqlite3

1. See http://www.runoob.com/sqlite/sqlite-intro.html when getting started, easy to say, but suitable for getting started

2. In the terminal input sqlite3, there is no return information, indicating that the system is not installed Sqlite3, or the system is installed (the system is usually shipped with the installation)

3. When you need to install it yourself, download it to http://www.sqlite.org/sqlite-autoconf-3070500.tar.gz

Then: (1) TAR-ZXVF sqlite-autoconf-3070500.tar.gz

(2) CD sqlite-autoconf-3070500

(3)./configure--prefix=/xx/xxxx (/xx/xxxx = file generation directory)

(4) Make & make install (generated after successful in/xx/xxxx directory (Bin include Lib share 4 directories))

Where bin place executable file Sqlite3,./sqlite3 can enter SQLite command line interface

Include drop header file

LIB Drop library file

Two, more commands to use.

(1) Get point command list:. Help

(2) Exit prompt:. Quit. Exit

(3) Outputs to screen: output stdout

(4) List database name:. databases

(5) Dump the database in SQL text format:. Dump

Third, the use of more storage classes

NULL, INTEGER, REAL, TEXT

Four, very useful grammar

(1) Create database: Sqlite3 xxx.db

(2) Create a table

(3) Delete a table

(4) INSERT into syntax

(5) Select syntax

(6) Where syntax

(7) Update syntax

(8) Delete syntax

Five, Sqlite3 API uses

(1) The three most important functions:

int sqlite3_open (const char*, sqlite3* *);

int sqlite3_exec (sqlite3*, const char *sql, Sqlite_callback, void*, char**);

int sqlite3_close (sqlite3*);

There are 2 other:

const char *sqlite3_errmsg (sqlite3*);

void Sqlite3_free (void*);

(2)sqlite3_open Returns an integer error code, =0 indicates that the success code,> 0 are error codes, details see Sqlite3 manual description

function to open/create a library of functions

Const char* Specifies the file name,sqlite3* * Specifies the database handle and the user operates the database through the database handle

      

(2)sqlite3_exec return 0 indicates the execution of the SQL instruction is complete, otherwise the execution is unsuccessful.

function is used to execute one or more SQL statements, separated by ";" Between SQL statements

      sqlite3* Specifies open database handle, const char *sql specifies SQL instruction, sqlite_callback can get results of SQL execution in callback function

void* Specifies the data passed to the callback function, char** specifies the detailed error message that the command failed to execute

(3) Callback function

      

(4) sqlite3_close Close database file, parameter is database handle

(5)sqlite3_errmsg Returns the text description of the error code, the parameter is the database handle

(6) Sqlite3_free releases the memory space where the error message is stored, the errmsg returned bysqlite3_errmsg must be freed with this function

(7) Simple test code

      

1#include <stdio.h>2#include <sqlite3.h>3 4 intCallbackvoid*PV,intargcChar**ARGV,Char**Col)5 {6     intCnt_i =0;7      for(Cnt_i =0; Cnt_i < argc;cnt_i++)8     {9printf"%s\t%s\n", col[cnt_i],argv[cnt_i]);Ten     } Oneprintf"\ n"); A     return 0; - } -  the intMainvoid) - { -Sqlite3 *db; -     intresult =0; +     Char*rerrmsg =NULL; -     Char*sql =NULL; +     Char*data ="Callback"; A  atresult = Sqlite3_open ("sample.db",&db); -     if(Result >0) -     { -printf"Open Database err:%s\n", sqlite3_errmsg (db)); -         return-1; -     } in  -     Else to     { +printf"Open Database successfully!\n"); -  thesql ="CREATE TABLE STUDENT ("     *               "NUM INT PRIMARY KEY not NULL,"     $               "NAME TEXT not NULL,"        Panax Notoginseng               "The age INT is not NULL,"         -               "sorce REAL);"; the  +result = Sqlite3_exec (db,sql,callback,null,&rerrmsg); A         if(Result! =0) the         { +printf"creat table err:%s\n", rerrmsg); - Sqlite3_free (rerrmsg); $             return-2; $         } -  -         Else the         { -printf"CREATE TABLE successfully!\n");Wuyi  thesql ="INSERT into STUDENT (num,name,age,sorce)"         -                   "VALUES (1, ' Paul ', 13,99.1);"                     Wu                   "INSERT into STUDENT (num,name,age,sorce)"         -                   "VALUES (2, ' Kate ', 15,94.1);"                     About                   "INSERT into STUDENT (num,name,age,sorce)"         $                   "VALUES (3, ' Jim ', 12,95.1);"                     -                   "INSERT into STUDENT (num,name,age,sorce)"         -                   "VALUES (4, ' Tom ', 13,99.4);"                     -                   "INSERT into STUDENT (num,name,age,sorce)"         A                   "VALUES (5, ' Jack ', 13,89.1);"; +  theresult = Sqlite3_exec (db,sql,callback,null,&rerrmsg); -             if(Result! =0) $             { theprintf"Insert Data err:%s\n", rerrmsg); the Sqlite3_free (rerrmsg); the                 return-3; the             } -  in             Else the             { theprintf"Insert Data successfully!\n"); About  thesql ="SELECT * from STUDENT"; theresult = Sqlite3_exec (Db,sql,callback, (void*) data,&rerrmsg); the                 if(Result! =0) +                 { -printf"Select Data err:%s\n", rerrmsg); the Sqlite3_free (rerrmsg);Bayi                     return-4; the                 } the  -                 Else -                 { theprintf"Select Data successfully!\n"); the                 } the             } the         } -     } the  the sqlite3_close (db); the 94     return 0; the}
View Code

VI, graphical interface management tools

SQLite expert-personal Edition, easy to get started, directly import database files can be

Above

2017/03/30

Sqlite3 Getting started précis-writers

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.