MySQL数据库 编程入门

目录

MySQL数据库数据类型

MySQL数据库命令执行

创建用户

创建删除库

创建删除表


MySQL数据库数据类型

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

整数类型:

  • TINYINT:有符号范围-128到127,无符号范围0到255。对应的C/C++类型为signed char或unsigned char。
  • SMALLINT:有符号范围-32768到32767,无符号范围0到65535。对应的C/C++类型为short或unsigned short。
  • INT:有符号范围-2147483648到2147483647,无符号范围0到4294967295。对应的C/C++类型为int或unsigned int。
  • BIGINT:有符号范围-9223372036854775808到9223372036854775807,无符号范围0到18446744073709551615。对应的C/C++类型为long long或unsigned long long。

浮点数类型:

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

字符串类型:

  • CHAR:固定长度字符串,最多255个字符。对应的C/C++类型为char数组。
  • VARCHAR:可变长度字符串,最多65535个字符。对应的C/C++类型为char数组或std::string。
  • TEXT:可变长度文本,最多65535个字符。对应的C/C++类型为char数组或std::string。

日期和时间类型:

  • DATE:日期,格式为YYYY-MM-DD。对应的C/C++类型为struct tm或std::string。
  • TIME:时间,格式为HH:MM:SS。对应的C/C++类型为struct tm或std::string。
  • DATETIME:日期和时间,格式为YYYY-MM-DD HH:MM:SS。对应的C/C++类型为struct tm或std::string。

其他类型:

  • 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 '密码';

范围包括:ip、localhost、%

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 
#include 

#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 
#include 

#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 
#include 

#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;
}

你可能感兴趣的:(MySQL,编程,数据库,mysql)