Database initialization
Link Database
my_con = mysql_init(NULL);mysql_real_connect(my_con,MYSQL_SERVER,MYSQL_USER,MYSQL_PASSWD,NULL,0,NULL,0));
Create a database file Smart_home
mysql_query(my_con, "create database if not exists smart_home"));
Choose a database file Smart_home
mysql_query(my_con, "use smart_home"));
Create DATABASE tables Users
mysql_query(my_con, "create table if not exists users(username VARCHAR(20),password VARCHAR(20))")) ;
Create a database table hardware
mysql_query (My_con, "Create Table if not existsSh_hardware (Sh_flagVARCHAR( -), Sh_timeVARCHAR( +), Sh_nameVARCHAR( -), sh_tmpVARCHAR( -), Sh_dampVARCHAR( -), Sh_fanVARCHAR( -), Sh_buzVARCHAR( -), sh_ledVARCHAR( -), Sh_camareVARCHAR( -))")) ;
Create a phone terminal connection table CLIENT_FD
mysql_query(my_con, "create table if not exists client_fd(client_fd int(8))"));
Database Operations
Increase:
char sql_insert[200]; sprintf(sql_insert, "INSERT INTO client_fd(client_fd) values(‘%d‘);",client_fd); mysql_query(my_con, sql_insert);
By deleting:
char sql_insert[200]; sprintf(sql_insert, "DELETE from client_fd WHERE client_fd=‘%d‘);",client_fd); mysql_query(my_con, sql_insert);
Check:
intItablerow; Mysql_res*res_ptr; Mysql_row Sqlrow; res = mysql_query (My_con,"Select Username,password from Users");//Query StatementsRes_ptr = Mysql_store_result (My_con);//Remove result set if(RES_PTR) {Itablerow = mysql_num_rows (res_ptr);//Line //itablecol = Mysql_num_fields (res_ptr);//Column 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); }
Instance
void Start_mysql_init () {//Link Database My_con = mysql_init (NULL);if(!mysql_real_connect (My_con,mysql_server,mysql_user,mysql_passwd,null,0Null0) {fprintf (stderr,'%s\ n ', Mysql_error (My_con));Exit(1); }printf("MySQL connect!\n");printf("MySQL version:%s\ n", Mysql_get_client_info ());//Create a database file Smart_homeif(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); }//Select Database file Smart_homeif(Mysql_query (My_con,"Use Smart_home")) {printf("Error %u: %s\ n", Mysql_errno (My_con), Mysql_error (My_con));Exit(1); }//CREATE database tables usersif(Mysql_query (My_con,"CREATE table if not EXISTS users (username varchar (), password varchar)")) {printf("Error %u: %s\ n", Mysql_errno (My_con), Mysql_error (My_con));Exit(1); }//CREATE DATABASE table hardwareif(Mysql_query (My_con,"CREATE table if not exists sh_hardware (sh_flag varchar), Sh_time varchar (+), Sh_name varchar (), sh_tmp varchar ( Sh_damp varchar (), Sh_fan varchar (), Sh_buz varchar, sh_led varchar (), sh_camare varchar) ")) {printf("Error %u: %s\ n", Mysql_errno (My_con), Mysql_error (My_con));Exit(1); }//Create phone Terminal connection Table CLIENT_FDif(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); }}
Increase:
//Sh_hardware:sh_flag varchar (sh_time) sh_name varchar (sh_tmp varchar )//Sh_damp varchar (sh_fan) sh_buz varchar (sh_led varchar )//Sh_camare VARCHAR (a)voidSql_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) {Charsql_insert[ $];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);intres = mysql_query (My_con, Sql_insert);if(RES) {fprintf(stderr,"Insert error%d:%s\n", Mysql_errno (My_con), Mysql_error (My_con)); } }
Check:
intSql_select_reg (Const Char*username) {intRes, I;intItablerow; Mysql_res *res_ptr; Mysql_row Sqlrow; res = mysql_query (My_con,"Select Username,password from Users");//Query Statements if(RES) {printf("Select Error:%s\n", Mysql_error (My_con));Exit(0); }Else{res_ptr = Mysql_store_result (My_con);//Remove result set if(RES_PTR) {Itablerow = mysql_num_rows (res_ptr);//Line //itablecol = Mysql_num_fields (res_ptr);//Column 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;}
By deleting:
void sql_remove_client_fd(int client_fd){ char sql_insert[200]; sprintf"DELETE from client_fd WHERE client_fd=%d",client_fd); int res = mysql_query(my_con, sql_insert); if (res) { "Delete error %d: %s\n", mysql_errno(my_con), mysql_error(my_con)); else { printf("MySQL : DELETE client_fd[%d]\n",client_fd); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Ubuntu MySQL Use