Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////////////
- //HEADERS AND CONSTANTS//
- /////////////////////////
- #include <iostream>
- #include <sstream>
- #include "sqlite3.h"
- #define DB "adbook.db"
- using namespace std;
- ////////////////
- //DB Variables//
- ////////////////
- bool isOpenDB = false;
- sqlite3 *db;
- char *sql;
- char *zErrMsg = 0;
- int rc;
- //////////////////////////
- //DB Function Prototypes//
- //////////////////////////
- static int callback(void *NotUsed, int argc, char **argv, char **azColName){
- int i;
- for(i=0; i<argc; i++){
- printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
- }
- printf("\n");
- return 0;
- }
- bool ConnectDB();
- void DisconnectDB();
- void InsertDB(unsigned long long,string,int,string,long int);
- /////////////////
- // MAIN PROGRAM//
- /////////////////
- int main()
- {
- ConnectDB();
- cout << "Cool this works yey! \n";
- unsigned long long phone;
- long int zipcode;
- int age;
- string name,address;
- cout << "Enter NAME: ";
- cin >> name;
- cout << "Enter PHONE NUMBER: ";
- cin >> phone;
- cout << "Enter ADDRESS: ";
- cin >> address;
- cout << "Enter ZIP CODE: ";
- cin >> zipcode;
- cout << "Enter AGE: ";
- cin >> age;
- InsertDB(phone,name,age,address,zipcode);
- DisconnectDB();
- }
- ///////////////////////
- //FUNCTION DEFINITION//
- ///////////////////////
- bool ConnectDB()
- {
- int con;
- con = sqlite3_open(DB, &db);
- if ( con == SQLITE_OK )
- {
- cout << "Connected Successful" << endl;
- isOpenDB = true;
- /* Creating the SQL Statement to Crate a Table */
- sql = "CREATE TABLE IF NOT EXISTS ADBOOK(" \
- "PHONE BIGINT PRIMARY KEY NOT NULL," \
- "NAME TEXT NOT NULL," \
- "AGE INT NOT NULL," \
- "ADDRESS CHAR(50)," \
- "ZIPCODE MEDIUMINT );";
- /* Execute SQL statement */
- rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
- /*
- ---SYNTAX for sqlite3_exec()---
- int sqlite3_exec(
- sqlite3*, An open database
- const char *sql, SQL to be evaluated
- int (*callback)(void*,int,char**,char**), Callback function
- void *, 1st argument to callback
- char **errmsg Error msg written here
- );
- */
- if( rc != SQLITE_OK ){
- cout << "SQL error: " << zErrMsg;
- sqlite3_free(zErrMsg);
- }
- else{
- cout << "Table created successfully\n";
- return true;
- }
- }
- else
- {
- cout << "Connection failed " << endl;
- return false;
- }
- }
- void InsertDB(unsigned long long phone,string name,int age,string address,long int zipcode)
- {
- stringstream insert;
- insert << "INSERT INTO ADBOOK (PHONE,NAME,AGE,ADDRESS,ZIPCODE) VALUES (" << phone << ",'" << name << "'," << age << ",'" << address << "'," << zipcode << " ); ";
- sql = insert.str();
- /* Create SQL statement */
- sql = "INSERT INTO ADBOOK (PHONE,NAME,AGE,ADDRESS,ZIPCODE) " \
- "VALUES (@phone, @name, @age, @address, @zipcode ); ";
- /* Execute SQL statement */
- rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
- if( rc != SQLITE_OK ){
- fprintf(stderr, "SQL error: %s\n", zErrMsg);
- sqlite3_free(zErrMsg);
- }else{
- fprintf(stdout, "Records created successfully\n");
- }
- return;
- }
- void DisconnectDB ()
- {
- if (isOpenDB == true)
- {
- sqlite3_close(db);
- cout << "Disconnection Successful" << endl;
- }
- else
- {
- cout << "Nope Nope Nope" << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement