Linux編程 - 使用C在mysql中插入數據


1. 代碼編寫

#include <stdlib.h>
#include <stdio.h>
#include  " mysql.h "

int main( int argc,  char *argv[]) 
{
    MYSQL my_connection;
     int res;
    mysql_init(&my_connection);
    
     if (mysql_real_connect(&my_connection,  " localhost "" root "" mysql "" mysql "0, NULL,  0)) 
    {
        printf( " Connection success\n ");
        res = mysql_query(&my_connection,  " INSERT INTO children(fname, age) VALUES('david', 8) ");
        
         if (!res) 
        {
            printf( " Inserted %lu rows\n ", (unsigned  long)mysql_affected_rows(&my_connection));
        } 
         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;
}

 

2. 插入前

 

3. 運行結果

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM