Proc Programming Report

Proc programming experiment
1, Experimental content
1) Create three tables in the database: Student s, course C and s grade SC.
2) Write a program using pro*C to complete the operation of the database and output it according to the corresponding format. The format is as follows:

3) Use Pro*C to write the program, complete the operation of the database, and output according to the corresponding format. The format example is as follows:

2, Experimental process
1) Created S table:

Figure 1:S table data information
Table C created:

Figure 2: created C table information
SC table created:



Figure 3: creating SC table information
2) Write a program using proc:
First, use rdbms to precompile the Executable program of pc file. Precompiled from the executable pc files.
You can also directly enter the proc file name in cmd to compile

Figure 4: proc writing pc file
3) Run compiled c Documents
If you directly use VS and compile on line, an error will be reported. The configuration parameters of vs need to be adjusted as follows:
Right click Project - > properties - > VC + + Directory - > include directory to add header file directory: oracle\product.1.0\client_1\precomp\public
Add to Library Directory lib Directory:
oracle\product\11.1.0\client_1\precomp\LIB
oracle\product\11.1.0\client_1\RDBMS\XA
oracle\product\11.1.0\client_1\oci\lib\msvc
oracle\product\11.1.0\client_1\precomp\LIB\msvc
Right click Project - > properties - > configuration properties - > linker - > Input - > add additional dependency lib file:
orasql11.lib;oraxa11.lib;oci.lib;orasqx11.lib
After repairing this parameter, the operation will still report an error, showing that the class in the code conflicts with the class in C. We need to precompile in advance. The precompiled code is as follows:
Here are some inline snippets.

#ifdef __cplusplus
extern "C"
{
#endif
    int yyparse(void);
    void sqliem(unsigned char*, signed int*);
    void sqlcxt(void**, unsigned int*,
        struct sqlexd*, const struct sqlcxp*);
    //. . .  Various required function declarations
#ifdef __cplusplus 
}
#endif

4) Modification pc file

void sql_error();//Error reporting function
void FindDept();//Find department student information
void FindName();//Find student information
int main()
{  
   exec sql include sqlca;
   exec sql begin declare section;
      char user_name[20];
      char  user_pwd[20]; 
      char db_name[20]; 
       char  ip[20]; 
      char port[20]; 
      char url[100];
      int i;
      char s_no[6];
      char s_name[10];
      char s_sex[4];
      int s_age; 
      int var_age; 
      char dept[5];
      int choice;
   exec sql end declare section;
   exec sql whenever SQLERROR do sql_error();
      printf("\n\n\b Welcome to the student age query function\n\n");
      printf("\b\b IP Address:"); gets_s(ip);
      printf("\b\b Port number:");gets_s(port);  
      printf("\b\b Database server name:");gets_s(db_name); 
      printf("\b\b user name:"); gets_s(user_name);
      printf("\b\b password:");gets_s(user_pwd);  
    strcpy_s(url,ip);
    strcat_s(url,":");
    strcat_s(url,port);
    strcat_s(url,"/");
    strcat_s(url,db_name);  
   exec sql connect :user_name
              identified by :user_pwd using :url;   
   printf("\n\n user:%s password:%s,Login success, ha ha.\n",user_name,user_pwd);  
   system("pause");
    while(1)
    {     
        system("cls");
        cout << "Please enter options:" << endl;
        cout << "1.Query students in a department" << endl;
        cout << "2.Query the course score of a student" << endl;
        cout << "3.Exit the system" << endl;
        cin >> choice;
        switch (choice)
        {
            case 1:
                FindDept();
                break;
            case 2:
                FindName();
                break;
            case 3:
                break;
       }
      if(choice == 3) break;
    }
    system("pause")
    exec sql commit release;             
    return 0;
}
 void FindDept()
{
     exec sql include sqlca;
     exec sql begin declare section;
     char s_no[6];
     char s_name[10];
     char s_sex[4];
     int s_age;
     char dept[5];
     exec sql end declare section;
    int i = 0;
    system("cls");
    cout << "Please enter the search department name:";
    cin >> dept;
    exec sql declare sx1 cursor for
        select sno, sname, sex, age from s where s.dept = :dept;
    exec sql open sx1;
    system("cls");
    cout << "\n\t\t" << dept << "The student information of the Department is" << endl;
    cout << "\n\t---------------------------------------------";
    cout << "\n\t Serial number\t Student number\t full name\t\t Gender\t Age" << endl;
    cout << "\n\t---------------------------------------------";
    while (1)
    {
        exec sql fetch sx1 into : s_no, : s_name, : s_sex, : s_age;
        if (sqlca.sqlcode != 0)  break;
        i++;
        printf("\n\t %d\t%s\t%s\t%s\t%d", i, s_no, s_name, s_sex, s_age);
    }
    exec sql close sx1;
}
void FindName()
{
    int i = 0;
    int sum = 0;
    exec sql include sqlca;
    exec sql begin declare section;
    char s_no[6];
    char s_name[10];
    char s_sex[4];
    char c_name[10];
    int grade;
    int s_age;
    char name[10];
    exec sql end declare section;
    system("cls");
    cout << "Please enter the name of the student you are looking for:";
    cin >> name;
    exec sql declare sx2 cursor for
        select c.cname, sc.grade from s, c, sc where c.cno = sc.cno and s.sno = sc.sno and s.sname = :name;
    exec sql open sx2;
    system("cls");
    cout << "\n\t\t" << name << "Student information is" << endl;
    cout << "\n\t---------------------------------------------";
    cout << "\n\t Serial number\t Course number\t achievement" << endl;
    cout << "\n\t---------------------------------------------";
    while (1)
    {
        exec sql fetch sx2 into : c_name, : grade;
        if (sqlca.sqlcode != 0)  break;
        i++;
        sum += grade;
        printf("\n\t %d\t%s\t%d", i, c_name, grade);
    }
    if (i != 0)
    {
        cout << "Average score:" << sum / i << endl;
    }
    exec sql close sx2;
}
void sql_error()
{
   printf("SQL sentence in wrong:Error code:%d Error description:%s\n",sqlca.sqlcode,sqlca.sqlerrm.sqlerrmc);
    system("pause");
    exit(0);
}
#endif

3, Experimental results
1) Login user:

Figure 5: login user
2) Query department student information:

3) Query the information of a student:

Figure 7: student information in the article
4, Experimental summary
Through computer programming, learn how to use proc precompile pc file, through online query and learning, learn how to use vs to compile the pre compiled c program, and understand the handling methods of handling function conflicts.

----This chapter is used to record the blogger's database learning. Please indicate the source for reprint!

Keywords: C Database Visual Studio

Added by cdinca on Mon, 31 Jan 2022 21:17:31 +0200