目录
MySQL 数据插入
MySQL 数据删除
MySQL 数据更新
MySQL 数据查询
MySQL 联合查询
解决乱码问题
MySQL 数据插入
向MySQL数据表插入数据通用的 INSERT INTO SQL语法:
INSERT INTO table_name ( field1, field2,...fieldN ) VALUES ( value1, value2,...valueN );
如果数据是字符型,必须使用单引号或者双引号,如:"value"。
测试代码
#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", "", "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 = "INSERT INTO `hello` (`编号`) VALUES (\"9527\");";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 数据删除
以下是 SQL DELETE 语句从 MySQL 数据表中删除数据的通用语法:
DELETE FROM table_name WHERE Clause
- 如果没有指定 WHERE 子句,MySQL 表中的所有记录将被删除。
- 你可以在 WHERE 子句中指定任何条件
- 您可以在单个表中一次性删除记录。
- 当你想删除数据表中指定的记录时 WHERE 子句是非常有用的
测试代码
#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 = "INSERT INTO `hello` (`编号`) VALUES (\"9527\");";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 = "INSERT INTO `hello` (`编号`) VALUES (\"9528\");";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 = "DELETE FROM `hello` WHERE `编号`=\"9527\";";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 数据更新
以下是 UPDATE 命令修改 MySQL 数据表数据的通用 SQL 语法:
UPDATE table_name SET field1=new-value1, field2=new-value2,... WHERE Clause
- 你可以同时更新一个或多个字段。
- 你可以在 WHERE 子句中指定任何条件。
- 你可以在一个单独表中同时更新数据。
- 当你需要更新数据表中指定行的数据时 WHERE 子句是非常有用的。
WHERE里面可以使用AND以外,还可以使用OR,来合并多个条件,形成一个更为复杂的条件
代码测试
sql = "UPDATE `hello` SET age=18 WHERE `编号`=\"9528\";";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 = "UPDATE `hello` SET age=19 WHERE `age`>=35 AND `age`!=18;";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 数据查询
以下为在MySQL数据库中查询数据通用的 SELECT 语法:
SELECT table_name.column_name,column_name,... FROM table_name,table_name2,... WHERE Clause LIMIT N OFFSET M
查询语句中你可以使用一个或者多个表,表之间使用逗号(,)分割,并使用WHERE语句来设定查询条件。
- SELECT 命令可以读取一条或者多条记录。
- 你可以使用星号(*)来代替其他字段,SELECT语句会返回表的所有字段数据
- 你可以使用 WHERE 语句来包含任何条件。
- 你可以使用 LIMIT 属性来设定返回的记录数。
- 你可以通过OFFSET指定SELECT语句开始查询的数据偏移量。默认情况下偏移量为0。
mysql_use_result这个函数,执行后,并不会立刻查询,而是要等到mysql_fetch_row这个函数执行后,才会执行查询,mysql_store_result这个函数,执行后,会立刻查询,并且获取结果
测试代码
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);MYSQL_ROW row;do {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;}}} while (row != NULL);
}
int make_query(MYSQL* pDB)
{std::string sql = "SELECT * FROM `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;}MYSQL_RES* result = mysql_use_result(pDB);if (result != NULL) {show_result(result);std::cout << "===================================================" << std::endl;mysql_free_result(result);}return 0;
}
MySQL 联合查询
MySQL UNION 操作符语法格式:
SELECT expression1, expression2, ... expression_n FROM tables [WHERE conditions] UNION [ALL | DISTINCT] SELECT expression1, expression2, ... expression_n FROM tables [WHERE conditions];
参数:
- expression1, expression2, ... expression_n: 要检索的列。
- tables: 要检索的数据表。
- WHERE conditions: 可选, 检索条件。
- DISTINCT: 可选,删除结果集中重复的数据。默认情况下 UNION 操作符已经删除了重复数据,所以 DISTINCT 修饰符对结果没啥影响。
- ALL: 可选,返回所有结果集,包含重复数据。
测试代码
sql = "SELECT age FROM `hello` UNION ALL SELECT age FROM `teacher`;";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_store_result(mysql);if (result != NULL) {show_result(result);std::cout << "===================================================" << std::endl;mysql_free_result(result);}
解决乱码问题
控制台中,在C/C++中,setlocale()函数用于设置程序的本地环境,包括字符编码、日期时间格式、货币符号等。通过将LC_ALL参数设置为"en_GB.UTF-8",将控制台的本地编码设置为UTF-8。
数据库中,三个SQL语句 SET NAMES 'utf8'
、SET CHARACTER SET utf8
和 SET CHARACTER_SET_RESULT = utf8
是用于设置MySQL客户端与服务器之间的字符编码。
-
SET NAMES 'utf8'
:- 作用:设置客户端发送给服务器的字符编码。
- 示例:
SET NAMES 'utf8';
-
SET CHARACTER SET utf8
:- 作用:设置连接中的字符编码,包括发送和接收数据。
- 示例:
SET CHARACTER SET utf8;
-
SET CHARACTER_SET_RESULT = utf8
:- 作用:设置返回结果集的字符编码。
- 示例:
SET CHARACTER_SET_RESULT = utf8;
这些语句都用于确保在与MySQL服务器进行通信时使用正确的字符编码,以避免乱码问题。
代码文件中,文件-- .cpp另存为,设置代码文件的格式。
确保数据库、控制台、代码三方都是统一的编码
就可以正常的显示中文了