数据库初始化
//链接数据库
my_con = mysql_init(NULL);
mysql_real_connect(my_con,MYSQL_SERVER,MYSQL_USER,MYSQL_PASSWD,NULL,0,NULL,0));
//创建数据库文件 smart_home
mysql_query(my_con, "create database if not exists smart_home"));
//选取数据库文件 smart_home
mysql_query(my_con, "use smart_home"));
//创建数据库表 users
mysql_query(my_con, "create table if not exists users(username VARCHAR(20),password VARCHAR(20))")) ;
//创建数据库表 hardware
mysql_query(my_con, "create table if not exists SH_hardware(SH_flag VARCHAR(20),SH_time VARCHAR(40), SH_name VARCHAR(20), SH_tmp VARCHAR(20), SH_damp VARCHAR(20), SH_fan VARCHAR(20), SH_buz VARCHAR(20), SH_led VARCHAR(20),SH_camare VARCHAR(20))"));
//创建手机终端连接表 client_fd
mysql_query(my_con, "create table if not exists client_fd(client_fd int(8))"));
数据库操作
增:
char sql_insert[200];
sprintf(sql_insert, "INSERT INTO client_fd(client_fd) values(‘%d‘);",client_fd);
mysql_query(my_con, sql_insert);
删:
char sql_insert[200];
sprintf(sql_insert, "DELETE from client_fd WHERE client_fd=‘%d‘);",client_fd);
mysql_query(my_con, sql_insert);
查:
int iTableRow;
MYSQL_RES *res_ptr;
MYSQL_ROW sqlrow;
res = mysql_query(my_con, "select username,password from users"); //查询语句
res_ptr = mysql_store_result(my_con); //取出结果集
if(res_ptr) {
iTableRow = mysql_num_rows(res_ptr);//行
//iTableCol = mysql_num_fields(res_ptr);//列
for(i=0; i<iTableRow; i++){
sqlrow = mysql_fetch_row(res_ptr);
if( (strcmp(username,sqlrow[0])==0 ) && (strcmp(password,sqlrow[1])==0)){
。。。。。。
}
}
}
mysql_free_result(res_ptr);
}
实例
void start_mysql_init(){
//链接数据库
my_con = mysql_init(NULL);
if(!mysql_real_connect(my_con,MYSQL_SERVER,MYSQL_USER,MYSQL_PASSWD,NULL,0,NULL,0)){
fprintf(stderr, "%s\n", mysql_error(my_con));
exit(1);
}
printf("mysql Connect!\n");
printf("mysql version :%s\n",mysql_get_client_info());
//创建数据库文件 smart_home
if(mysql_query(my_con, "create database if not exists smart_home")) {
printf("Error %u: %s\n", mysql_errno(my_con), mysql_error(my_con));
exit(1);
}
//选取数据库文件 smart_home
if(mysql_query(my_con, "use smart_home")) {
printf("Error %u: %s\n", mysql_errno(my_con), mysql_error(my_con));
exit(1);
}
//创建数据库表 users
if(mysql_query(my_con, "create table if not exists users(username VARCHAR(20),password VARCHAR(20))")) {
printf("Error %u: %s\n", mysql_errno(my_con), mysql_error(my_con));
exit(1);
}
//创建数据库表 hardware
if(mysql_query(my_con, "create table if not exists SH_hardware(SH_flag VARCHAR(20),SH_time VARCHAR(40), SH_name VARCHAR(20), SH_tmp VARCHAR(20), SH_damp VARCHAR(20), SH_fan VARCHAR(20), SH_buz VARCHAR(20), SH_led VARCHAR(20),SH_camare VARCHAR(20))")) {
printf("Error %u: %s\n", mysql_errno(my_con), mysql_error(my_con));
exit(1);
}
//创建手机终端连接表 client_fd
if(mysql_query(my_con, "create table if not exists client_fd(client_fd int(8))")) {
printf("Error %u: %s\n", mysql_errno(my_con), mysql_error(my_con));
exit(1);
}
}
增:
// SH_hardware: SH_flag VARCHAR(20) SH_time VARCHAR(20) SH_name VARCHAR(20) SH_tmp VARCHAR(20)
// SH_damp VARCHAR(20) SH_fan VARCHAR(20) SH_buz VARCHAR(20) SH_led VARCHAR(20)
// SH_camare VARCHAR(20)
void sql_insert_SHhardware(const char *SH_flag,const char *SH_time,const char *SH_name,const char *SH_tmp,const char *SH_damp,const char *SH_fan,const char *SH_buz,const char *SH_led, const char *SH_camare){
char sql_insert[200];
sprintf(sql_insert, "INSERT INTO SH_hardware(SH_flag,SH_time,SH_name,SH_tmp,SH_damp,SH_fan,SH_buz,SH_led,SH_camare) values(‘%s‘,‘%s‘,‘%s‘,%s,‘%s‘,‘%s‘,‘%s‘,‘%s‘,‘%s‘);", SH_flag, SH_time,SH_name,SH_tmp,SH_damp,SH_fan,SH_buz,SH_led,SH_camare);
int res = mysql_query(my_con, sql_insert);
if (res) {
fprintf(stderr, "Insert error %d: %s\n", mysql_errno(my_con), mysql_error(my_con));
}
}
查:
int sql_select_reg(const char *username){
int res, i;
int iTableRow;
MYSQL_RES *res_ptr;
MYSQL_ROW sqlrow;
res = mysql_query(my_con, "select username,password from users"); //查询语句
if (res) {
printf("SELECT error:%s\n",mysql_error(my_con));
exit(0);
} else {
res_ptr = mysql_store_result(my_con); //取出结果集
if(res_ptr) {
iTableRow = mysql_num_rows(res_ptr);//行
//iTableCol = mysql_num_fields(res_ptr);//列
for(i=0; i<iTableRow; i++){
sqlrow = mysql_fetch_row(res_ptr);
if(strcmp(username,sqlrow[0]) == 0){
mysql_free_result(res_ptr);
return 1;
}
}
}
mysql_free_result(res_ptr);
}
return 0;
}
删:
void sql_remove_client_fd(int client_fd){
char sql_insert[200];
sprintf(sql_insert, "DELETE from client_fd WHERE client_fd=%d",client_fd);
int res = mysql_query(my_con, sql_insert);
if (res) {
fprintf(stderr, "Delete error %d: %s\n", mysql_errno(my_con), mysql_error(my_con));
} else {
printf("MySQL : DELETE client_fd[%d]\n",client_fd);
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/muyang_ren/article/details/47066597