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";
- const char *user = "postgres";
- const char *password = "12345678";
- 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:");
- gets(emp_id);
- printf(" enter the following updated information : ");
- printf("salary : ");
- gets(salary);
- printf("phone : ");
- gets(phone);
- printf("dep_id : ");
- gets(dep_id);
- s
- 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 ;
- printf("(%s %s %s %s)\n",emp_id ,salary,phone ,dep_id);
- printf("Query updated");
- }
- void executeQuery(){
- printf("Enter Select Query: ");
- 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;
- int pdno,psalary;
- int x;
- /*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("%d %d", &pdno, &psalary);
- strcpy(query, "SELECT fname, Basepay FROM employee WHERE dno=");
- sprintf(buf, "%d",pdno);
- strcat(query, buf);
- strcat(query, " AND salary > ");
- sprintf(buf, "%d",psalary);
- strcat(query, buf);
- 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 %10d__%d\n", name,sal,c);
- c++;
- }*/
- }
- printf("\nEnter Option: ");
- fflush(stdin);
- scanf("%d",&x);
- }
- }
Add Comment
Please, Sign In to add comment