Guest User

Untitled

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