目录
MySQL数据库数据类型
MySQL数据库命令执行
创建用户
创建删除库
创建删除表
MySQL数据库定义了多种数据类型,下面是一些常见的MySQL数据类型及其对应的C/C++类型:
整数类型:
浮点数类型:
字符串类型:
日期和时间类型:
其他类型:
执行命令的过程:
发送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;
关键字和含义如下:
以下为删除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;
}