SQLite Learning note 8:c using SQLite to create tables in the language

Source: Internet
Author: User

I've already said how to open and close the database, this time to say how to execute the SQL statement to create a table.


The function to use:

Sqlite3_exec (sqlite3* db, const char *sql, Sqlite_callback callback, void *data, char **errmsg)
Parameter: DB: An open DB instance Sql:sql statement, is a string callback: is a callback function data: As the first parameter of a callback function ErrMsg: Used to bring back the error message
The callback function has two types of return values.        1. Return 0: Sqlite3_exec () will continue to execute the query.        2. Return non 0: sqlite3_exec () will immediately break the query, and Sqlite3_exec () will return Sqlite_abort.

The callback function is formatted as follows: Int Sqlite_callback (    void* PV,/    * is passed by the fourth parameter of the SQLITE3_EXEC () */    int argc,/        * The number of columns in the table *    / char** argv,    /* Pointer to the query result array, can be obtained by sqlite3_column_text () */    char** col/        * pointer to the table header array, can be sqlite3_column_ Name () (); parameter format:    A callback function passed to sqlite3_exec that displays the query result    called once for each query result:    PV: Initialization parameters passed by sqlite3_exec    argc: Column number of table header    col: Table header array pointer    argv: Data array pointer return value for table header:    1: Interrupt lookup    0: Continue enumerating the data queried



Instance:

 #include < stdio.h> #include <stdlib.h> #include "sqlite/sqlite3.h" #define Db_nane "sqlite/test.db" sqlite3 *db = NULL;  char* sql = Null;char *zerrmsg = null;int ret = 0;typedef enum{false, true} bool;static int callback (void *notused,    int argc, char **argv, char **azcolname) {int i = 0;    for (i=0; i < argc; i++) {printf ("%s =%s\n", Azcolname[i], argv[i]? Argv[i]: "NULL");        } printf ("\ n"); return 0;}        BOOL Connectdb () {ret = Sqlite3_open (Db_nane, &db);        if (ret! = SQLITE_OK) {fprintf (stderr, "Error Open Database:%s\n", sqlite3_errmsg (db));                Sqlite3_free (ZERRMSG);    return false;    } fprintf (stdout, "successfully opened database\n"); return true;} 
/* Added this function relative to the previous article */bool createtable () {/    * Create SQL statement *    /sql = "Create TABLE Company ("      "ID INT PRIMA RY KEY     is not null, "     " NAME TEXT is not    null, "     " age            INT is not     null, "     " ADDRESS        CHAR (50 ), "     SALARY         REAL);";        /* Execute SQL statement *    /ret = sqlite3_exec (db, SQL, callback, 0, &zerrmsg);    if (ret! = SQLITE_OK) {        fprintf (stderr, "Error SQL:%s\n", zerrmsg);        Sqlite3_free (zerrmsg);                return false;    }        fprintf (stdout, "successfully table created\n");        return true;} BOOL Closedb () {    int ret = 0;    ret = sqlite3_close (db);    if (ret = = sqlite_busy) {        return false;    }        return true;} int main (int argc, char* argv[]) {    connectdb ();    CreateTable ();/* Added a function call *    /Closedb ();        return 0;}

This function can only be executed once, the second execution will have an error saying that the table already exists, delete the generated test.db can be executed again.

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.