MySQL中有多种数据类型,每种类型用于存储不同类型的数据。以下是MySQL中常见的数据类型:
数值类型:
字符串类型:
日期和时间类型:
布尔类型:
二进制类型:
此外,MySQL还支持其他特殊数据类型,如枚举类型(ENUM)和集合类型(SET),用于存储从预定义选项中选择的值。
多个数据库服务器可以集群工作
一个服务器上有多个数据库,一个数据库上有多个数据表
数据库:数据表的集合
MYSQL* mysql = new MYSQL();
MYSQL* pDB = mysql_init(mysql); //初始化mysql对象
//这里两个变量 mysql和pDB 两个变量记录(指向)同一个值避免初始化失败后返回NULL指针内存泄漏的风险
if (pDB == NULL) {
std::cout << "mysql_init failed!" << std::endl;
return -1;
}
pDB = mysql_real_connect(pDB, "localhost", "root", "1234", "mysql", 3306, NULL, 0);
补充:
int creat_user(MYSQL* pDB){ //创建新用户
std::string sql = "CREATE USER 'fc'@'localhost' IDENTIFIED BY ''";
int ret = mysql_real_query(pDB, sql.c_str(), (unsigned long)sql.size());
if (ret != 0) {
std::cout << "mysql error:" << mysql_error(pDB) << std::endl;
return -1;
}
return 0;
}
int grant_userpower(MYSQL* pDB) { //授予用户权限
std::string sql = "GRANT ALL PRIVILEGES ON *.* TO 'fc'@'localhost' with grant option";
int ret = mysql_real_query(pDB, sql.c_str(), (unsigned long)sql.size());
if (ret != 0) {
std::cout << "mysql error:" << mysql_error(pDB) << std::endl;
return -1;
}
return 0;
}
drop USER fc@localhost
int creat_database(MYSQL* pDB) { //创建数据仓库
std::string sql = "CREATE DATABASE hello";
int ret = mysql_real_query(pDB, sql.c_str(), (unsigned long)sql.size());
if (ret != 0) {
std::cout << "mysql error:" << mysql_error(pDB) << std::endl;
return -1;
}
return 0;
}
int grant_database_power(MYSQL* pDB) { //授予仓库权限
std::string sql = "GRANT ALL ON hello.* TO 'fc'@'localhost'";
int ret = mysql_real_query(pDB, sql.c_str(), (unsigned long)sql.size());
if (ret != 0) {
std::cout << "mysql error:" << mysql_error(pDB) << std::endl;
return -1;
}
return 0;
}
MYSQL_RES* result = mysql_use_result(mysql);
if (result != NULL) {
show_result(result); //展示数据
mysql_free_result(result); //释放结果,与use_result相互对应
}
void show_result(MYSQL_RES* result) {
//获取结果集列的数量
unsigned nFiles = mysql_num_fields(result);
//获取结果集行的数量
my_ulonglong nRows = mysql_num_rows(result);
//获取每一列的定义接口:
MYSQL_FIELD* fields = mysql_fetch_fields(result);
for (unsigned i = 0; i < nRows; i++) { //遍历每一行
MYSQL_ROW row = mysql_fetch_row(result);
//获取每一行结果
if (row != NULL) {
for (unsigned j = 0; j < nFiles; j++) {
//对于每一行遍历每列的信息
std::cout << "type:" << fields[j].type << " " << fields[j].name << " : " << row[j] << std::endl;
//对应存储数据的值
}
}
std::cout << "==================" << std::endl;
}
}
附录:
完整代码
#include
#include
void show_result(MYSQL_RES* result) {
//获取结果集列的数量
unsigned nFiles = mysql_num_fields(result);
//获取结果集行的数量
my_ulonglong nRows = mysql_num_rows(result);
//获取每一列的定义接口:
MYSQL_FIELD* fields = mysql_fetch_fields(result);
for (unsigned i = 0; i < nRows; i++) { //遍历每一行
MYSQL_ROW row = mysql_fetch_row(result);
//获取每一行结果
if (row != NULL) {
for (unsigned j = 0; j < nFiles; j++) {
//对于每一行遍历每列的信息
std::cout << "type:" << fields[j].type << " " << fields[j].name << " : " << row[j] << std::endl; //对应存储数据的值
}
}
std::cout << "==================" << std::endl;
}
}
int creat_user(MYSQL* pDB){ //创建新用户
std::string sql = "CREATE USER 'fc'@'localhost' IDENTIFIED BY ''";
int ret = mysql_real_query(pDB, sql.c_str(), (unsigned long)sql.size());
if (ret != 0) {
std::cout << "mysql error:" << mysql_error(pDB) << std::endl;
return -1;
}
return 0;
}
int grant_userpower(MYSQL* pDB) { //授予用户权限
std::string sql = "GRANT ALL PRIVILEGES ON *.* TO 'fc'@'localhost' with grant option";
int ret = mysql_real_query(pDB, sql.c_str(), (unsigned long)sql.size());
if (ret != 0) {
std::cout << "mysql error:" << mysql_error(pDB) << std::endl;
return -1;
}
return 0;
}
int creat_database(MYSQL* pDB) { //创建数据仓库
std::string sql = "CREATE DATABASE hello";
int ret = mysql_real_query(pDB, sql.c_str(), (unsigned long)sql.size());
if (ret != 0) {
std::cout << "mysql error:" << mysql_error(pDB) << std::endl;
return -1;
}
return 0;
}
int grant_database_power(MYSQL* pDB) { //授予仓库权限
std::string sql = "GRANT ALL ON hello.* TO 'fc'@'localhost'";
int ret = mysql_real_query(pDB, sql.c_str(), (unsigned long)sql.size());
if (ret != 0) {
std::cout << "mysql error:" << mysql_error(pDB) << std::endl;
return -1;
}
return 0;
}
int fe() {
MYSQL* mysql = new MYSQL();
MYSQL* pDB = mysql_init(mysql); //初始化mysql对象
//这里两个变量 mysql和pDB 两个变量记录(指向)同一个值避免初始化失败后返回NULL指针内存泄漏的风险
if (pDB == NULL) {
std::cout << "mysql_init failed!" << std::endl;
return -1;
}
pDB = mysql_real_connect(pDB, "localhost", "root", "1234", "mysql", 3306, NULL, 0);
//连接数据库
std::cout << pDB << std::endl;
if (pDB) {
//creat_user(pDB);
//grant_userpower(pDB);
//creat_database(pDB);
//grant_database_power(pDB);
//获取SQL的数据
MYSQL_RES* result = mysql_use_result(mysql);
if (result != NULL) {
show_result(result); //展示数据
mysql_free_result(result); //释放结果,与use_result相互对应
}
}
mysql_close(pDB);
delete(mysql);
return 0;
}
int main()
{
fe();
return 0;
}