Search
 
SCRIPT & CODE EXAMPLE
 

SQL

sqlite3 get data from table c

#include <stdio.h>
#include <string>
using std::string;
#include <sstream>
using std::stringstream;

#include "sqlite3.h"

bool find_employee(int _id)
{
    bool found = false;
    sqlite3* db;
    sqlite3_stmt* stmt;
    stringstream ss;

    // create sql statement string
    // if _id is not 0, search for id, otherwise print all IDs
    // this can also be achieved with the default sqlite3_bind* utilities
    if(_id) { ss << "select * from employees where id = " << _id << ";"; }
    else { ss << "select * from employees;"; }
    string sql(ss.str());

    //the resulting sql statement
    printf("sql: %s
", sql.c_str());

    //get link to database object
    if(sqlite3_open("data/test.db", &db) != SQLITE_OK) {
        printf("ERROR: can't open database: %s
", sqlite3_errmsg(db));
        sqlite3_close(db);
        return found;
    }

    // compile sql statement to binary
    if(sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, NULL) != SQLITE_OK) {
        printf("ERROR: while compiling sql: %s
", sqlite3_errmsg(db));
        sqlite3_close(db);
        sqlite3_finalize(stmt);
        return found;
    }

    // execute sql statement, and while there are rows returned, print ID
    int ret_code = 0;
    while((ret_code = sqlite3_step(stmt)) == SQLITE_ROW) {
        printf("TEST: ID = %d
", sqlite3_column_int(stmt, 0));
        found = true;
    }
    if(ret_code != SQLITE_DONE) {
        //this error handling could be done better, but it works
        printf("ERROR: while performing sql: %s
", sqlite3_errmsg(db));
        printf("ret_code = %d
", ret_code);
    }

    printf("entry %s
", found ? "found" : "not found");

    //release resources
    sqlite3_finalize(stmt);
    sqlite3_close(db);

    return found;
}
Comment

PREVIOUS NEXT
Code Example
Sql :: oracle apex call duration 
Sql :: mysql collation portugues brasil 
Sql :: reorder primary key sql 
Sql :: mysql import datetime YYYY-MM-DDThh:mm:ss.000000Z 
Sql :: cursors in db2 
Sql :: oracle execute immediate quotes 
Sql :: how to set up service broker in sql server 
Sql :: oracle create package specification 
Sql :: nueva tabla mysql 
Sql :: events not working db 
Sql :: trigger value from maltiple table to single table mysql 
Sql :: product of a column in mysql 
Sql :: get statis values sql 
Sql :: psql limit order group by 
Sql :: interview experience as a call? 
Sql :: downgrading sql localdb visual studio 
Sql :: ring MySQL rollback updates to the database 
Sql :: docmd openargs 
Sql :: odoo there is no primary key for referenced table "res_users" 
Sql :: SQL MAX() and MIN() in Nested SELECT 
Sql :: child row: a foreign key constraint fails 
Sql :: select from another database 
Sql :: supabase change timezozne 
Sql :: ms sql convert hijri to gregorian 
Sql :: 9999 
Sql :: play framework connection via windows sql server 
Sql :: how set default setting of toolbar in sql developer 
Sql :: truncate syntax in sql 
Sql :: firebird where 
Sql :: fix sqlite3 ruby on rails 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =