SQLite Learning Notes 10:c language using SQLite query and update data

Source: Internet
Author: User
Tags sql error sqlite query

The third parameter in Sqlite_exec () mentioned earlier, SQLite calls this callback function for each record that is processed in each SELECT statement executed within the SQL parameter.

This section adds two functions, selectfromtable and updatetable.

The instance program is as follows:

#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;const char* data = "Callback function called";/* Change to global */int ret = 0;type Def enum{false, true} bool;/*typedef int (*sqlite3_callback) (void*, Data provided in the 4th argument of Sqlite3 _exec () int, the number of columns in rowchar**, a array of strings representing fields in the rowchar** an ARRA    Y of strings representing column names); */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;}      BOOL CreateTable () {/* Create SQL statement */sql = "Create TABLE Company (" "ID INT PRIMARY KEY not NULL,"         "NAME TEXT NOT NULL," "The Age INT is not null," "ADDRESS CHAR (#)," "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 Insertrecords () {/* Create SQL statement */sql = "INSERT into Company" (id,name,age,address,salary) "VAL UES (1, ' Paul ', +, ' California ', 20000.00); "INSERT into Company" VALUES (2, ' Allen ', id,name,age,address,salary, ' Texas ', 15000.00); "" INSERT into Company (id,name,age,address,salary) "VALUES (3, ' Teddy ', +, ' NoRway ', 20000.00); ""        INSERT into Company (id,name,age,address,salary) "VALUES (4, ' Mark ', +, ' Rich-mond ', 65000.00);";    /* Execute SQL statement */ret = sqlite3_exec (db, SQL, callback, 0, &zerrmsg);        if (ret! = SQLITE_OK) {fprintf (stderr, "SQL Error:%s\n", zerrmsg);        Sqlite3_free (ZERRMSG);    return false;    } fprintf (stdout, "successfully records created\n"); return true;}
BOOL Selectfromtable ()/* new plus */{/    * Create SQL statement */    sql = "SELECT * from company";        /* Execute SQL statement *    /ret = sqlite3_exec (db, SQL, Callback, (void*) data, &zerrmsg);    if (ret! = SQLITE_OK) {        fprintf (stderr, "Error SQL:%s\n", zerrmsg);        Sqlite3_free (zerrmsg);        return false;    }        fprintf (stdout, "successfully operation Done\n");        return true;}

BOOL UpdateTable ()/* NEW Add */{/* Create merged SQL statement */sql = "UPDATE Company set SALARY = 25000.00 where id=1 ;        "SELECT * from Company";    /* Execute SQL statement */ret = sqlite3_exec (db, SQL, Callback, (void*) data, &zerrmsg);        if (ret! = SQLITE_OK) {fprintf (stderr, "SQL Error:%s\n", zerrmsg);                Sqlite3_free (ZERRMSG);    return false;        } fprintf (stdout, "successfully operation done \ 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 (); */    /*insertrecords (); */    selectfromtable ();    UpdateTable ();    Selectfromtable ();    Closedb ();        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.