SQLite Learning note 11:c using SQLite to delete records in the language

Source: Internet
Author: User
Tags sql error

The last section, which records how to delete data.

All of the preceding code is inherited here, Ubuntu14.04 and Mac10.9 on the pro-test pass.


#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"; int ret = 0;typedef 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 array of string    s 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 () {/* 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 () {/* 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 Deletetable ()/* NEW plus */{/* Create merged SQL statement */sql = "DELETE from company where id=2;"        "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 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 (); */deletetable ();    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.