目录
MySQL 数据插入
MySQL 数据删除
MySQL 数据更新
MySQL 数据查询
MySQL 联合查询
解决乱码问题
向MySQL数据表插入数据通用的 INSERT INTO SQL语法:
INSERT INTO table_name ( field1, field2,...fieldN ) VALUES ( value1, value2,...valueN );
如果数据是字符型,必须使用单引号或者双引号,如:"value"。
测试代码
#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", "", "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;
}
以下是 SQL DELETE 语句从 MySQL 数据表中删除数据的通用语法:
DELETE FROM table_name WHERE Clause
测试代码
#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 = "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;
}
以下是 UPDATE 命令修改 MySQL 数据表数据的通用 SQL 语法:
UPDATE table_name SET field1=new-value1, field2=new-value2,... WHERE Clause
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数据库中查询数据通用的 SELECT 语法:
SELECT table_name.column_name,column_name,... FROM table_name,table_name2,... WHERE Clause LIMIT N OFFSET M
查询语句中你可以使用一个或者多个表,表之间使用逗号(,)分割,并使用WHERE语句来设定查询条件。
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 UNION 操作符语法格式:
SELECT expression1, expression2, ... expression_n FROM tables [WHERE conditions] UNION [ALL | DISTINCT] SELECT expression1, expression2, ... expression_n FROM tables [WHERE conditions];
参数:
测试代码
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另存为,设置代码文件的格式。
确保数据库、控制台、代码三方都是统一的编码
就可以正常的显示中文了