C語言操作MYSQL小例子

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

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

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


上代碼:

[cpp]?view plain?copy
  1. //test.c??
  2. //gcc?test.c?-o?test?-lmysqlclient??
  3. #include?<stdio.h>??
  4. #include?<stdlib.h>??
  5. #include?<mysql/mysql.h>??
  6. ??
  7. //發生錯誤時,輸出錯誤信息,關閉連接,退出程序??
  8. void?error_quit(const?char?*str,?MYSQL?*connection)??
  9. {??
  10. ????fprintf(stderr,?"%s?:?%d:?%s\n",??
  11. ????????str,?mysql_errno(connection),??
  12. ????????mysql_error(connection));??
  13. ????if(?connection?!=?NULL?)??
  14. ????????mysql_close(connection);??
  15. ????exit(1);??
  16. }??
  17. ??
  18. int?main(int?argc,?char?*argv[])???
  19. {??
  20. ????MYSQL?*my_con?=?malloc(?sizeof(MYSQL)?);??
  21. ????MYSQL_RES?*my_res;??
  22. ????MYSQL_FIELD?*my_field;??
  23. ????MYSQL_ROW?my_row;??
  24. ????int?rows,?i;??
  25. ????int?res;??
  26. ??
  27. ????//連接數據庫??
  28. ????mysql_init(my_con);???
  29. ????my_con?=?mysql_real_connect(my_con,?"localhost",?"test",?"aaaaaaa",??
  30. ????????"test1",?0,?NULL,?CLIENT_FOUND_ROWS);??
  31. ????if(?NULL?==?my_con?)???
  32. ????????error_quit("Connection?fail",?my_con);??
  33. ????printf("Connection?success\n");??
  34. ??
  35. ????//向數據庫中插入一條記錄??
  36. ????res?=?mysql_query(my_con,???
  37. ????????"insert?into?class1(name,?age,?birthday)?value('abc',?52,?NOW());");??
  38. ????if(?res?!=?0?)???
  39. ????????error_quit("Insert?fail",?my_con);??
  40. ????//返回的是表中被影響的行數??
  41. ????res?=?mysql_affected_rows(my_con);??
  42. ????printf("Inserted?%d?rows\n",?res);??
  43. ??
  44. ????//獲取整個表的內容??
  45. ????res?=?mysql_query(my_con,?"select?*?from?class1;");??
  46. ????if(?res?!=?0?)??
  47. ????????error_quit("Select?fail",?my_con);??
  48. ????my_res?=?mysql_store_result(my_con);??
  49. ????if(?NULL?==?my_res?)??
  50. ????????error_quit("Get?result?fail",?my_con);??
  51. ??
  52. ????//獲取表的列數??
  53. ????rows?=?mysql_num_fields(my_res);??
  54. ????//獲取并輸出表頭??
  55. ????my_field?=?mysql_fetch_fields(my_res);??
  56. ????for(i=0;?i<rows;?i++)??
  57. ????????printf("%s\t",?my_field[i].name);??
  58. ????printf("\n-------------------------------------\n");??
  59. ??
  60. ????//輸出整個表的內容??
  61. ????while(?1?)??
  62. ????{??
  63. ????????my_row?=?mysql_fetch_row(my_res);??
  64. ????????if(?NULL?==?my_row?)??
  65. ????????????break;??
  66. ????????for(i=0;?i<rows;?i++)??
  67. ????????{??
  68. ????????????if(?my_row[i]?==?NULL?)??
  69. ????????????????printf("NULL\t");??
  70. ????????????else??
  71. ????????????????printf("%s\t",?(char*)my_row[i]);??
  72. ????????}??
  73. ????????printf("\n");??
  74. ????}??
  75. ??
  76. ????//釋放空間,關閉連接??
  77. ????mysql_free_result(my_res);??
  78. ????mysql_close(my_con);??
  79. ????free(my_con);??
  80. ??
  81. ????return?0;??
  82. }??

運行結果:

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/384162.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/384162.shtml
英文地址,請注明出處:http://en.pswp.cn/news/384162.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Find a way——BFS

【題目描述】 Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year, yifenfei have many people to meet. Especially a good friend Merceki. Yifenfei’s home is at the countryside, but Merceki’s home is in the ce…

用C語言操作MySQL數據庫

http://blog.chinaunix.net/uid-26743670-id-3479887.html 參考MYSQL的幫助文檔整理 這里歸納了C API可使用的函數&#xff0c;并在下一節詳細介紹了它們。請參見25.2.3節&#xff0c;“C API函數描述”。 函數 描述 mysql_affected_rows() 返回上次UPDATE、DELETE或INSERT查…

三字棋

整個游戲可以分為以下幾個環節 1.打印一個玩游戲的菜單 2.玩游戲 (1)玩家走一步 (2)電腦走一步 每走一步對結果進行顯示,其中游戲的結果可分為玩家贏,電腦贏,以及平局 代碼顯示如下 game.c #define _CRT_SECURE_NO_WARNINGS 1 #include<stdlib.h> #include<stdio.h&…

gets fgets 區別

http://www.cnblogs.com/aexin/p/3908003.html 1. gets與fgets gets函數原型&#xff1a;char*gets(char*buffer);//讀取字符到數組&#xff1a;gets(str);str為數組名。 gets函數功能&#xff1a;從鍵盤上輸入字符&#xff0c;直至接受到換行符或EOF時停止&#xff0c;并將讀取…

Fliptile——搜索+二進制優化

【題目描述】 Farmer John knows that an intellectually satisfied cow is a happy cow who will give more milk. He has arranged a brainy activity for cows in which they manipulate an M N grid (1 ≤ M ≤ 15; 1 ≤ N ≤ 15) of square tiles, each of which is col…

掃雷

1.將掃雷界面看成一個二維數組,先對界面進行打印 2.置雷 3.排雷 4.對每次的結果進行游戲輸出 5.提醒用戶游戲輸贏 game.c #define _CRT_SECURE_NO_WARNINGS 1 #include"game.h" //初始化 void init_board(char mine[ROWS][COLS], int rows, int cols, char set) {in…

C相關練習題

1.調整數組使奇數全部都位于偶數前面。 輸入一個整數數組&#xff0c;實現一個函數&#xff0c;來調整該數組中數字的順序使得數組中所有的奇數位于數組的前半部分&#xff0c;所有偶數位于數組的后半部分。 #include<stdio.h> void range(int arr[], int sz) {int left…

【C語言】單鏈表的所有操作的實現(包括PopBack、PushBack、PopFront、PushFront、Insert)

http://blog.csdn.net/hanjing_1995/article/details/51539563 [cpp] view plaincopy #define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> using namespace std; //單鏈表的實現 #include<assert.h> typedef int DataType; typedef…

Shuffle'm Up——簡單模擬

【題目描述】 A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks of poker chips, S1 and S2, each stack containing C chips. Each stack may contain chips of several diff…

C++ explicit關鍵字詳解

http://www.cnblogs.com/ymy124/p/3632634.html 首先, C中的explicit關鍵字只能用于修飾只有一個參數的類構造函數, 它的作用是表明該構造函數是顯示的, 而非隱式的, 跟它相對應的另一個關鍵字是implicit, 意思是隱藏的,類構造函數默認情況下即聲明為implicit(隱式). 那么顯示聲…

Fire!——兩個BFS

【題目描述】 【題目分析】 看到題目后很清楚是兩個BFS&#xff0c;可是我覺得對于火的BFS可以轉換成判斷&#xff0c;我的做法是將火的位置全部記錄下來&#xff0c;然后判斷某個位置距離每個火的步數是否小于當前步數&#xff0c;可是錯了&#xff0c;還不清楚為什么&#x…

函數調用過程(棧楨)

棧楨 首先來看一段代碼 #include<stdio.h> int add(int x, int y) {int z x y;return z; } int main() {int a 10;int b 20;int ret add(a, b);printf("ret %d\n",ret);return 0; } 此處是為了給a,b分別開辟空間,這時棧楨如圖所示 兩條push命令將a,b變…

C++智能指針簡單剖析

http://blog.csdn.net/lanxuezaipiao/article/details/41603883 導讀 最近在補看《C Primer Plus》第六版&#xff0c;這的確是本好書&#xff0c;其中關于智能指針的章節解析的非常清晰&#xff0c;一解我以前的多處困惑。C面試過程中&#xff0c;很多面試官都喜歡問智能指針相…

非常可樂——BFS

【題目描述】 大家一定覺的運動以后喝可樂是一件很愜意的事情&#xff0c;但是seeyou卻不這么認為。因為每次當seeyou買了可樂以后&#xff0c;阿牛就要求和seeyou一起分享這一瓶可樂&#xff0c;而且一定要喝的和seeyou一樣多。但seeyou的手中只有兩個杯子&#xff0c;它們的容…

整型數據存儲

//代碼1 #include<stdio.h> int main() {char a -1;signed char b -1;unsigned char c -1;printf("a %d, b %d, c %d", a, b, c);return 0; } 1000 0000 0000 0001 -> -1源碼 1111 1111 1111 1110 -> -1反碼 1111 1111 1111 1111 -> -1補碼 對于…

聊聊gcc參數中的-I, -L和-l

http://blog.csdn.net/stpeace/article/details/49408665 在本文中&#xff0c; 我們來聊聊gcc中三個常見的參數&#xff0c; 也即-I, -L和-l 一. 先說 -I (注意是大寫的i) 我們先來看簡單的程序&#xff1a; main.c: [cpp] view plaincopy #include <stdio.h> #incl…

Pots——BFS

【題目描述】 You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap; DROP(i) empty the pot i to the drain; POUR(i,j) pour from pot i to pot j;…

HDU - 4578Transformation——線段樹+區間加法修改+區間乘法修改+區間置數+區間和查詢+區間平方和查詢+區間立方和查詢

【題目描述】 HDU - 4578Transformation Problem Description Yuanfang is puzzled with the question below: There are n integers, a1, a2, …, an. The initial values of them are 0. There are four kinds of operations. Operation 1: Add c to each number between ax …

[C++基礎]034_C++模板編程里的主版本模板類、全特化、偏特化(C++ Type Traits)

http://www.cnblogs.com/alephsoul-alephsoul/archive/2012/10/18/2728753.html 1. 主版本模板類 首先我們來看一段初學者都能看懂&#xff0c;應用了模板的程序&#xff1a; 1 #include <iostream>2 using namespace std;3 4 template<class T1, class T2>5 clas…

自定義類型: 結構體,枚舉,聯合

1.結構體 個人認為結構體和數組特別相似&#xff0c;只不過結構體和數組的區別在于結構體的成員可以是不同類型&#xff0c;而數組成員類型是相同的。 &#xff08;1&#xff09;結構體的聲明 struct tag {成員列表//至少得有一個成員 }值列表;//值列表可以省略 struct {int a…