2014-06-10 83 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