一.MySQl表的创建与删除
创建语句与删除语句
注意:
主键的数据类型,不可以是变长度类型需要()限制长度,表名字需要使用``符号来包围。
二.MySQl数据插入
三.MySQl数据删除
四.MySQl数据更新
使用语句
SET SQL_SAFE_UPDATES = 0;
show variables like 'SQL_SAFE_UPDATES';
当要哦
第一次修改后:
第二次修改后:
注意:WHERE里面可以使用AND以外,还可以使用OR,来合并多个条件,形成一个更为复杂的条件。
五 .MYSQL数据查询
附录:代码
#include <iostream>
#include <mysql.h>void show_result(MYSQL_RES* result) {//获取结果集列的数量unsigned nFiles = 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 < nFiles; j++) {//对于每一行遍历每列的信息std::cout << "type:" << fields[j].type << " " << fields[j].name << " : " << row[j] << std::endl; //对应存储数据的值}}std::cout << "==================" << std::endl;}}int creat_user(MYSQL* pDB){ //创建新用户std::string sql = "CREATE USER 'fc'@'localhost' IDENTIFIED BY ''";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;}return 0;
}int grant_userpower(MYSQL* pDB) { //授予用户权限std::string sql = "GRANT ALL PRIVILEGES ON *.* TO 'fc'@'localhost' with grant option";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;}return 0;
}int creat_database(MYSQL* 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;return -1;}return 0;
}int use_database(MYSQL* pDB) { //选择数据仓库std::string sql = "use 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;}return 0;
}int grant_database_power(MYSQL* pDB) { //授予仓库权限std::string sql = "GRANT ALL ON hello.* TO 'fc'@'localhost'";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;}return 0;
}int create_table(MYSQL* pDB) { //创建数据库的表std::string sql = "CREATE TABLE IF NOT EXISTS `hello` (`num` char(16) PRIMARY KEY)engine=InnoDB default charset=UTF8MB4;";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;}return 0;}int insert_data(MYSQL* pDB) { //插入数据库数据std::string sql = "insert into `hello` (`编号`) values (\"9527\");";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;}return 0;}int delete_data(MYSQL* pDB) { //删除所筛选的数据std::string sql = "delete from `hello` where `编号`=\"9527\";";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;}return 0;
}int up_data(MYSQL* pDB) { //修改所筛选的数据std::string sql = "update `hello` set age=30 where `编号`=\"9527\";";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;}return 0;
}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_store_result(pDB);if (result != NULL) {show_result(result); //展示数据mysql_free_result(result); //释放结果,与use_result相互对应}return 0;
}int fe() {setlocale(LC_ALL, "en_GB.UTF-8"); //设置控制台编码MYSQL* mysql = new MYSQL();MYSQL* pDB = mysql_init(mysql); //初始化mysql对象//这里两个变量 mysql和pDB 两个变量记录(指向)同一个值避免初始化失败后返回NULL指针内存泄漏的风险if (pDB == NULL) {std::cout << "mysql_init failed!" << std::endl;return -1;}pDB = mysql_real_connect(pDB, "localhost", "root", "1234", "mysql", 3306, NULL, 0);//连接数据库std::cout << pDB << std::endl;if (pDB) {//creat_user(pDB);//grant_userpower(pDB);//creat_database(pDB);//grant_database_power(pDB);//获取SQL的数据//use_database(pDB);//make_query(pDB);}mysql_close(pDB);delete(mysql);return 0;}int main()
{fe();return 0;}