目录

MySQL数据库数据类型

MySQL数据库命令执行

创建用户

创建删除库

创建删除表


MySQL数据库数据类型

MySQL数据库定义了多种数据类型,下面是一些常见的MySQL数据类型及其对应的C/C++类型

整数类型

浮点数类型:

  • FLOAT:单精度浮点数。对应的C/C++类型为float
  • DOUBLE:双精度浮点数。对应的C/C++类型为double。

字符串类型:

日期时间类型:

其他类型:

  • BOOLEAN:布尔类型,值为TRUE或FALSE。对应的C/C++类型为bool。
  • BLOB:二进制对象,最多65535字节。对应的C/C++类型为unsigned char数组。

MySQL数据库命令执行

执行命令过程

  1. 发送SQL命令
  2. 获取SQL执行结果
  3. 解析获取到的结果

发送SQL命令接口

int STDCALL mysql_real_query(MYSQL *mysql, const char *q, unsigned long length);

q是SQL指令length是指令的长度;向MySQL服务器发送SQL指令

获取SQL执行结果接口

MYSQL_RES *STDCALL mysql_use_result(MYSQL *mysql);

释放结果接口

void STDCALL mysql_free_result(MYSQL_RES *result);

获取结果集列的数量

unsigned int STDCALL mysql_num_fields(MYSQL_RES *res);

获取结果集行的数量

my_ulonglong STDCALL mysql_num_rows(MYSQL_RES *res);

取结果中的行接口

MYSQL_ROW STDCALL mysql_fetch_row(MYSQL_RES *result);

获取每一行的长度接口

​​​​​​​unsigned long *STDCALL mysql_fetch_lengths(MYSQL_RES *result);

获取结果集中每一列的定义接口

MYSQL_FIELD *STDCALL mysql_fetch_fields(MYSQL_RES *res);

创建用户

CREATE USER '用户名'@'范围' IDENTIFIED BY '密码';

范围包括:iplocalhost、%

ip即只能从指定地址登录localhost 即只能从本机登录;% 即可以从任何地址登录

光创建用户,并没有什么作用,还需要授权操作

GRANT privileges ON databasename.tablename TO '用户名'@'范围'

privileges可以是:

SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER,EXECUTE,INDEX,REFERENCES

ALTER ROUTINE,CREATE ROUTINE,CREATE TEMPORARY,SHOW VIEW,LOCK TABLES

ALL

databasename是库的名称可以填入*表示所有

tablename是表的名称,可以填入*表示所有

有授权自然有撤销权限

REVOKE 权限 ON databasename.tablename FROM '用户名'@'范围'
#include <iostream>
#include <mysql.h>

#pragma comment(lib,"libmysql.lib")

int main()
{
	MYSQL* mysql = new MYSQL();
	//MYSQL mysql;//占用的是栈上的内存
	MYSQL* pDB = mysql_init(mysql);
	if (pDB == NULL) {
		std::cout << "mysql_init failed!" << std::endl;
		return -1;
	}
	pDB = mysql_real_connect(pDB, "localhost", "root", "123456", "mysql", 3306, NULL, 0);
	std::cout << pDB << std::endl;
	if (pDB) {
		std::string sql = "CREATE USER 'hello'@'localhost' IDENTIFIED BY '123456'";
		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;
		}
		MYSQL_RES* result = mysql_use_result(mysql);
		if (result != NULL) {
			unsigned nFields = 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 < nFields; j++) {
						std::cout << "type:" << fields[j].type << " " << fields[j].name << ":" << row[j] << std::endl;
					}
				}
				std::cout << "===================================================" << std::endl;
			}
			mysql_free_result(result);
		}

		sql = "GRANT ALL ON *.* TO 'hello'@'localhost'";
		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;
		}
		result = mysql_use_result(mysql);
		if (result != NULL) {
			unsigned nFields = 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 < nFields; j++) {
						std::cout << "type:" << fields[j].type << " " << fields[j].name << ":" << row[j] << std::endl;
					}
				}
				std::cout << "===================================================" << std::endl;
			}
			mysql_free_result(result);
		}
		mysql_close(pDB);
	}

	delete mysql;
}

创建删除

MySQL如何创建数据仓库
CREATE DATABASE 数据仓库名;
数据库名称不要使用奇怪的或者特殊的符号,例如空格路径符、引号

MySQL如何指派仓库权限给用户
GRANT 权限 ON databasename.tablename TO ‘用户名’@’范围’

删除
DROP DATABASE 数据仓库名;

切换数据库
USE 数据仓库名;

用户、数据库、表等等,这些元素的创建,一般是不会有返回结果
仅仅有一个返回值,标明执行的结果是成功还是失败!

测试代码

#include <iostream>
#include <mysql.h>

#pragma comment(lib,"libmysql.lib")

void show_result(MYSQL_RES* result)
{
	unsigned nFields = 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 < nFields; j++) {
				std::cout << "type:" << fields[j].type << " " << fields[j].name << ":" << row[j] << std::endl;
			}
		}
	}
}


int main()
{
	MYSQL* mysql = new MYSQL();
	//MYSQL mysql;//占用的是栈上的内存
	MYSQL* pDB = mysql_init(mysql);
	if (pDB == NULL) {
		std::cout << "mysql_init failed!" << std::endl;
		return -1;
	}
	pDB = mysql_real_connect(pDB, "localhost", "root", "123456", "mysql", 3306, NULL, 0);
	std::cout << pDB << std::endl;
	if (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;
		}
		MYSQL_RES* result = mysql_use_result(mysql);
		if (result != NULL) {
			show_result(result);
			std::cout << "===================================================" << std::endl;
			mysql_free_result(result);
		}
		//授予权限
		sql = "GRANT ALL ON hello.* TO 'hello'@'localhost';";
		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;
		}
		result = mysql_use_result(mysql);
		if (result != NULL) {
			show_result(result);
			std::cout << "===================================================" << std::endl;
			mysql_free_result(result);
		}


		sql = "USE hello;";
		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;
		}
		result = mysql_use_result(mysql);
		if (result != NULL) {
			show_result(result);
			std::cout << "===================================================" << std::endl;
			mysql_free_result(result);
		}

		sql = "DROP DATABASE hello;";
		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;
		}
		result = mysql_use_result(mysql);
		if (result != NULL) {
			show_result(result);
			std::cout << "===================================================" << std::endl;
			mysql_free_result(result);
		}
		mysql_close(pDB);
	}

	delete mysql;
	return 0;
}

创建删除

CREATE TABLE IF NOT EXISTS `表名称` (
`列名称1` 数据类型 关键字列表 默认值
...
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

关键字和含义如下:

  1. AUTO_INCREMENT 自动增加,只能用于整数类型列
  2. NOT NULL 列不能为空
  3. NULL 列可以为空
  4. PRIMARY KEY 主键 表中主键的值是唯一的,可以用于标记查找数据
  5. UNSIGNED 无符号的数,只能修饰整数类型列
  6. DEFAULT 默认值  用于指定列的默认值
  7. UNIQUE 唯一 该列的要么为NULL要么就必须是唯一的,不可重复
  8. ZEROFILL  0值填充,如果没有指定该列的值,则会填入0

以下为删除MySQL数据表的通用语法

DROP TABLE `表名称` ;

主键数据类型,它不能是可变长度的类型,例如:TEXT、BLOB

表名需要使用“符号来包围,千万不能使用单引号

测试代码

#include <iostream>
#include <mysql.h>

#pragma comment(lib,"libmysql.lib")

void show_result(MYSQL_RES* result)
{
	unsigned nFields = 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 < nFields; j++) {
				std::cout << "type:" << fields[j].type << " " << fields[j].name << ":" << row[j] << std::endl;
			}
		}
	}
}


int main()
{
	MYSQL* mysql = new MYSQL();
	//MYSQL mysql;//占用的是栈上的内存
	MYSQL* pDB = mysql_init(mysql);
	if (pDB == NULL) {
		std::cout << "mysql_init failed!" << std::endl;
		return -1;
	}
	pDB = mysql_real_connect(pDB, "localhost", "root", "123456", "mysql", 3306, NULL, 0);
	std::cout << pDB << std::endl;
	if (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;
		}
		MYSQL_RES* result = mysql_use_result(mysql);
		if (result != NULL) {
			show_result(result);
			std::cout << "===================================================" << std::endl;
			mysql_free_result(result);
		}
		//授予权限
		sql = "GRANT ALL ON hello.* TO 'hello'@'localhost';";
		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;
		}
		result = mysql_use_result(mysql);
		if (result != NULL) {
			show_result(result);
			std::cout << "===================================================" << std::endl;
			mysql_free_result(result);
		}


		sql = "USE hello;";
		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;
		}
		result = mysql_use_result(mysql);
		if (result != NULL) {
			show_result(result);
			std::cout << "===================================================" << std::endl;
			mysql_free_result(result);
		}

		sql = "CREATE TABLE IF NOT EXISTS `hello` (`编号` NVARCHAR(16) PRIMARY KEY)ENGINE=InnoDB DEFAULT CHARSET=utf8;";
		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;
		}
		sql = "DROP TABLE `hello`;";
		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;
		}
		sql = "DROP DATABASE hello;";
		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;
		}
		mysql_close(pDB);
	}

	delete mysql;
	return 0;
}

原文地址:https://blog.csdn.net/qq_61553520/article/details/134649842

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_6303.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注