Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <ecpgtype.h>
- #include <ecpglib.h>
- #include <sqlda.h>
- EXEC SQL BEGIN DECLARE SECTION;
- char query[1000],temp,name[30], buf[20];
- const char *target = "postgres@localhost";
- const char *user = "postgres";
- const char *password = "umang999";
- EXEC SQL END DECLARE SECTION;
- void executeUpdate(){
- EXEC SQL BEGIN DECLARE SECTION;
- char salary[15],phone[15],dep_id[15],emp_id[15];
- EXEC SQL END DECLARE SECTION;
- EXEC SQL WHENEVER SQLERROR SQLPRINT;
- printf("enter employeeId for which you want to update:");
- scanf("%s",&emp_id);
- printf(" enter the following updated information : ");
- printf("salary : ");
- scanf("%s",&salary);
- printf("phone : ");
- scanf("%s",&phone);
- printf("dep_id : ");
- scanf("%s",&dep_id);
- 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 ;
- EXEC SQL COMMIT;
- printf("(%s %s %s %s)\n",emp_id ,salary,phone ,dep_id);
- printf("Query updated");
- }
- void executeQuery(){
- printf("Enter Select Query: ");
- getchar();
- scanf("%[^\n]s",query);
- EXEC SQL PREPARE query_prep FROM :query;
- EXEC SQL EXECUTE query_prep;
- EXEC SQL COMMIT;
- }
- int main(){
- EXEC SQL BEGIN DECLARE SECTION;
- char sal[20],pdno[20],psalary[20];
- int x,c=0;
- char uid[20],fname[20],lname[20],password[20],home_latitude,home_longitude,work_latitude,work_longitude,work_latitude,eWalletBal,joinDate,email,contact;
- EXEC SQL END DECLARE SECTION;
- EXEC SQL WHENEVER SQLERROR SQLPRINT;
- EXEC SQL CONNECT TO :target USER :user USING :password;
- EXEC SQL set search_path to taxi;
- printf("Choose Option: \n");
- printf("1 = > Insert or Update query\n");
- printf("2 = > Select query\n");
- printf("3 = > Insert user (Using PreparedStatement)" );
- printf("4 = > Select employee with salary of atleast certain amount(Using PreparedStatement)" );
- printf("0 = > Exit\n\n");
- printf("\n\nEnter Option: ");
- fflush(stdin);
- scanf("%d",&x);
- printf("\n");
- while(x){
- if (x==1)
- executeUpdate();
- else if(x==2)
- executeQuery();
- else if(x==3){
- printf("User ID: ");
- gets(uid);
- printf("first name: ");
- gets(fname);
- printf("last name: ");
- gets(lname);
- printf("password: ");
- gets(password);
- printf("Home longitude: ");
- gets(home_longitude);
- printf("Home latitude: ");
- gets(home_latitude);
- printf("Work latitude:");
- gets(work_longitude);
- printf("Work latitude: ");
- gets(work_latitude);
- printf("email:");
- gets(email);
- printf("Contact:");
- gets(contact);
- EXEC SQL INSERT INTO users (Cust_ID,Name,DOB,Ride_Count,Emer_Contact,Reg_Date,City,Area,phone) VALUES
- (:uid,:fname,:lname,:password,:home_longitude,:home_latitude,:work_longitude,:work_latitude,:email,:contact);
- printf("Added to the database.\n");
- }else if(x==4){
- printf("Enter departmentNo and Salary respectively");
- scanf("%s", &psalary);
- //printf("%d %d\n",pdno,psalary );
- strcpy(query, "SELECT fname, basepay FROM employee WHERE");
- //strcat(query,pdno);
- //strcat(query, s);
- strcat(query, " basepay > ");
- strcat(query, psalary);
- printf("%s\n",query);
- EXEC SQL PREPARE query_prep FROM :query;
- EXEC SQL DECLARE emp_cur CURSOR FOR query_prep;
- EXEC SQL OPEN emp_cur;
- while (sqlca.sqlcode == 0){
- EXEC SQL FETCH emp_cur INTO :name, :sal;
- printf("%-10s %10s__%d\n", name,sal,c);
- // c++;
- }
- exit(0);
- }
- printf("\nEnter Option: %d",x);
- getchar();
- fflush(stdin);
- scanf("%d",&x);
- }
- }
Add Comment
Please, Sign In to add comment