mysql> create database conn;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| conn |
+--------------------+
2 rows in set (0.00 sec)
mysql> use conn
Database changed
mysql> create table user( id int primary key auto_increment, name varchar(32) not null, age int not null, telphone varchar(32) unique);
Query OK, 0 rows affected (0.06 sec)
mysql> show tables;
+----------------+
| Tables_in_conn |
+----------------+
| user |
+----------------+
1 row in set (0.00 sec)
mysql> desc user;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(32) | NO | | NULL | |
| age | int(11) | NO | | NULL | |
| telphone | varchar(32) | YES | UNI | NULL | |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
#include
#include
#include
const std::string host = "127.0.0.1";
const std::string user = "connector";
const std::string password = "123456";
const std::string db = "conn";
const unsigned int port = 3306;
int main()
{
// std::cout<<"mysql client version: "<
MYSQL *my = mysql_init(nullptr);
if (nullptr == my)
{
std::cerr << " init MYSQL error" << std::endl;
return 1;
}
if (mysql_real_connect(my, host.c_str(), user.c_str(), password.c_str(), db.c_str(), port, nullptr, 0) == nullptr)
{
std::cerr << " connect MYSQL error" << std::endl;
return 2;
}
mysql_set_character_set(my, "utf8");
// std::cout<<"connect success "<
// std::string sql="insert into user(name,age,telphone) values('jim',18,'123488899');";
std::string sql="update user set name='李四' where id =2;";
// std::string sql = "delete from user where id =3; ";
//std::string sql = "select * from user";
int n = mysql_query(my, sql.c_str());
if (n == 0)
std::cout << sql << " success " << std::endl;
else
{
std::cerr << "filed :" << sql << std::endl;
return 3;
}
mysql_close(my);
return 0;
}
MYSQL *mysql_init(MYSQL *mysql);
如: MYSQL *mfp = mysql_init(nullptr)
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 clientflag);
所需参数分别是 mysql句柄 ,主机ip,sql用户名,sql密码,数据库,端口号,剩余两个填nullptr和0即可
如:
if(mysql_real_connect(my,host.c_str(),user.c_str(),password.c_str(), db.c_str(), port, nullptr, 0) == nullptr)
{
std::cerr << " connect MYSQL error" << std::endl;
return 2;
}
mysql_set_character_set(myfd, "utf8");
int mysql_query(MYSQL *mysql, const char *q);
MYSQL_RES *mysql_store_result(MYSQL *mysql);
如:
//把结果存到结果集中
MYSQL_RES *res = mysql_store_result(my);
if (nullptr == res)
{
std::cerr << " mysql_store_result error" << std::endl;
return 4;
}
my_ulonglong mysql_num_rows(MYSQL_RES *res);
unsigned int mysql_num_fields(MYSQL_RES *res);
MYSQL_FIELD *mysql_fetch_fields(MYSQL_RES *res);
如:
my_ulonglong row = mysql_num_rows(res);
my_ulonglong col = mysql_num_fields(res);
std::cout << "行: " << row << std::endl;
std::cout << "列: " << col << std::endl;
std::cout<<std::endl;
//属性
MYSQL_FIELD* fields=mysql_fetch_fields(res);
for(int i=0;i<col;++i)
{
std::cout<< fields[i].name<<"\t";
}
std::cout<<std::endl;
MYSQL_ROW mysql_fetch_row(MYSQL_RES *result);
如:
//内容
for (int i = 0; i < row; ++i)
{
MYSQL_ROW row = mysql_fetch_row(res);
for (int j = 0; j < col; ++j)
{
std::cout << row[j] << "\t";
}
std::cout << "\n";
}
std::cout<<fields[0].db<<std::endl;
std::cout<<fields[0].table<<std::endl;
void mysql_free_result(MYSQL_RES *result)
void mysql_close(MYSQL *sock);
#include
#include
#include
const std::string host = "127.0.0.1";
const std::string user = "connector";
const std::string password = "123456";
const std::string db = "conn";
const unsigned int port = 3306;
int main()
{
// std::cout<<"mysql client version: "<
MYSQL *my = mysql_init(nullptr);
if (nullptr == my)
{
std::cerr << " init MYSQL error" << std::endl;
return 1;
}
if (mysql_real_connect(my, host.c_str(), user.c_str(), password.c_str(), db.c_str(), port, nullptr, 0) == nullptr)
{
std::cerr << " connect MYSQL error" << std::endl;
return 2;
}
mysql_set_character_set(my, "utf8");
// std::cout<<"connect success "<
// std::string sql="insert into user(name,age,telphone) values('jim',18,'123488899');";
// std::string sql="update user set name='李四' where id =2;";
// std::string sql = "delete from user where id =3; ";
std::string sql = "select * from user";
int n = mysql_query(my, sql.c_str());
if (n == 0)
std::cout << sql << " success " << std::endl;
else
{
std::cerr << "filed :" << sql << std::endl;
return 3;
}
//把结果存到结果集中
MYSQL_RES *res = mysql_store_result(my);
if (nullptr == res)
{
std::cerr << " mysql_store_result error" << std::endl;
return 4;
}
my_ulonglong row = mysql_num_rows(res);
my_ulonglong col = mysql_num_fields(res);
std::cout << "行: " << row << std::endl;
std::cout << "列: " << col << std::endl;
std::cout<<std::endl;
//属性
MYSQL_FIELD* fields=mysql_fetch_fields(res);
for(int i=0;i<col;++i)
{
std::cout<< fields[i].name<<"\t";
}
std::cout<<std::endl;
//内容
for (int i = 0; i < row; ++i)
{
MYSQL_ROW row = mysql_fetch_row(res);
for (int j = 0; j < col; ++j)
{
std::cout << row[j] << "\t";
}
std::cout << "\n";
}
std::cout<<fields[0].db<<std::endl;
std::cout<<fields[0].table<<std::endl;
mysql_free_result(res);
mysql_close(my);
return 0;
}