Linux C 調用MYSQL API 函數mysql_escape_string()轉義插入資料

來源:互聯網
上載者:User

標籤:

Title:Linux C 調用MYSQL API 函數mysql_escape_string()轉義插入資料 --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 success\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);        /*裡頭的函數返回受表中影響的行數*/        }        else        {        //分別列印出錯誤碼及詳細資料        fprintf(stderr, "Insert error %d: %s\n",mysql_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 調用MYSQL API 函數mysql_escape_string()轉義插入資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.