数据库的基本操作
首先进行表的创建:
sql = "CREATE TABLE IF NOT EXISTS `STUDENT`(`ID` int(8) PRIMARY KEY,`name` VARCHAR(16),`SEX` VARCHAR(16))"
数据插入
语法:
INSERT INTO table_name(field1…)VALUES(VALUE1…)
字符型必须使用单引号或双引号
代码实现:
sql = "INSERT INTO `student`(`ID`, `name`, `SEX`)VALUES(\"1\", \"zhangsan\", \"nan\")"
数据删除
语法:
DELETE FROM table_name WHERE clause
- 如果没有指定WHERE,那么整张表的内容将被删除
代码实现:
sql = "DELETE FROM `student` WHERE `SEX` = \"nv\"" //删除学生中性别为女的记录
数据更新
我们向表中添加新的一列Age
sql = "ALTER TABLE `student` add column `Age` int(8)"
语法:
UPDATE table_name SET field1 = new-value1…
WHERE clause
我们使用以下语句来对年龄进行更新
sql = "UPDATE `student` SET `Age` = 22 WHERE `ID` = 1";
sql = "UPDATE `student` SET `Age` = 23 WHERE `ID` = 3";
sql = "UPDATE `student` SET `Age` = 21 WHERE `ID` = 4";
使用WHERE语句进行更新
sql = "UPDATE `student` SET `Age` = 18 WHERE `Age` > 22";
WHERE里可以使用AND,OR来合并多个条件
数据查询
语法:
SELECT column_name…
FROM table_name…
WHERE clause
LIMIT N OFFSET M
LIMIT IN : 显示查询结果的前N条
OFFSET M : 表示数据的偏移量
查询函数代码:
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++) {
cout << fields[j].name << ":" << row[j] << "\t" << " ";
}
cout << endl;
}
} while (row != NULL);
}
int make_query(MYSQL* pDB) {
string sql = "SELECT * FROM `student`";
int ret = mysql_real_query(pDB, sql.c_str(), (unsigned long)sql.size());
if (ret != 0) {
cout << "error:" << mysql_error(pDB) << endl;
return -1;
}
MYSQL_RES* result = mysql_use_result(pDB);
if (result != NULL) {
show_result(result);
cout << "-----------------------------" << endl;
mysql_free_result(result);
}
return 0;
}
完整代码:
int select() {
MYSQL* mysql = new MYSQL();
MYSQL* pDB = mysql_init(mysql);
if (pDB == NULL) {
cout << "mysql_init failed" << endl;
return -1;
}
pDB = mysql_real_connect(pDB, "localhost", "root", "123456", "mysql", 3306, NULL, 0);
cout << pDB << endl;
if (pDB) {
string sql = "USE `hello`";
int ret = mysql_real_query(pDB, sql.c_str(), (unsigned long)sql.size());
if (ret != 0) {
cout << "error:" << mysql_error(pDB) << endl;
return -1;
}
make_query(pDB);
}
mysql_close(pDB);
delete mysql;
return 0;
}
int main() {
select();
}
在执行mysql_use_result时只是建立了链接,并不会立刻擦查询,要等到mysql_fetch_row执行后才会查询
mysql_store_result
该函数执行后会立刻进行查询并返回结果
联表查询
防止出现乱码:
setlocale(LC_ALL,“en_GB UTF-8”) 设置本地控制台编码
SET field_name UTF-8 设置数据库编码
保存文件时选择文件类型为无标签的UTF-8
语法:
SELECT… UNION[ALL|DISTINCT] SELECT…
ALL:全部显示
DISTINCT:删除重复数据
代码:
int make_query(MYSQL* pDB) {
string sql = "SELECT AGE FROM `student` UNION SELECT AGE FROM `teacher`";
int ret = mysql_real_query(pDB, sql.c_str(), (unsigned long)sql.size());
if (ret != 0) {
cout << "error:" << mysql_error(pDB) << endl;
return -1;
}
MYSQL_RES* result = mysql_use_result(pDB);
if (result != NULL) {
show_result(result);
cout << "-----------------------------" << endl;
mysql_free_result(result);
}
return 0;
}