[C/C ++ school] (22) Mysql database programming-c language database operations, mysql-C

Source: Internet
Author: User

[C/C ++ school] (22) Mysql database programming-c language database operations, mysql-C

Makefile

.SUFFIXES: .c .oCC=gccSRCS=mysql1.cOBJS=$(SRCS:.c=.o)EXEC=mysql1all: $(OBJS)$(CC) -o $(EXEC) $(OBJS) -lmysqlclient@echo '-------------ok--------------'.c.o:$(CC) -Wall -g -o $@ -c $< clean:rm -f $(OBJS)rm -f core*

Mysql1.c

# Include <stdio. h> # include <stdlib. h> # include <unistd. h> # include <string. h> # include <errno. h> # include <termios. h> # include <mysql/mysql. h> # define BUFSIZE 1024 MYSQL 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 sentence replaces the printf function, in this way, the char name [1024]; memset (name, 0, sizeof (name); read (STDIN_FI LENO, name, sizeof (name); // wait for the user to enter the name to be deleted. Assume that the user entered "Rice island love" name [strlen (name)-1] = 0; // replace \ n, the last character of the string, with 0, with an integer 0 instead of '0' memset (SQL, 0, BUFSIZE); sprintf (SQL, "delete from table1 WHERE name = '% S'", name ); /// SQL = delete from table1 WHERE name = 'island ai' 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 sentence is replaced In this way, the char name [1024] can be output to the screen without \ n; memset (name, 0, sizeof (name); read (STDIN_FILENO, name, sizeof (name); // wait for the user to enter the name to be inserted. Assume that the user entered the name "Rice island love" name [strlen (name)-1] = 0; // replace \ n, the last character of the string, with 0, with an integer 0 instead of '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 sex to insert, s Ex [strlen (sex)-1] = 0; // replace \ n, the last character of the string, with 0 and the last integer 0, rather than 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 (age); read (STDIN_FILENO, age, sizeof (age )); // wait for the user to enter the age to insert [strlen (age)-1] = 0; // replace \ n with 0, the last value is an integer 0 instead of the character '0' memset (SQL, 0, BUFSIZE); sprintf (SQL, "% s", "Enter the class to be inserted>: "); write (STDOUT_FILENO, SQL, strl En (SQL); char classes [1024]; memset (classes, 0, sizeof (classes); read (STDIN_FILENO, classes, sizeof (classes )); // wait for the user to enter the class classes to be inserted [strlen (classes)-1] = 0; // replace \ n with 0, the last value is an 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 = 'island ai' printf ("'% s' \ n", SQL );} Void updatename (char * SQL) {memset (SQL, 0, BUFSIZE); sprintf (SQL, "% s", "Enter the name to modify> :"); write (STDOUT_FILENO, SQL, strlen (SQL); // This sentence replaces the printf function. In this way, the char name [1024] can be output to the screen without \ n. memset (name, 0, sizeof (name); read (STDIN_FILENO, name, sizeof (name); // wait for the user to enter the name to insert, assume that the user entered "Rice island love" name [strlen (name)-1] = 0; // replace \ n, the last character of the string, with an integer 0, rather than the character '0' memset (SQL, 0, BUFSIZE); sprintf (SQL, "% s", "Enter the gender to be gender>:"); 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 sex to be inserted. sex [strlen (sex)-1] = 0; // replace \ n, the last character of the string, with 0, with an integer 0 instead of '0' memset (SQL, 0, BUFSIZE); sprintf (SQL, "% s", "Enter age>:"); write (STDOUT_FILENO, SQL, strlen (SQL); char age [1024]; memset (age, 0, sizeof (age); read (STDIN_FILENO, age, sizeof (age); // wait for the user to enter the age to insert [strlen (age) -1] = 0; // replace \ n, the last character of the string, with 0 and the last integer 0 instead of '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 classes to be inserted [strlen (classes) -1] = 0; // replace \ n, the last character of the string, with 0 and the last integer 0 instead of '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 = 'island ai' printf ("'% s' \ n", SQL);} void selectname (const char * SQL) {/* char SQL [1024]; memset (SQL, 0, BUFSIZE); sprintf (SQL, "% s", "Enter the name to query> :"); write (STDOUT_FILENO, SQL, strlen (SQL); // This sentence replaces the printf function. In this way, the char name [1024] can be output to the screen without \ n. memset (name, 0, sizeof (name); read (STDIN_FILENO, name, sizeof (name ));/ /Wait for the user to enter the name to be deleted. Assume that the user entered "Rice island love" name [strlen (name)-1] = 0; // replace \ n, the last character of the string, with 0, with an integer 0 instead of '0' memset (SQL, 0, BUFSIZE); if (strlen (name) = 0) // the user does not have any input, just press enter and 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 ("query error, % s \ n", mysql_error (& mysql);} // call mysql_store_result to obtain the query result, put the result in the MYSQL_RES structure MYSQL_RES * result = mysql_store_result (connection); // you must know the number of columns in the returned dataset to use various SELECT statements MYSQL_FIELD * field freely; int iFieldCount = 0; while (1) {field = mysql_fetch_field (result); // The column name is obtained cyclically. If the column name is obtained at the end of the loop, the function returns NULLif (field = NULL) break; printf ("% s \ t", field-> name); iFieldCount ++;} printf ("\ n"); // cyclically traverse each row MYSQL_ROW row; whi Le (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 ); // It is equivalent to initializing a TCP socket in SQL, and initializing the memory and some structures required by SQL. // 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) // set the character set to UTF8 {printf ("set character set 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) // The input user inputs 4 {memset (buf, 0, sizeof (buf )); strcpy (buf, "enter any SELECt statement"); write (STDOUT_FILENO, buf, strlen (buf); memse T (buf, 0, sizeof (buf); read (STDIN_FILENO, buf, sizeof (buf); selectname (buf);} else {if (strncmp (buf, "1", 1) = 0) // The input user inputs 1 {insertname (buf);} if (strncmp (buf, "2", 1) = 0) // The input user inputs 2 {deletename (buf);} if (strncmp (buf, "3", 1) = 0) // The input user inputs 3 {updatename (buf);} mysql_query (connection, buf);} mysql_close (connection ); // disconnect from SQL server puts ("!!! Hello World !!! ");/* Prints !!! Hello World !!! */Return EXIT_SUCCESS ;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.