目录
数据库建模与建库建表
mysql数据库编程连接与插入数据
在 Node Server 服务器上通过SQL语句在数据库服务器上插入信息
mysql数据库的查询操作
mysql数据删除与存储过程调用
完整代码:
CREATE DATABASE GQ_DB; #创建数据库
SHOW DATABASES;
USE GQ_DB; #使用数据库
CREATE TABLE TBL_USER( #创建User表
U_ID INT PRIMARY KEY AUTO_INCREMENT,
U_NAME VARCHAR(32),
U_GENGDER VARCHAR(8)
);SHOW TABLES; #显示table
注意事项:写SQL语句的时候。a.建议大写 b.在每个表的前面加上TBL,数据库加DB。再做一个项目之前首先想到的就是数据库建模。
首先建立连接,上面传输的是SQL语句。
SELECT *FROM TBL_USER;
INSERT TBL_USER(U_NAME, U_GENGDER) VALUES('GQ', 'man'); #插入姓名性别
SELECT *FROM TBL_USER;
首先,需要在NS服务器上安装MySQL开发工具,在Ubuntu上执行下面命令:
sudo apt-get install libmysqlclient-dev
利用代码实现:
#include
#include
#include
#define GQ_DB_SERVER_IP "192.168.80.128"
#define GQ_DB_SERVER_PORT 3306
#define GQ_DB_SERVER_USERNAME "admin"
#define GQ_DB_SERVER_PASSWORD "521125"
#define GQ_DB_SERVER_DEFAULTBD "GQ_DB"
#define SQL_INSERT_TBL_USER "INSERT TBL_USER(U_NAME, U_GENGDER) VALUES('GQ', 'man'); "
//C U R D
int main(){
MYSQL mysql; //操作句柄 mysql | (管道)
if(NULL == mysql_init(&mysql)){
printf("mysql_init: %s\n", mysql_error(&mysql));
return -1;
}
if(!mysql_real_connect(&mysql, GQ_DB_SERVER_IP, GQ_DB_SERVER_USERNAME,
GQ_DB_SERVER_PASSWORD, GQ_DB_SERVER_DEFAULTBD, GQ_DB_SERVER_PORT, NULL, 0)){
printf("mysql_real_connect: %s\n", mysql_error(&mysql));
return -2;
}
//mysql --> insert
if(mysql_real_query(&mysql, SQL_INSERT_TBL_USER, strlen(SQL_INSERT_TBL_USER))){
printf("mysql_real_query: %s\n",mysql_error(&mysql));
}
mysql_close(&mysql);
return 0;
}
编译命令:
gcc -o mysql mysql.c -I /usr/include/mysql/ # -I加入系统头文件
gcc -o mysql mysql.c -I /usr/include/mysql/ -lmysqlclient # -lmysqlclient 引入编译所需库
代码结果:
我们修改第17行的信息.,增加用户信息。
#define SQL_INSERT_TBL_USER "INSERT TBL_USER(U_NAME, U_GENGDER) VALUES('GCJ', 'woman');
注意:值得关注第28行与第33行的if判断,Linux的API大部分返回0为成功(第42行是如此),而mysql提供的28行与33行两个接口恰好相反。
至少需要四个步骤:
int Gq_mysql_select(MYSQL *handle){
//mysql_real_query --> sql
if(mysql_real_query(handle, SQL_SELECT_TBL_USER, strlen(SQL_SELECT_TBL_USER))){
printf("mysql_real_query: %s\n",mysql_error(handle));
return -1;
}
//sotre -->
MYSQL_RES *res = mysql_store_result(handle);
if(res == NULL){
printf("mysql_store_result: %s\n", mysql_error(handle));
return -2;
}
//rows / fields
int rows = mysql_num_rows(res);
printf("rows: %d\n", rows);
int fields = mysql_num_fields(res);
printf("fields: %d\n", fields);
//fetch
MYSQL_ROW row;
while((row = mysql_fetch_row(res))){
int i = 0;
for(i = 0; i < fields; i++){
printf("%s\t", row[i]);
}
printf("\n");
}
mysql_free_result(res);
return 0;
}
代码运行结果:
在MYSQL workbench 中输入如下命令:
DELETE FROM TBL_USER WHERE U_NAME='GQ';
出现了下面错误: DELETE FROM TBL_USER WHERE U_NAME='GQ' Error Code: 1046. No database selected Select the default DB to be used by double-clicking its name in the SCHEMAS list in the sidebar.
版本5.6以后提供了按照主键删除的方式,U_NAME不是主键(key)。如果非要这么做我们可以设置成安全模式:
SET SQL_SAFE_UPDATES=0; #设置安全模式
DELETE FROM TBL_USER WHERE U_NAME='GQ';
SET SQL_SAFE_UPDATES=1;SELECT *FROM TBL_USER;
可以看到用户GQ被删除成功。
代码实现时,我们要将三条语句一起实现,可以但是容易出错,这里我们引入一个存储过程,定义一个存储过程:
DELIMITER @@ #定义标识符,存储过程以什么结束
CREATE PROCEDURE PRCO_DELETE_USER(IN UNAME VARCHAR(32))#创建存储过程
BEGIN
SET SQL_SAFE_UPDATES=0;
DELETE FROM TBL_USER WHERE U_NAME=UNAME;
SET SQL_SAFE_UPDATES=1;
END @@CALL PROC_DELETE_USER('GQ') #D调用存储过程
下面看如何在代码实现,定义一个存储过程。
#define SQL_DELETE_TBL_USER "CALL PROC_DELETE_USER('GQ')"
// mysql -->delete
printf("case: mysql -->delete\n");
#if 1
if(mysql_real_query(&mysql, SQL_DELETE_TBL_USER, strlen(SQL_DELETE_TBL_USER))){
printf("mysql_real_query: %s\n",mysql_error(&mysql));
}
#endif
我们先插入用户GQ再进行删除的结果:
#include
#include
#include
#define GQ_DB_SERVER_IP "192.168.80.128"
#define GQ_DB_SERVER_PORT 3306
#define GQ_DB_SERVER_USERNAME "admin"
#define GQ_DB_SERVER_PASSWORD "521125"
#define GQ_DB_SERVER_DEFAULTBD "GQ_DB"
#define SQL_INSERT_TBL_USER "INSERT TBL_USER(U_NAME, U_GENGDER) VALUES('GQ', 'man'); "
#define SQL_SELECT_TBL_USER "SELECT *FROM TBL_USER;"
#define SQL_DELETE_TBL_USER "CALL PROC_DELETE_USER('GQ')"
int Gq_mysql_select(MYSQL *handle){
//mysql_real_query --> sql
if(mysql_real_query(handle, SQL_SELECT_TBL_USER, strlen(SQL_SELECT_TBL_USER))){
printf("mysql_real_query: %s\n",mysql_error(handle));
return -1;
}
//sotre -->
MYSQL_RES *res = mysql_store_result(handle);
if(res == NULL){
printf("mysql_store_result: %s\n", mysql_error(handle));
return -2;
}
//rows / fields
int rows = mysql_num_rows(res);
printf("rows: %d\n", rows);
int fields = mysql_num_fields(res);
printf("fields: %d\n", fields);
//fetch
MYSQL_ROW row;
while((row = mysql_fetch_row(res))){
int i = 0;
for(i = 0; i < fields; i++){
printf("%s\t", row[i]);
}
printf("\n");
}
mysql_free_result(res);
return 0;
}
//C U R D
int main(){
MYSQL mysql; //操作句柄 mysql | (管道)
if(NULL == mysql_init(&mysql)){
printf("mysql_init: %s\n", mysql_error(&mysql));
return -1;
}
if(!mysql_real_connect(&mysql, GQ_DB_SERVER_IP, GQ_DB_SERVER_USERNAME,
GQ_DB_SERVER_PASSWORD, GQ_DB_SERVER_DEFAULTBD, GQ_DB_SERVER_PORT, NULL, 0)){
printf("mysql_real_connect: %s\n", mysql_error(&mysql));
return -2;
}
//mysql --> insert
printf("case: mysql --> insert\n");
#if 1
if(mysql_real_query(&mysql, SQL_INSERT_TBL_USER, strlen(SQL_INSERT_TBL_USER))){
printf("mysql_real_query: %s\n",mysql_error(&mysql));
}
#endif
Gq_mysql_select(&mysql);
// mysql -->delete
printf("case: mysql -->delete\n");
#if 1
if(mysql_real_query(&mysql, SQL_DELETE_TBL_USER, strlen(SQL_DELETE_TBL_USER))){
printf("mysql_real_query: %s\n",mysql_error(&mysql));
}
#endif
Gq_mysql_select(&mysql);
mysql_close(&mysql);
return 0;
}