利用mysql提供的c语言接口操作数据库

http://blog.csdn.net/bladeandmaster88/article/details/52980872

  1. //1.工程要在c/c++->常规->附加包含目录添加mysql.h的路径D:\mysql5.5\include  
  2. //2.工程要在链接器->常规->附加库目录添加libmysql.lib的路径D:\mysql5.5\lib  
  3.   
  4. #include <WinSock2.h>//mysql的库用到了windows网络接口  
  5. #include <mysql.h>  
  6.   
  7. #pragma comment(lib, "libmysql.lib")  
  8.   
  9. #include <iostream>  
  10. using namespace std;  
  11.   
  12. int main()  
  13. {  
  14.     MYSQL mysql;  
  15.     MYSQL_RES *res;  
  16.     MYSQL_ROW row;  
  17.   
  18.     // 初始化MYSQL变量  
  19.     mysql_init(&mysql);  
  20.   
  21.     // 连接Mysql服务器,本例使用本机作为服务器。访问的数据库名称为"student",参数中的user为你的登录用户名,***为登录密码,  
  22.     //需要根据你的实际用户进行设置  
  23.     //如果连接成功,返回MYSQL*连接句柄。如果连接失败,返回NULL  
  24.     if(!mysql_real_connect(&mysql, "127.0.0.1""root""123456""student", 3306, 0, 0))  
  25.     {  
  26.         cout << "mysql_real_connect failure!" << endl;  
  27.         return 0;  
  28.     }  
  29.   
  30.     // 查询mysql数据库中的t_student表  
  31.     //如果查询成功,函数返回零。如果发生一个错误,函数返回非零。  
  32.     if(mysql_real_query(&mysql, "select * from t_student", (unsigned long)strlen("select * from t_student")))  
  33.     {  
  34.         cout << "mysql_real_query failure!" << endl;  
  35.         return 0;  
  36.     }  
  37.   
  38.     // 存储结果集  
  39.     res = mysql_store_result(&mysql);  
  40.     if(NULL == res)  
  41.     {  
  42.         cout << "mysql_store_result failure!" << endl;  
  43.         cout << mysql_error(&mysql) << endl;  
  44.         return 0;  
  45.     }  
  46.   
  47.     // 重复读取行,并输出第一个字段的值,直到row为NULL  
  48.   
  49.     while(row = mysql_fetch_row(res))  
  50.     {  
  51.         cout << row[0] << endl;  
  52.     }  
  53.   
  54.     // 释放结果集  
  55.     mysql_free_result(res);  
  56.   
  57.     // 关闭Mysql连接  
  58.     mysql_close(&mysql);  
  59.   
  60.     return 0;  
  61. }  


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/384165.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数组相关运算

数组的初始化 数组及指针在内存中的存储 一维数组在内存中的存储 有关数组的运算 //一维数组 int a[] {1,2,3,4}; printf("%d\n",sizeof(a));//16这里的a表示的是整个数组,计算出的是整个数组的大小,单位为byte printf("%d\n",sizeof(a 0));/*a没有单独…

Find The Multiple——简单搜索+大胆尝试

【题目描述】 Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more t…

C语言操作MYSQL小例子

http://blog.csdn.net/small_qch/article/details/8180678 初学使用用C语言操作MYSQL&#xff0c;写了个小例子&#xff0c;帖上来献丢人一下&#xff0c;呵呵。 程序很简单&#xff0c;先连接数据库&#xff0c;然后向class1表中插入一条数据&#xff0c;最后获取并输出整个cl…

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;…