Advertisement
Guest User

Address Book using C++ and SQLite Program

a guest
Feb 25th, 2015
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.68 KB | None | 0 0
  1. /////////////////////////
  2. //HEADERS AND CONSTANTS//
  3. /////////////////////////
  4.  
  5. #include <iostream>
  6. #include <sstream>
  7. #include "sqlite3.h"
  8. #define DB "adbook.db"
  9. using namespace std;
  10.  
  11. ////////////////
  12. //DB Variables//
  13. ////////////////
  14.  
  15. bool isOpenDB = false;
  16. sqlite3 *db;
  17. char *sql;
  18. char *zErrMsg = 0;
  19. int  rc;
  20.  
  21. //////////////////////////
  22. //DB Function Prototypes//
  23. //////////////////////////
  24.  
  25. static int callback(void *NotUsed, int argc, char **argv, char **azColName){
  26.    int i;
  27.    for(i=0; i<argc; i++){
  28.       printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
  29.    }
  30.    printf("\n");
  31.    return 0;
  32. }
  33.  
  34. bool ConnectDB();
  35. void DisconnectDB();
  36. void InsertDB(unsigned long long,string,int,string,long int);
  37.  
  38. /////////////////
  39. // MAIN PROGRAM//
  40. /////////////////
  41.  
  42. int main()
  43. {
  44.     ConnectDB();
  45.    
  46.     cout << "Cool this works yey! \n";
  47.    
  48.     unsigned long long phone;
  49.     long int zipcode;
  50.     int age;
  51.     string name,address;
  52.    
  53.     cout << "Enter NAME: ";
  54.     cin >> name;
  55.     cout << "Enter PHONE NUMBER: ";
  56.     cin >> phone;
  57.     cout << "Enter ADDRESS: ";
  58.     cin >> address;
  59.     cout << "Enter ZIP CODE: ";
  60.     cin >> zipcode;
  61.     cout << "Enter AGE: ";
  62.     cin >> age;
  63.    
  64.     InsertDB(phone,name,age,address,zipcode);
  65.    
  66.     DisconnectDB();
  67. }
  68.  
  69. ///////////////////////
  70. //FUNCTION DEFINITION//  
  71. ///////////////////////
  72.  
  73. bool ConnectDB()
  74. {
  75.     int con;
  76.     con = sqlite3_open(DB, &db);
  77.     if ( con == SQLITE_OK )
  78.     {
  79.                 cout << "Connected Successful" << endl;
  80.                 isOpenDB = true;
  81.            
  82.            
  83.             /* Creating the SQL Statement to Crate a Table */  
  84.        
  85.             sql = "CREATE TABLE IF NOT EXISTS ADBOOK("  \
  86.                  "PHONE     BIGINT     PRIMARY KEY NOT NULL," \
  87.                  "NAME      TEXT    NOT NULL," \
  88.                  "AGE       INT     NOT NULL," \
  89.                  "ADDRESS   CHAR(50)," \
  90.                  "ZIPCODE   MEDIUMINT );";
  91.        
  92.             /* Execute SQL statement */
  93.            
  94.             rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
  95.  
  96.         /*  
  97.         ---SYNTAX for sqlite3_exec()---  
  98.        
  99.         int sqlite3_exec(
  100.         sqlite3*,                                  An open database
  101.         const char *sql,                           SQL to be evaluated
  102.         int (*callback)(void*,int,char**,char**),  Callback function
  103.         void *,                                    1st argument to callback
  104.         char **errmsg                              Error msg written here
  105.         );
  106.         */
  107.      
  108.  
  109.             if( rc != SQLITE_OK ){
  110.                 cout << "SQL error: " << zErrMsg;
  111.                 sqlite3_free(zErrMsg);
  112.             }
  113.             else{
  114.                 cout << "Table created successfully\n";
  115.                 return true;
  116.             }
  117.     }
  118.    
  119.     else
  120.     {
  121.         cout << "Connection failed " << endl;
  122.         return false;
  123.     }      
  124.  
  125.   }
  126.  
  127. void InsertDB(unsigned long long phone,string name,int age,string address,long int zipcode)
  128. {
  129.     stringstream insert;
  130.     insert << "INSERT INTO ADBOOK (PHONE,NAME,AGE,ADDRESS,ZIPCODE) VALUES (" << phone << ",'" << name << "'," << age << ",'" << address << "'," << zipcode << " ); ";
  131.     sql = insert.str();
  132.    
  133.  
  134.     /* Create SQL statement */
  135.    
  136.     sql = "INSERT INTO ADBOOK (PHONE,NAME,AGE,ADDRESS,ZIPCODE) "  \
  137.          "VALUES (@phone, @name, @age, @address, @zipcode ); ";
  138.          
  139.     /* Execute SQL statement */
  140.    rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
  141.    if( rc != SQLITE_OK ){
  142.       fprintf(stderr, "SQL error: %s\n", zErrMsg);
  143.       sqlite3_free(zErrMsg);
  144.    }else{
  145.       fprintf(stdout, "Records created successfully\n");
  146.    }
  147.    return;    
  148.  
  149. }
  150.  
  151. void DisconnectDB ()
  152. {
  153.     if (isOpenDB == true)
  154.     {
  155.         sqlite3_close(db);
  156.         cout << "Disconnection Successful" << endl;    
  157.     }
  158.     else
  159.     {
  160.         cout << "Nope Nope Nope" << endl;
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement