0

我正在自学 Pro*C,这是一个通过数据库中的记录进行游标的程序,它可以编译并运行。它甚至提示“输入Guest_ID(类型退出以终止)>>”。之后,如果输入整数,则会出错为“分段错误(核心转储)”。如果输入一个字符串,它似乎会立即进入外部 for 循环内的条件

if(nGuest_ID==0)
{
      printf("BYE\n");
      exit(0);
}

并打印“BYE”然后退出。由于我仍然熟悉变量是如何声明和从 SQL 中输入的,因此我不确定哪些声明对于此处的故障排除可能至关重要,因此我将代码保留在此处。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
exec sql include sqlca;

// OK - Here we GO
void main()
{
    // First, create all the variables that we will need to communicate between 
    // the "C" program and the database
    exec sql begin declare section;
        //VARCHAR sLastName[51], sFirstName[51], sHotelName[51], sCheckInDate[12], sRoom[11];
        VARCHAR sLastName[51], sFirstName[51], sHotelName[51], sTransDate[11];
        //int nDays, nGuest_ID, nCount;
        int nGuest_ID, nQuantity, nUnitPrice, nCount, nHotelID, nItemID;
        //VARCHAR sInCity[11];
        VARCHAR sItemName[31], sTaxable[11];
        VARCHAR sUserID[21], sPassword[21];
    exec sql end declare section;
/////// begin needs work ///////
        // Now define the cursor we will use to get all of the charges that the guest incurred at all hotels
    exec sql declare dbGuest cursor for
        Select G.Guest_ID, G.Last_Name, G.First_Name, C.Item_ID, C.Item_Name, C.Quantity, C.Unit_Price, C.Trans_Date, H.Hotel_Name, H.Hotel_ID, SI.Taxable
        From Hotel H, Charge C, Stay S, Guest G, Sales_Item SI Where
        C.Stay_ID=S.Stay_ID And H.Hotel_ID=S.Hotel_ID And G.Guest_ID=S.Guest_ID
            And SI.Item_ID=C.Item_ID
        Group By S.Guest_ID;
//////// end needs work ///////
    // Set up the user-id and password to access my database
    // Because we are using the local database on this server
    // we don't need to use any database location or SID
    strcpy(sUserID.arr,"myusername"); 
    strcpy(sPassword.arr,"mypassword"); 
    sUserID.len=strlen(sUserID.arr);
    sPassword.len=strlen(sPassword.arr);
    exec sql connect :sUserID identified by :sPassword;

    // sqlca.sqlcode is a variable that is set based on the last command sent in to the database
    // a value anything other than zero for what we just did (connect to the database) indicates
    // a error.
    if(sqlca.sqlcode !=0)
       {
        //printf("Sorry, cannot connect to server, pgm aborted %s\n",sqlca.sqlcode); //correction 2/5/14
        printf("Sorry, cannot connect to server, pgm aborted %d\n",sqlca.sqlcode); //change to %d
        exit(1);
       }
    //we made it here, so we were able to open the database correctly
    exec sql SELECT COUNT(*) INTO :nCount FROM Guest;
    printf ("There are %d Guests.\n",nCount);
    for(;;){
        // Read in through stdio the Guest we want to query, then set it up do we can use it
        printf("Enter a Guest_ID(type exit to terminate)>>\n");
        scanf("%d",nGuest_ID);
        //Guest_ID.len= strlen(Guest_ID.arr);
        if(nGuest_ID==0)
        {
            printf("BYE\n");
            exit(0);
        }
        printf("%s %s %s %s %d\n","Charge Summary for:", sFirstName.arr, sLastName.arr, " Guest_ID:", nGuest_ID);
        //printf("I do not work yet (type exit to terminate)>>\n");
                // Open our cursor and begin reading records
        exec sql open dbGuest;
        for(;;)
        {
            //exec sql fetch dbGuest into :nGuest_ID, :sLastName, :sFirstName, :sHotelName, :sCheckInDate, :nDays, :sRoom;
            exec sql fetch dbGuest into :nGuest_ID, :sLastName, :sFirstName, :nItemID, :sItemName, :nQuantity, :nUnitPrice, :sTransDate, :sHotelName, :nHotelID;
            if(sqlca.sqlcode !=0)  // If anything went wrong or we read past eof, stop the loop
            {
                break;
            }
            // Do the crazy stuff to end the C-Strings
            sLastName.arr[sLastName.len] = 0;
            sFirstName.arr[sFirstName.len] = 0;
            sItemName.arr[sItemName.len] = 0;
            sTransDate.arr[sTransDate.len] = 0;
            sHotelName.arr[sHotelName.len] = 0;

            // Print out the information for this guest
            printf("%s %d %s %s \n", "Sales_Item: ", nItemID, " - ", sItemName.arr);
        }
        // close the cursor and end the program
        exec sql close dbGuest ;
    }
    exit(0);
}

我确定我犯了一些简单的错误,但我没有发现任何对搜索有帮助的东西。我在服务器上运行它,并且我没有得到任何调试(这是迄今为止我无法解决的唯一主要错误),所以你所拥有的就是我所拥有的。通常C程序会在调试器中运行,但这是Pro C,我对整个Oracle Pro C调试的东西有点迷失(因为它在远程数据库上运行)。对于这种错误,我通常会怀疑没有正确分配内存,但我在这里看不到类似的东西。

经历了这些但没有帮助:

C 中的分段错误(核心转储)运行时错误

不太有用的错误——家庭作业中的分段错误(核心转储)

分段错误(核心转储)

从标准输入读取的分段错误(核心转储)

4

1 回答 1

4

由于nGuest_ID是 int,因此在调用 时scanf(),您需要提供以下地址nGuest_ID

scanf("%d",&nGuest_ID);

这可能是您遇到核心转储的原因,但是,OracleUser也提出了一些很好的建议。

希望有帮助。

于 2014-02-19T20:54:57.280 回答