Recently, I wrote a new MySQL connection. To facilitate flexible application, read MySQL connection parameters by reading the INI file.
By the way, I learned about the INI file.
INI format.
The INI file consists of multiple sections.
[Network] // node host = *. * // host: Key, IP: Value: User = rootpwd = // It is null if no data is written.
Read:
GetPrivateProfileString("NETWORK","host","",host.GetBuffer(MAX_PATH),MAX_PATH,strFileName);
The following parameters are listed in sequence: node name, key name, lpdefault, key value, key value length, and ini file path.
Lpdefault: If the INI file does not contain the field name or key name specified by the first two parameters, this value is assigned to the variable.
Return Value: key value.
Write:
WritePrivateProfileString("NETWORK","host","1.2.3.4",strFileName);
The following parameters are listed in sequence: node name, key name, key value, and ini file path.
Returned value: long. A value of non-zero indicates success, and a value of zero indicates failure.
File Path:
1. The path of the INI file must be complete, and directories at all levels before the file name must exist. Otherwise, the write operation fails and the function returns false.
2. The file name path must be \, because in VC ++, \ indicates \.
3. You can also put the INI file in the directory where the program is located. In this case, the lpfilename parameter is: ". \ student. ini ".
After the configuration file is written, connect:
mysql_real_connect(&mydata,host,user,pwd,database,port,unix_socket,clientflag);
// The parameters are obtained by reading the file.
The data is connected to a bunch of posts at night and will not be repeated. The MySQL connection base class has been written. Upload to the resource later.
Another important step is to connect remotely:
You can remotely set the server:
1. Run
Mysql-u root-P MySQL, the system prompts to enter the password of the Database Root User, enter the MySQL console after the input is complete, the first MySQL of this command is to execute the command, the second MySQL is the system data name, which is different.
2. On the MySQL console, run grant all privileges on *. * To 'root' @ '%' identified by 'mypassword' with grant option;
3. mysql>
Flush privileges; // make the modification take effect immediately
A 10060 connection failure error may occur. Check whether the firewall has disabled port 3306.