Sqlite learning notes 9: Using sqlite to insert data in C language, sqlite learning notes

Source: Internet
Author: User
Tags sql error

Sqlite learning notes 9: Using sqlite to insert data in C language, sqlite learning notes

I have created a table before. Now I want to insert some data into the table. The data inserted is similar to that in the table creation. The SQL language is different. 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**    An array 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," \    "AGE            INT     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;}
/* Added this function */bool insertRecords () {/* Create SQL statement */SQL = "INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) "\" VALUES (1, 'Paul ', 32, 'california', 20000.00); "\" insert into company (ID, NAME, AGE, ADDRESS, SALARY) "\" VALUES (2, 'allen ', 25, 'texas', 15000.00); "\" insert into company (ID, NAME, AGE, ADDRESS, SALARY) "\" VALUES (3, 'Teddy ', 23, 'norway', 20000.00); "\" INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY) "\" VALUES (4, 'mark', 25, '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;}



How does C language insert variables into the SQLite database ??

To use the bind function, see "query parameterization"

How to operate sqlite databases in C language in linux

Please char * SQL = "CREATE TABLE SensorData (ID INTEGER PRIMARY KEY, SensorID INTEGER, SiteNum INTEGER, Time VARCHAR (12), SensorParameter REAL );";
Here is your SQL statement
Implicit conversion bit const char * SQL
You will assign a value to the SQL statement later, that is, it is strange to write the. rodata segment without a segment error.

If you want to use the original buff char SQL statement to allocate a large buff [512];
Either Dynamic Allocation (unnecessary)

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.