日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

C语言操作MYSQL小例子

發布時間:2023/11/30 数据库 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C语言操作MYSQL小例子 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://blog.csdn.net/small_qch/article/details/8180678

??初學使用用C語言操作MYSQL,寫了個小例子,帖上來獻丟人一下,呵呵。

? ? ?程序很簡單,先連接數據庫,然后向class1表中插入一條數據,最后獲取并輸出整個class1表的內容。


上代碼:

[cpp]?view plain?copy
  • //test.c??
  • //gcc?test.c?-o?test?-lmysqlclient??
  • #include?<stdio.h>??
  • #include?<stdlib.h>??
  • #include?<mysql/mysql.h>??
  • ??
  • //發生錯誤時,輸出錯誤信息,關閉連接,退出程序??
  • void?error_quit(const?char?*str,?MYSQL?*connection)??
  • {??
  • ????fprintf(stderr,?"%s?:?%d:?%s\n",??
  • ????????str,?mysql_errno(connection),??
  • ????????mysql_error(connection));??
  • ????if(?connection?!=?NULL?)??
  • ????????mysql_close(connection);??
  • ????exit(1);??
  • }??
  • ??
  • int?main(int?argc,?char?*argv[])???
  • {??
  • ????MYSQL?*my_con?=?malloc(?sizeof(MYSQL)?);??
  • ????MYSQL_RES?*my_res;??
  • ????MYSQL_FIELD?*my_field;??
  • ????MYSQL_ROW?my_row;??
  • ????int?rows,?i;??
  • ????int?res;??
  • ??
  • ????//連接數據庫??
  • ????mysql_init(my_con);???
  • ????my_con?=?mysql_real_connect(my_con,?"localhost",?"test",?"aaaaaaa",??
  • ????????"test1",?0,?NULL,?CLIENT_FOUND_ROWS);??
  • ????if(?NULL?==?my_con?)???
  • ????????error_quit("Connection?fail",?my_con);??
  • ????printf("Connection?success\n");??
  • ??
  • ????//向數據庫中插入一條記錄??
  • ????res?=?mysql_query(my_con,???
  • ????????"insert?into?class1(name,?age,?birthday)?value('abc',?52,?NOW());");??
  • ????if(?res?!=?0?)???
  • ????????error_quit("Insert?fail",?my_con);??
  • ????//返回的是表中被影響的行數??
  • ????res?=?mysql_affected_rows(my_con);??
  • ????printf("Inserted?%d?rows\n",?res);??
  • ??
  • ????//獲取整個表的內容??
  • ????res?=?mysql_query(my_con,?"select?*?from?class1;");??
  • ????if(?res?!=?0?)??
  • ????????error_quit("Select?fail",?my_con);??
  • ????my_res?=?mysql_store_result(my_con);??
  • ????if(?NULL?==?my_res?)??
  • ????????error_quit("Get?result?fail",?my_con);??
  • ??
  • ????//獲取表的列數??
  • ????rows?=?mysql_num_fields(my_res);??
  • ????//獲取并輸出表頭??
  • ????my_field?=?mysql_fetch_fields(my_res);??
  • ????for(i=0;?i<rows;?i++)??
  • ????????printf("%s\t",?my_field[i].name);??
  • ????printf("\n-------------------------------------\n");??
  • ??
  • ????//輸出整個表的內容??
  • ????while(?1?)??
  • ????{??
  • ????????my_row?=?mysql_fetch_row(my_res);??
  • ????????if(?NULL?==?my_row?)??
  • ????????????break;??
  • ????????for(i=0;?i<rows;?i++)??
  • ????????{??
  • ????????????if(?my_row[i]?==?NULL?)??
  • ????????????????printf("NULL\t");??
  • ????????????else??
  • ????????????????printf("%s\t",?(char*)my_row[i]);??
  • ????????}??
  • ????????printf("\n");??
  • ????}??
  • ??
  • ????//釋放空間,關閉連接??
  • ????mysql_free_result(my_res);??
  • ????mysql_close(my_con);??
  • ????free(my_con);??
  • ??
  • ????return?0;??
  • }??

  • 運行結果:

    [plain]?view plain?copy
  • Connection?success??
  • Inserted?1?rows??
  • id??name????age?birthday??
  • -------------------------------------??
  • 49??ddd?43??2012-11-09?09:49:41??
  • 50??fff?31??0000-00-00?00:00:00??
  • 58??eee?32??NULL??
  • 59??qqq?43??NULL??
  • 78??abc?52??2012-11-13?14:47:55??

  • 總結

    以上是生活随笔為你收集整理的C语言操作MYSQL小例子的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。