Guest User

Untitled

a guest
Nov 10th, 2017
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ecpgtype.h>
  5. #include <ecpglib.h>
  6. #include <sqlda.h>
  7.  
  8. EXEC SQL BEGIN DECLARE SECTION;
  9. char query[1000],temp,name[30], buf[20];
  10. const char *target = "postgres@localhost";
  11. const char *user = "postgres";
  12. const char *password = "umang999";
  13. EXEC SQL END DECLARE SECTION;
  14.  
  15. void executeUpdate(){
  16. EXEC SQL BEGIN DECLARE SECTION;
  17. char salary[15],phone[15],dep_id[15],emp_id[15];
  18. EXEC SQL END DECLARE SECTION;
  19.  
  20. EXEC SQL WHENEVER SQLERROR SQLPRINT;
  21.  
  22. printf("enter employeeId for which you want to update:");
  23. scanf("%s",&emp_id);
  24. printf(" enter the following updated information : ");
  25. printf("salary : ");
  26. scanf("%s",&salary);
  27. printf("phone : ");
  28. scanf("%s",&phone);
  29. printf("dep_id : ");
  30. scanf("%s",&dep_id);
  31.  
  32. EXEC SQL UPDATE employee SET basepay =:salary, contactno =:phone, deptid =:dep_id WHERE empid = :emp_id RETURNING empid ,basepay,contactno,deptid INTO :emp_id,:salary,:phone,:dep_id ;
  33. EXEC SQL COMMIT;
  34. printf("(%s %s %s %s)\n",emp_id ,salary,phone ,dep_id);
  35. printf("Query updated");
  36. }
  37.  
  38. void executeQuery(){
  39. printf("Enter Select Query: ");
  40. getchar();
  41. scanf("%[^\n]s",query);
  42. EXEC SQL PREPARE query_prep FROM :query;
  43. EXEC SQL EXECUTE query_prep;
  44. EXEC SQL COMMIT;
  45. }
  46.  
  47. int main(){
  48.  
  49. EXEC SQL BEGIN DECLARE SECTION;
  50.  
  51. char sal[20],pdno[20],psalary[20];
  52. int x,c=0;
  53. char uid[20],fname[20],lname[20],password[20],home_latitude,home_longitude,work_latitude,work_longitude,work_latitude,eWalletBal,joinDate,email,contact;
  54.  
  55. EXEC SQL END DECLARE SECTION;
  56.  
  57. EXEC SQL WHENEVER SQLERROR SQLPRINT;
  58.  
  59. EXEC SQL CONNECT TO :target USER :user USING :password;
  60. EXEC SQL set search_path to taxi;
  61.  
  62. printf("Choose Option: \n");
  63. printf("1 = > Insert or Update query\n");
  64. printf("2 = > Select query\n");
  65. printf("3 = > Insert user (Using PreparedStatement)" );
  66. printf("4 = > Select employee with salary of atleast certain amount(Using PreparedStatement)" );
  67. printf("0 = > Exit\n\n");
  68. printf("\n\nEnter Option: ");
  69. fflush(stdin);
  70.  
  71. scanf("%d",&x);
  72. printf("\n");
  73. while(x){
  74. if (x==1)
  75. executeUpdate();
  76. else if(x==2)
  77. executeQuery();
  78. else if(x==3){
  79. printf("User ID: ");
  80. gets(uid);
  81. printf("first name: ");
  82. gets(fname);
  83. printf("last name: ");
  84. gets(lname);
  85. printf("password: ");
  86. gets(password);
  87. printf("Home longitude: ");
  88. gets(home_longitude);
  89. printf("Home latitude: ");
  90. gets(home_latitude);
  91. printf("Work latitude:");
  92. gets(work_longitude);
  93. printf("Work latitude: ");
  94. gets(work_latitude);
  95. printf("email:");
  96. gets(email);
  97. printf("Contact:");
  98. gets(contact);
  99.  
  100.  
  101. EXEC SQL INSERT INTO users (Cust_ID,Name,DOB,Ride_Count,Emer_Contact,Reg_Date,City,Area,phone) VALUES
  102. (:uid,:fname,:lname,:password,:home_longitude,:home_latitude,:work_longitude,:work_latitude,:email,:contact);
  103.  
  104. printf("Added to the database.\n");
  105.  
  106. }else if(x==4){
  107. printf("Enter departmentNo and Salary respectively");
  108. scanf("%s", &psalary);
  109. //printf("%d %d\n",pdno,psalary );
  110. strcpy(query, "SELECT fname, basepay FROM employee WHERE");
  111. //strcat(query,pdno);
  112. //strcat(query, s);
  113. strcat(query, " basepay > ");
  114.  
  115. strcat(query, psalary);
  116. printf("%s\n",query);
  117.  
  118. EXEC SQL PREPARE query_prep FROM :query;
  119. EXEC SQL DECLARE emp_cur CURSOR FOR query_prep;
  120. EXEC SQL OPEN emp_cur;
  121.  
  122. while (sqlca.sqlcode == 0){
  123. EXEC SQL FETCH emp_cur INTO :name, :sal;
  124. printf("%-10s %10s__%d\n", name,sal,c);
  125. // c++;
  126. }
  127. exit(0);
  128. }
  129. printf("\nEnter Option: %d",x);
  130. getchar();
  131. fflush(stdin);
  132. scanf("%d",&x);
  133. }
  134.  
  135. }
Add Comment
Please, Sign In to add comment