Title:linux C calls the MySQL API function mysql_escape_string () Escape Insert Data--2013-10-11 11:57
#include <stdio.h> #include <string.h> #include <stdlib.h> #include "mysql.h" int main (int argc, char * Argv[]) {MYSQL my_connection; int res; Mysql_init (&my_connection); Char unames[50]; Char sql_insert[400]; Char *p[5]; P[0]= "123456"; p[1]= "Fuc ' a ' k"; P[2]= "127.0.0.1"; p[3]= "2013-09-26 10:10:10"; p[4]= "1"; /*mysql_real_connect (&mysql,host,user,passwd,dbname,0,null,0) = = NULL) */if (Mysql_real_connect (&my_ Connection, "127.0.0.1", "root", "Fuckfuck", "DatabaseName", 0,null,client_found_rows)) {printf ("Connection Succe Ss\n "); Mysql_escape_string (Unames,p[1],strlen (p[1)); snprintf (Sql_insert, sizeof (Sql_insert), "insert INTO ' tablename ' (' C1 ', ' C2 ', ' C3 ', ' C4 ', ' C5 ') VALUES ('%s ', '%s ', '%s ', ' %s ', '%s '); ", P[0],unames,p[2],p[3],p[4]); res = mysql_query (&my_connection,sql_insert); if (!res) {printf ("Inserted%lu rows\n", (unsigned long) mysql_affected_rows (&my_connection)); printf ("%s------\ n", Sql_insert); printf ("%s------\ n", unames); /* The function returns the number of rows affected by the table */} else {//print out the error code and details separately fprintf (stderr, "Insert error%d:%s\n", M Ysql_errno (&my_connection), Mysql_error (&my_connection)); } mysql_close (&my_connection); } else {fprintf (stderr, "Connection failed \ n"); if (Mysql_errno (&my_connection)) {fprintf (stderr, "Connection error%d:%s\n", Mysql_errno (& my_connection), Mysql_error (&my_connection)); }} return exit_success;}
Linux C calls the MySQL API function mysql_escape_string () Escape Insert Data