# Include "mysql. h"
# Include <iostream>
# Include <string>
# Include <stdio. h>
Using namespace std;
Int main ()
{
MYSQL * SQL _conn = NULL;
MYSQL_RES * res_ptr = NULL;
MYSQL_ROW SQL _row;
SQL _conn = mysql_init (NULL );
If (SQL _conn = NULL )//
{
Cout <"init conect fail" <endl;
Return 0;
}
If (mysql_real_connect (SQL _conn, "192.168.1.223", "root", "123456", "myLearnSql", MYSQL_PORT, NULL, 0) = NULL)
{
Cout <"connect database myLearnSql fail" <endl;
Mysql_close (SQL _conn );
Return 0;
}
String strSqlQuery = "select f001v_0031, f002n_0031 from tb_0031 ";
Int ret = 0;
Ret = mysql_query (SQL _conn, strSqlQuery. c_str ());
If (ret! = 0)
{
Cout <"query:" <strSqlQuery <"fail" <endl;
Mysql_close (SQL _conn );
Return 0;
}
Res_ptr = mysql_use_result (SQL _conn );
If (res_ptr)
{
While (SQL _row = mysql_fetch_row (res_ptr )))
{
Printf ("query: f002n_0031 = % s, f001v_0031 = % s \ n", SQL _row [0], SQL _row [1]);
}
Mysql_free_result (res_ptr );
}
Mysql_close (SQL _conn );
Return 0;
}
Compile:
G ++-g-o mysql_main-I/usr/include/mysql/mysql_main.cpp-L/usr/lib/mysql/-lz
The following error occurs:
Mysql_main.cpp: 1: 19: error: mysql. h: No such file or directory
Mysql_main.cpp: In function 'int main (int, char **)':
Mysql_main.cpp: 9: error: 'mysql' was not declared in this scope
Mysql_main.cpp: 9: error: expected '; 'before' SQL _ conn'
Mysql_main.cpp: 10: error: 'SQL _ conn' was not declared in this scope
Mysql_main.cpp: 10: error: 'mysql _ init 'was not declared in this scope
Mysql_main.cpp: 15: error: 'SQL _ conn' was not declared in this scope
[Root @ localhost thread_learn] # whereis mysql
Mysql:/usr/bin/mysql/usr/lib/mysql/usr/include/mysql/usr/share/man/man1/mysql.1.gz
[Root @ localhost thread_learn] # find/-name mysql. h
/Usr/include/mysql. h
/Home/yuj/install-hxlt/mysql-5.1.37/include/mysql. h
Mysql. h already exists, but why cannot I find the file?
I know the reason. I used to write-I as l, which is why I is unfamiliar with the gcc compiler.
The complete command is as follows:
G ++-g-o mysql_main-I/usr/include/mysql mysql_main.cpp-L/usr/lib/mysql-lmysqlclient-lz
Connect to the mysqlclient Library