程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> mysql中的mysql_real_connect連接參數設置_MySQL教程

mysql中的mysql_real_connect連接參數設置_MySQL教程

編輯:關於MYSQL數據庫

在前一篇文章中,講述了如何進行MySQL源程序代碼的編譯鏈接,但是沒有講述運行情況,在按照上一篇文章代碼下進行編譯運行後,發現無法鏈接數據庫文件,顯然是在mysql_real_connect()函數中出現了問題。在mysql的英文手冊中找到關於MySQL_real_connect()的如下描述:
     
//函數原型描述 MYSQL *mysql_real_connect(MYSQL *MySQL, const char *host, const char *user,
const char *passwd, const char *db, unsigned int port, const char *unix_socket,
unsigned long clIEnt_flag)

Description

mysql_real_connect() attempts to establish a connection to a MySQL database engine
running on host. MySQL_real_connect() must complete successfully before you can
execute any other API functions that require a valid MySQL connection handle structure.

The parameters are specifIEd as follows:

    *

      The first parameter should be the address of an existing MySQL structure. Before
 calling mysql_real_connect() you must call mysql_init() to initialize the MySQL
structure. You can change a lot of connect options with the MySQL_options() call.
See Section 17.2.3.47, “MySQL_options()”.
    *

      The value of host may be either a hostname or an IP address. If host is NULL or the
string "localhost", a connection to the local host is assumed. If the OS supports sockets
(Unix) or named pipes (Windows), they are used instead of TCP/IP to connect to the server.
    *

      The user parameter contains the user's MySQL login ID. If user is NULL or the empty
string "", the current user is assumed. Under Unix, this is the current login name. Under
 Windows ODBC, the current username must be specifIEd explicitly. See Section 18.1.9.2,
 Â“Configuring a MyODBC DSN on Windows”.
    *

      The passwd parameter contains the passWord for user. If passwd is NULL, only entrIEs
 in the user table for the user that have a blank (empty) passWord fIEld are checked for a
match. This allows the database administrator to set up the MySQL privilege system in
such a way that users get different privileges depending on whether they have specifIEd
a passWord.

      Note: Do not attempt to encrypt the passWord before calling MySQL_real_connect();
 passWord encryption is handled automatically by the clIEnt API.
    *

      db is the database name. If db is not NULL, the connection sets the default database
to this value.
    *

      If port is not 0, the value is used as the port number for the TCP/IP connection. Note
that the host parameter determines the type of the connection.
    *

      If unix_socket is not NULL, the string specifIEs the socket or named pipe that should
 be used. Note that the host parameter determines the type of the connection.
    *

      The value of clIEnt_flag is usually 0, but can be set to a combination of the following
flags to enable certain features:

//上面描述了五個參數的主要取值,MYSQL *為mysql_init函數返回的指針,host為null或              // localhost時鏈接的是本地的計算機,當mysql默認安裝在unix(或類unix)系統中,root賬戶是沒// 有密碼的,因此用戶名使用root,密碼為null,當db為空的時候,函數鏈接到默認數據庫,在進行  // MySQL安裝時會存在默認的test數據庫,因此此處可以使用test數據庫名稱,port端口為0,使用    // unix連接方式,unix_socket為null時,表明不使用socket或管道機制,最後一個參數經常設置為0

      Flag Name    Flag Description
      CLIENT_COMPRESS    Use compression protocol.
      CLIENT_FOUND_ROWS    Return the number of found (matched) rows, not the number of
 changed rows.
      CLIENT_IGNORE_SPACE    Allow spaces after function names. Makes all functions names
reserved Words.
      CLIENT_INTERACTIVE    Allow interactive_timeout seconds (instead of wait_timeout
seconds) of inactivity before closing the connection. The clIEnt's session wait_timeout
variable is set to the value of the session interactive_timeout variable.
      CLIENT_LOCAL_FILES    Enable LOAD DATA LOCAL handling.
      CLIENT_MULTI_STATEMENTS    Tell the server that the clIEnt may send multiple
statements in a single string (separated by ‘;’). If this flag is not set,
multiple-statement execution is disabled. Added in MySQL 4.1.

    CLIENT_MULTI_RESULTS    Tell the server that the clIEnt can handle multiple result
sets from multiple-statement executions or stored procedures. This is automatically
set if CLIENT_MULTI_STATEMENTS is set. Added in MySQL 4.1.
      CLIENT_NO_SCHEMA    Don't allow the db_name.tbl_name.col_name syntax. This is for
ODBC. It causes the parser to generate an error if you use that syntax, which is useful
 for trapping bugs in some ODBC programs.
      CLIENT_ODBC    The client is an ODBC clIEnt. This changes MySQLd to be more
 ODBC-frIEndly.
      CLIENT_SSL    Use SSL (encrypted protocol). This option should not be set by
application programs; it is set internally in the clIEnt library. Instead, use
mysql_ssl_set() before calling MySQL_real_connect().

For some parameters, it is possible to have the value taken from an option file rather
than from an explicit value in the MySQL_real_connect() call. To do this, call
mysql_options() with the MYSQL_READ_DEFAULT_FILE or MySQL_READ_DEFAULT_GROUP option
before calling mysql_real_connect(). Then, in the MySQL_real_connect() call, specify
the “no-value” value for each parameter to be read from an option file:

    *

      For host, specify a value of NULL or the empty string ("").
    *

      For user, specify a value of NULL or the empty string.
    *

      For passwd, specify a value of NULL. (For the passWord, a value of the empty string in
 the MySQL_real_connect() call cannot be overridden in an option file, because the empty
string indicates explicitly that the MySQL account must have an empty passWord.)
    *

      For db, specify a value of NULL or the empty string.
    *

      For port, specify a value of 0.
    *

      For unix_socket, specify a value of NULL.

If no value is found in an option file for a parameter, its default value is used as
indicated in the descriptions given earlIEr in this section.

Return Values

A MySQL* connection handle if the connection was successful, NULL if the connection
was unsuccessful. For a successful connection, the return value is the same as the value
of the first parameter.
// 返回值:當連接成功時,返回MySQL連接句柄,失敗,返回NULL。當成功時,返回值與第一個參數值是// 相同的。

Errors

    *

      CR_CONN_HOST_ERROR

      Failed to connect to the MySQL Server.
    *

      CR_CONNECTION_ERROR

      Failed to connect to the local MySQL Server.
    *

      CR_IPSOCK_ERROR

      Failed to create an IP socket.
    *

      CR_OUT_OF_MEMORY

      Out of memory.
    *

      CR_SOCKET_CREATE_ERROR

      Failed to create a Unix socket.
    *

      CR_UNKNOWN_HOST

      Failed to find the IP address for the hostname.
    *

      CR_VERSION_ERROR

      A protocol mismatch resulted from attempting to connect to a server with a clIEnt
library that uses a different protocol version. This can happen if you use a very old
clIEnt library to connect to a new server that wasn't started with the --old-protocol
option.
    *

      CR_NAMEDPIPEOPEN_ERROR

      Failed to create a named pipe on Windows.
    *

      CR_NAMEDPIPEWAIT_ERROR

      Failed to wait for a named pipe on Windows.
    *

      CR_NAMEDPIPESETSTATE_ERROR

      Failed to get a pipe handler on Windows.
    *

      CR_SERVER_LOST

      If connect_timeout > 0 and it took longer than connect_timeout seconds to connect to
the server or if the server dIEd while executing the init-command.


    因此MySQL_real_connect()函數調用為:
       mysql_real_connect(MySQL,"localhost","root",NULL,"test",0,NULL,0);
判斷是否出錯,出錯調用mysql_error()函數顯示出錯信息,或使用MySQL_errno()函數獲取出錯代號。


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved