#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include < errno.h> #include <termios.h> #include <mysql/mysql.h> #define BUFSIZE 1024MYSQL MySQL, *connection;void Deletename (char *sql) {memset (SQL, 0, BUFSIZE), sprintf (SQL, "%s", "Enter the name to be killed;:"); write (Stdout_fileno, SQL, strlen ( SQL)); This is an alternative to the printf function, so that writing does not require \ nthe output of char name[1024];memset (name, 0, sizeof) to the screen, read (Stdin_fileno, name, sizeof ( name)); Wait for the user to enter the name to delete, assuming that the user entered the "Rice Island Love" Name[strlen (name)-1] = 0; Replace the last bit of the string with 0, and the last place is the integer 0 instead of the character ' 0 ' memset (SQL, 0, BUFSIZE); sprintf (SQL, "DELETE from table1 WHERE name = '%s '", name); sql = DELETE from table1 WHERE name = ' Rice Island Love ' printf (' '%s ' \ n ', SQL);} void Insertname (char *sql) {memset (SQL, 0, BUFSIZE), sprintf (SQL, "%s", "Enter the name to insert;:"); write (Stdout_fileno, SQL, BUFSIZE); This is an alternative to the printf function, so that writing does not require \ nthe output of char name[1024];memset (name, 0, sizeof) to the screen, read (Stdin_fileno, name, sizeof ( name)); Wait for the user to enter the name to insert, assuming that the user entered "Rice Island Love" name[strLen (name)-1] = 0; Replace the last bit of the string with 0, and the last place is the integer 0 instead of the character ' 0 ' memset (SQL, 0, BUFSIZE), sprintf (SQL, "%s", "Enter the gender to insert;:"); Write (Stdout_fileno , SQL, strlen (SQL)), char sex[1024];memset (sex, 0, sizeof (sex)); Read (Stdin_fileno, sex, sizeof (sex)); Wait for the user to enter the gender to be inserted, Sex[strlen (Sex)-1] = 0; Replace the last bit of the string with 0, and the last place is the integer 0 instead of the character ' 0 ' memset (SQL, 0, BUFSIZE), sprintf (SQL, "%s", "Enter the age to insert;:"); Write (Stdout_fileno , SQL, strlen (SQL)), Char Age[1024];memset (age, 0, sizeof); Read (Stdin_fileno, age, sizeof); Waits for the user to enter the age to insert Age[strlen-1] = 0; Replace the last bit of the string with 0, and the last place is the integer 0 instead of the character ' 0 ' memset (SQL, 0, BUFSIZE), sprintf (SQL, "%s", "enter class to insert;:"); Write (Stdout_fileno , SQL, strlen (SQL)), char classes[1024];memset (classes, 0, sizeof (classes)); Read (Stdin_fileno, classes, sizeof (classes )); Wait for the user to enter the class to be inserted Classes[strlen (classes)-1] = 0; Replace the last bit of the string with 0, and the last place is the integer 0 instead of the character ' 0 ' memset (SQL, 0, BUFSIZE); sprintf (SQL, "INSERT into table1 (name, sex, age, Class) VALUES ('%s ', '%s ',%s, '%s ') ", Name, sex, age, classes); SQL = DELete from table1 WHERE name = ' Rice Island Love ' printf (' '%s ' \ n ', SQL);} void UpdateName (char *sql) {memset (SQL, 0, BUFSIZE), sprintf (SQL, "%s", "Please enter name to be modified;:"); write (Stdout_fileno, SQL, Strlen (SQL)); This is an alternative to the printf function, so that writing does not require \ nthe output of char name[1024];memset (name, 0, sizeof) to the screen, read (Stdin_fileno, name, sizeof ( name)); Wait for the user to enter the name to insert, assuming that the user entered "Rice Island Love" Name[strlen (name)-1] = 0; Replace the last bit of the string with 0, and the last place is the integer 0 instead of the character ' 0 ' memset (SQL, 0, BUFSIZE), sprintf (SQL, "%s", "Enter gender for sex;:"); Write (Stdout_fileno , SQL, strlen (SQL)), char sex[1024];memset (sex, 0, sizeof (sex)); Read (Stdin_fileno, sex, sizeof (sex)); Wait for the user to enter the gender to be inserted, Sex[strlen (Sex)-1] = 0; Replace the last bit of the string with 0, and the last place is the integer 0 instead of the character ' 0 ' memset (SQL, 0, BUFSIZE), sprintf (SQL, "%s", "enter age to age;:"); Write (Stdout_fileno , SQL, strlen (SQL)), Char Age[1024];memset (age, 0, sizeof); Read (Stdin_fileno, age, sizeof); Waits for the user to enter the age to insert Age[strlen-1] = 0; Replace the last bit of the string with 0, and the last place is the integer 0 instead of the character ' 0 ' memset (SQL, 0, BUFSIZE), sprintf (SQL, "%s", "Enter the class to be modified;:"); Write (Stdout_fileno , SQL, StRlen (SQL)); Char classes[1024];memset (classes, 0, sizeof (classes)); Read (Stdin_fileno, classes, sizeof (classes)); Wait for the user to enter the class to be inserted Classes[strlen (classes)-1] = 0; Replace the last bit of the string with 0, and the last place is the integer 0 instead of the character ' 0 ' memset (SQL, 0, BUFSIZE); sprintf (sql, "UPDATE table1 SET sex = '%s ', age =%s, class = '%s ' WHERE name = '%s ' ", sex, age, classes, name); sql = DELETE from table1 WHERE name = ' Rice Island Love ' printf (' '%s ' \ n ', SQL);} void Selectname (const char *sql) {/*char sql[1024];memset (SQL, 0, BUFSIZE), sprintf (SQL, "%s", "Please enter the name to be queried;:"); Write ( Stdout_fileno, SQL, strlen (SQL)); This is an alternative to the printf function, so that writing does not require \ nthe output of char name[1024];memset (name, 0, sizeof) to the screen, read (Stdin_fileno, name, sizeof ( name)); Wait for the user to enter the name to delete, assuming that the user entered the "Rice Island Love" Name[strlen (name)-1] = 0; Replace the last bit of the string with 0, and the last place is the integer 0 instead of the character ' 0 ' memset (SQL, 0, BUFSIZE), if (strlen (name) = = 0)//user does not have any input, just hit enter, the length is 0 {sprintf (SQL , "SELECT * from table2"); } else{sprintf (SQL, "select * from table2 WHERE name = '%s '", name);//}*/if (mysql_query (connection, SQL)! = 0) {printf ( The query Error,%s\n ", Mysql_error (&mysql));} Call Mysql_store_result to get the results of the query, the results are placed in the mysql_res structure mysql_res *result = Mysql_store_result (connection);// To know how many columns of the return dataset are free to use the various SELECT statements Mysql_field *field;int ifieldcount = 0;while (1) {FIELD = Mysql_fetch_field (result); The loop gets the column name, and if you loop to the end of the column, the function returns nullif (field = = NULL) break;printf ("%s\t", field->name); ifieldcount++;} printf ("\ n");//loop through each line Mysql_row Row;while (1) {row = Mysql_fetch_row (result); if (row = = NULL) Break;int i = 0;for (; i < Ifieldcount; i++) {printf ("%s\t", (const char *) row[i]);} printf ("\ n");} Mysql_free_result (result);} int main (int arg, char *args[]) {if (Arg < 4) return-1;mysql_init (&mysql);//equivalent to SQL internal initialization of a TCP socket, At the same time initialize the SQL must have memory and some structure//connect to MySQL serverconnection = Mysql_real_connect (&mysql, args[1], args[2], args[3], args[4],0, 0, 0); if (connection = = NULL) {printf ("Connect error,%s\n", Mysql_error (&mysql)); return-1;} if (mysql_query (connection, "set NAMES UTF8")! = 0)//sets the character set to utf8{printf ("Set charset error,%s\n", Mysql_error (&mysQL));} Char Buf[bufsize];memset (buf, 0, sizeof (BUF)), strcpy (buf, "select \n1: Insert \n2: Delete \n3: Modify \n4: Query \ n"); write (Stdout_fileno, buf , strlen (BUF)), memset (buf, 0, sizeof (BUF)), read (Stdin_fileno, buf, sizeof (BUF)), if (strncmp (buf, "4", 1) = = 0)// Input user input is 4{memset (buf, 0, sizeof (BUF)), strcpy (buf, "Please enter any SELECT statement"); write (Stdout_fileno, buf, strlen (BUF)); Memset ( BUF, 0, sizeof (BUF)); Read (Stdin_fileno, buf, sizeof (BUF)); Selectname (BUF);} Else{if (strncmp (buf, "1", 1) = = 0)//input user input is 1{insertname (BUF);} if (strncmp (buf, "2", 1) = = 0)//input user input is 2{deletename (BUF);} if (strncmp (buf, "3", 1) = = 0)//input user input is 3{updatename (BUF);} mysql_query (connection, buf);} Mysql_close (connection);//Disconnects the connection to SQL Server puts ("!!! Hello world!!! "); /* Prints!!! Hello World!!! */return exit_success;}
(18) Database programming--c language Operations database