2014-06-10 123 views
1

在每個使用Firebird的c接口代碼的程序中,我傳遞用戶憑證以連接到數據庫。如果我不傳遞這些憑據並直接調用isc_attach_database()來連接數據庫,則其拋出錯誤:您的用戶名和密碼未定義。要求數據庫管理員設置Firebird登錄。Firebird數據庫連接憑證

有沒有辦法跳過這些或使這些東西作爲默認。我的意思是我想連接到每個程序的數據庫,而不通過unamepassword

下面是我用來連接數據庫的示例代碼。

int main() 
{ 
    isc_db_handle db1 = NULL; // Database handle. 
    isc_tr_handle trans = NULL;//transaction handle 
    // Allocate some pointers to a dpb (database parameter buffer). 
    // You use the dpb to talk with the database. 
    char dpb_buffer[256], *dpb, *p; 

    short dpb_length; 
    char *uname; // user-name. 
    char *upass; // password. 
    ISC_STATUS status_vector[20]; // Status vector, to monitor connection. 

    char *str = "/Users/Sumanth/Desktop/NewDB2.fdb"; 
    uname = "SYSDBA"; 
    upass = "masterkey"; 

    dpb = dpb_buffer; 

    // Specify the version of the parameter buffer, always the 
    // compile-time constant isc_dpb_version1. 
    *dpb++ = isc_dpb_version1; 

    // # of cache buffers to allocate for use with the database, 
    // default = // 75. In the API guide I think isc_dpb_num_buffers is 
    //specified as // isc_num_buffers, but that I could not get to work. 
    *dpb++ = isc_dpb_num_buffers; 
    *dpb++ = 1; 
    *dpb++ = 90; 

    *dpb++ = isc_dpb_user_name; // Save user-name in dpb. 
    *dpb++ = strlen(uname); 
    for (p = uname; *p;) 
     *dpb++ = *p++; 

    *dpb++ = isc_dpb_password; // Save password in dpb. 
    *dpb++ = strlen(upass); 
    for (p = upass; *p;) 
     *dpb++ = *p++; 

    dpb_length = dpb - dpb_buffer; 

    // Attach to the database. 
    isc_attach_database(status_vector, strlen(str), str, &db1,dpb_length, dpb_buffer); 
} 

回答

1

您可以設置環境變量ISC_USERISC_PASSWORD連接到Firebird數據庫,而不在應用程序中指定用戶名和密碼。請參閱Setting The ISC_USER And ISC_PASSWORD Environment Variables。請注意,鏈接的文檔只會談到SYSDBA帳戶,但它適用於任何帳戶。

火鳥書,由海倫·博裏第2版說:

It is possible to set up the two environment variables ISC_USER and ISC_PASSWORD on the server, to avoid the need to log in when working with databases locally. You will be able do everything that the named user is allowed to do, without needing to supply credentials each time.

文本「在服務器上」提到,但它實際上是本地客戶端應用程序。

InterBase的6.0操作指南(可從Firebird website)說:

If you do not provide a user name and password when you connect to a database or when you run utilities such as gbak, gstat, and gfix, InterBase looks to see if the ISC_USER and ISC_PASSWORD environment variables are set and uses that user and password as the InterBase user.

Although setting these environment variables is convenient, it is strongly not recommended if security is at all an issue.

+0

我試圖這樣做。我在終端中設置了這些憑據。但是,當我執行代碼時,它仍然要求憑據連接到數據庫 – user3723478

+0

@ user3723478您確定您的執行環境與您的終端共享這些環境變量嗎? –

+0

是的,我做得對。 – user3723478