Linux C calls the MySQL API function mysql_escape_string () Escape Insert Data

Source: Internet
Author: User

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

Related Article

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.