SQLite Learning note 9:c inserting data using SQLite in the language

Source: Internet
Author: User
Tags sql error sqlite

A table was created earlier and now inserts some data into it. Inserting data is almost the same as creating a table, but the SQL language is different, and the complete code 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;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**, an Array of strings representing fields in the rowchar** a array of strings representing column names); */static int call    Back (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;}

/* added this function */bool insertrecords () {/* Create SQL statement */sql = "INSERT into Company" ( id,name,age,address,salary) "" VALUES (1, ' Paul ', +, ' California ', 20000.00); "" INSERT into Company (id,name,age,address,salary) "VALUES (2, ' Allen ', +, ' Texas ', 15000.00);" "     INSERT into Company (id,name,age,address,salary) "" VALUES (3, ' Teddy ', +, ' Norway ', 20000.00); "        "INSERT into Company" VALUES (4, ' Mark ', id,name,age,address,salary, ' 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 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 ();    Closedb ();        return 0;}


SQLite Learning note 9:c inserting data using SQLite in the language

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.