"to the program Ape on the road."
For a developer, the ability to develop a free module in a system is the embodiment of its core value.
For an architect, mastering the advantages of various languages and applying them to the system simplifies the development of the system and is the first step in its architectural career.
for a development team. The ability to develop a user-pleasing software system in the short term is the embodiment of its core competitiveness.
Each program ape can not stand on their laurels, to contact the new industry, new technology field, breakthrough self.
Makefile
.suffixes:. C. oCC=gccSRCs=mysql1.c MYDB.CObjs=$(SRCs:. c=.o)EXEC=mysql1All : $(Objs)$(CC)-O$(EXEC)$(Objs)-lmysqlclient@echo '-------------OK--------------'. C.o: $(CC) -Wall-g-o[email protected]-C$< Clean :Rm-f$(Objs) Rm-f core*
Mydb.h
#ifndef mydb_h_ #define MYDB_H_ void init_db (); int conn_db (const char *hostname, const char *username, const char *password, const char *dbname); void disconn_db (); int open_db (const char *sql); int exec_db (const char *sql); #endif/* Mydb_h_ */
Mydb.c
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <mysql/mysql.h>#include "mydb.h"MYSQL *connection = NULL; MySQL MySQL;voidinit_db () {mysql_init (&mysql);//Initialize MySQL}intconn_db (Const Char*hostname,Const Char*username,Const Char*password,Const Char*dbname) {if(connection) mysql_close (connection); Connection = Mysql_real_connect (&MYSQL, hostname, username, password, dbname,0,0,0);//Connect to MySQL if(Connection = = NULL) {printf("%s\n", Mysql_error (&mysql));return-1;//Connection failed, return-1}printf("Success Connect to mysql\n");return 0;}voiddisconn_db ()//Disconnecting database connections{if(connection) {mysql_close (connection); connection = NULL; }}intopen_db (Const Char*sql)//Run SQL statement with return data set{intstate = mysql_query (connection, SQL);//Run SQL statements if(state! =0) {printf("%s\n", mysql_error (connection));return-1;//Run failed, return-1} mysql_res *result = Mysql_store_result (connection);//Get query Results if(Result = = (Mysql_res *) NULL) {printf("%s\n", mysql_error (connection));return-1;//Failed to run. Returns-1}Else{Mysql_field *sqlfield;intIfieldcount =0; while(1)//Loop through all fields{Sqlfield = Mysql_fetch_field (result);if(Sqlfield = = NULL) Break;printf("%s\t", sqlfield->name);//Print field names to screenifieldcount++; }printf("\ n");//print a \ n character at the end of each lineMysql_row Sqlrow; while(1)//loop to each row{Sqlrow = mysql_fetch_row (result);if(Sqlrow = = NULL) Break;intI for(i =0; i < Ifieldcount; i++)//loop to get each field in each row{if(Sqlrow[i] = = NULL)printf("Null\t");//Assume a value of NULL. Screen printed as "NULL" Else printf("%s\t", (Const Char*) sqlrow[i]);//Screen printing as field contents}printf("\ n");//print a \ n character at the end of each line}printf("Query is OK,%u rows affected\n", (unsigned int) Mysql_affected_rows (connection)); Mysql_free_result (result); }return 0;}intexec_db (Const Char*sql)//Run SQL statement with no return data set{intstate = mysql_query (connection, SQL);//Run SQL statements if(state! =0) {printf("%s\n", mysql_error (connection));return-1; }printf("Query is OK,%u rows affected\n", (unsigned int) Mysql_affected_rows (connection));return 0;}
mysql1.c
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <sys/types.h>#include <errno.h>#include <termios.h>#include "mydb.h"voidSqldbConst Char*SRC)//Parameter SRC is the SQL statement to run{if((strncmp(SRC,"SELECT",6) ==0) || (strncmp(SRC,"SELECT",6) ==0) || (strncmp(SRC,"Show",4) ==0) || (strncmp(SRC,"SHOW",4) ==0) || (strncmp(SRC,"desc",4) ==0) || (strncmp(SRC,"DESC",4) ==0) {open_db (SRC);//Assuming SRC is a return dataset SQL statement, then call the open_db function}Else{exec_db (SRC);//Assume that SRC is not having a return dataset SQL statement. Then call the exec_db function}}voidWorkConst Char*userid,Const Char*password) {init_db ();if(Conn_db ("localhost", UserID, Password,"Test") !=0)//Connect to database{return;//connection to database failed. function Exit}Charbuf[2048]; while(1)//loop read from keyboard{Write (Stdout_fileno,"Mysql1>",strlen("Mysql1>"));//Screen output command prompt mysql1> memset(BUF,0,sizeof(BUF)); Read (Stdin_fileno, buf,sizeof(BUF));//wait for the user to enter from the keyboard if(strncmp(BUF,"Quit",4) ==0) Break;//user input quit, loop break;Sqldb (BUF); } disconn_db ();//Disconnecting database connections}structTermios Oldterm;voidSetstty ()//Set input backspace key, do not echo{//system ("Stty erase ^h");//Run the shell command, can also be used to set the time to read the user's keyboard input. Backspace key does not echo structTermios term;if(Tcgetattr (Stdin_fileno, &term) = =-1)//Get the system Termion settings{printf("Tcgetattr error is%s\n", Strerror (errno));return; } oldterm = term;//Keep the current Termios setting so that the program can recover when it exits Termios /* Term.c_lflag &= ~icanon;//cancel icanon option (nonstandard input) Term.c_lflag |= icanon;//set icanon option (canonical input) term.c_cc field to set Detailed special input characters, such as c_cc[verase] for backspace key, term.c_cc[verase] = ' \b '; meaning to change the backspace key to ' \b ' verase means to erase a character forward, vintr to send CTRL + C interrupt signal, CTRL + C The ASCII code is 3 for example: term.c_cc[vintr] = ' \ t ', meaning that the TAB key is set to Terminal signal tcsetattr, the second parameter indicates that Tcsaflush: The change takes effect when all output is sent. When a change occurs, all input data that is not read is deleted Tcsanow: The change takes effect immediately tcsadrain: The change occurs only after the entire output has been sent. This option should be used if you change the output parameters * /Term.c_cc[verase] =' \b ';//' \b ' is the backspace ASCII code if(Tcsetattr (Stdin_fileno, Tcsanow, &term) = =-1)//Set system Termion{printf("Tcsetattr error is%s\n", Strerror (errno)); }return;}voidReturnstty ()//Termios settings for recovery system{if(Tcsetattr (Stdin_fileno, Tcsaflush, &oldterm) = =-1)//Set system Termion{printf("Tcsetattr error is%s\n", Strerror (errno)); }return;}intMainintArgChar*args[]) {if(Arg <4)//Assuming no parameters, main function exits{returnExit_failure; }if(strncmp(args[1],"-U",2) !=0)//Assume the second parameter is not-U. Main function Exits{returnExit_failure; }if(strncmp(args[3],"-P",2) !=0)//Assuming that the fourth parameter is not-p,main function exit{returnExit_failure; }Const Char*PASSWD = Getpass ("Please input password:");//Enter password, screen does not EchoSetstty ();//Set input backspace key, do not echoWork (args[2], passwd); Returnstty ();//Termios settings for recovery system returnexit_success;}
|========== Wu Yingjiang csdn Blog column ==========|
|== The content of the A/C + + College column (not updated regularly) ===|
|== Linux-Driven development explores the secrets of Linux's underlying ========|
|== Basic knowledge of Java Basic Learning =====|
| ====== Daily Progress a little, healthy and happy every day ======|
MySQL database programming--c language programming implementation mysqlclient