aytdev

C201050 AYAT ULLAH

Jul 4th, 2021 (edited)
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.41 KB | None | 0 0
  1. //c201050
  2. //AYAT ULLAH
  3. #include <iostream>
  4. #include<bits/stdc++.h>
  5. #include <Windows.h>
  6. #include <mysql.h>
  7. #include <iomanip>
  8. #include <stdlib.h>
  9. #include <sstream>
  10. #include <stdio.h>
  11. using namespace std;
  12. class Shop{
  13.    MYSQL* conn;
  14.     MYSQL_ROW row;
  15.     MYSQL_RES * res;
  16.     int qstate;
  17. public:
  18.  
  19.      Shop(){
  20.  
  21.     conn = mysql_init(0);
  22.     conn = mysql_real_connect(conn, "localhost", "root", "", "data", 0, NULL, 0);
  23.  
  24.     if(conn)
  25.     {
  26.  
  27.     }else
  28.     {
  29.         cout <<" Connection failure"<<endl;
  30.     }
  31.            }
  32.     string additem(string itemid,string itemname,string itemprice,string itemstock){
  33.                 int state = 0;
  34.                 stringstream ss;
  35.                 ss<<"INSERT INTO data(id,name,price,stock) values ('" << itemid<<"','" << itemname<<" ','" <<itemprice<<" ','" <<itemstock<<"')";
  36.                 string query = ss.str();
  37.  
  38.                 const char * q = query.c_str();
  39.                 qstate = mysql_query(conn, q);
  40.  
  41.                 if (qstate == 0)
  42.                 {
  43.  
  44.                 cout <<" Record inserted successfully ..."<<endl;
  45.                 }
  46.                 else
  47.                 {
  48.                 cout <<" Error, data not inserted..."<<endl;
  49.                 }
  50.  
  51.     }
  52.       void showitems(){
  53.             qstate = mysql_query(conn,"SELECT * from data");
  54.             if(!qstate){
  55.                 res = mysql_store_result(conn);
  56.  
  57.                 cout<<endl;
  58.                 cout<<"id          name          price          stock"<<endl;
  59.                 while(row = mysql_fetch_row(res)){
  60.  
  61.                     cout<<row[0]<<"|     "<<row[1]<<"|        "<<row[2]<<"|              "<<row[3]<<endl;
  62.  
  63.                 }
  64.  
  65.  
  66.             }
  67.  
  68.  
  69.  
  70.  
  71.     }
  72.     void searchitem(string name){
  73.             string query ="SELECT * FROM data WHERE name='"+name+"'";
  74.  
  75.                 const char * q = query.c_str();
  76.                 qstate = mysql_query(conn, q);
  77.  
  78.            if(!qstate){
  79.                 int c=0;
  80.                 res = mysql_store_result(conn);
  81.  
  82.                 cout<<endl;
  83.                 cout<<"id          name          price          stock"<<endl;
  84.                 while(row = mysql_fetch_row(res)){
  85.                   c++;
  86.                     cout<<row[0]<<"|     "<<row[1]<<"|        "<<row[2]<<"|              "<<row[3]<<endl;
  87.  
  88.                 }
  89.                 cout<<endl;
  90.                 cout<<"Result: total "<<c<<" data founded"<<endl;
  91.  
  92.  
  93.             }
  94.         else{
  95.               cout<<"Error"<<endl;
  96.  
  97.           }
  98.  
  99.  
  100.     }
  101.     void editeitem(string id,string newname){
  102.          string query ="UPDATE data set name='"+newname+"' where id ='"+id+"'";
  103.          const char * q = query.c_str();
  104.          qstate = mysql_query(conn,q);
  105.  
  106.         if(!qstate){
  107.  
  108.             cout<<"Updated"<<endl;
  109.         }else{
  110.            cout<<"Error"<<endl;
  111.         }
  112.  
  113.  
  114.     }
  115.     void deleteitem(string id){
  116.          string query ="DELETE from data where id ='"+id+"'";
  117.          const char * q = query.c_str();
  118.          qstate = mysql_query(conn,q);
  119.         if(!qstate){
  120.  
  121.             cout<<"Deleted"<<endl;
  122.         }else{
  123.            cout<<"Error"<<endl;
  124.         }
  125.  
  126.  
  127.     }
  128.  
  129.  
  130. };
  131.  
  132. using namespace std;
  133.  
  134. int main()
  135. {
  136.  
  137.  
  138.     Shop iiuc;
  139.     //iiuc.additem("c201033","ayat","120","1");
  140.     //iiuc.showitems();
  141.     //iiuc.searchitem("MANO");
  142.      //iiuc.editeitem("C201050","ayat");
  143.     // iiuc.deleteitem("C201050");
  144.  
  145.  
  146.     return 0;
  147. }
  148.  
Add Comment
Please, Sign In to add comment