新学期刚才始,这学期开了两门关于dbms的课程:数据库原理及数据库应用,so今儿就先抛开原理不说,搞了个轻量级的开源数据库mysql小试一下.其实一年多前就用过mysql,今儿也就算重温了一下.记得当时就是学了学SQL和mysql的基本管理,并没有在程序中使用数据库,所以这次目标很明确~~~
当不知道从何处下手的时间总是会google,这回不例外.查看一些文章和别人的blog以后发现只要用mysql developer tool提供的C API即可.头文件:#include <mysql.h>
仅仅加入这一行是不能通过编译的,还要:工具->选项->项目与解决方案->VC++目录,在"显示以下内容"列表框中选择"包含文件",然后把"C:\MySQL\MySQL Server 5.1\include"加到下面列表中.接着在"显示以下内容"列表框中选择"库文件",再把"C:\MySQL\MySQL Server 5.1\lib\debug"加到下面列表中.
最后把代码copy到main.cpp文件中,代码如下(网上的一个例子,这里我修改了一些地方以便程序适合本地的环境):
1#include <windows.h>
2#include <iostream>
3#include <mysql.h>
4#include <stdio.h>
5
6using namespace std;
7
8int main()
9{
10 //connection params
11 //FILE *file;
12 char *host = "localhost";
13 char *user = "root";
14 char *pass = "123456";
15 char *db = "mysql";
16
17 //sock
18 MYSQL *sock;
19 MYSQL_RES *results;
20 MYSQL_ROW record;
21 sock = mysql_init(0);
22 if (sock)
23 cout << "sock handle ok!" << endl;
24 else
25 {
26 cout << "sock handle failed!" << mysql_error(sock) << endl;
27 }
28
29 //connection
30 if (mysql_real_connect(sock, host, user, pass, db, 0, NULL, 0))
31 cout << "connection ok!" << endl;
32 else
33 {
34 cout << "connection fail: " << mysql_error(sock) << endl;
35 }
36 mysql_set_character_set(sock,"gb2312");
37
38 //connection character set
39 cout << "connection character set: " << mysql_character_set_name(sock) << endl;
40
41 //wait for posibility to check system/mysql sockets
42 if(mysql_query(sock,"select * from user"))
43 {
44 cout<<"very good!"<<endl;
45 }
46 results=mysql_store_result(sock);
47 printf("主机\t用户名\n");
48 while(record = mysql_fetch_row(results))
49 {
50 printf("%s\t%s \n",record[0],record[1]);
51 }
52 mysql_free_result(results);
53 //system("PAUSE");
54
55 //closing connection
56 mysql_close(sock);
57
58 return EXIT_SUCCESS;
59}
2#include <iostream>
3#include <mysql.h>
4#include <stdio.h>
5
6using namespace std;
7
8int main()
9{
10 //connection params
11 //FILE *file;
12 char *host = "localhost";
13 char *user = "root";
14 char *pass = "123456";
15 char *db = "mysql";
16
17 //sock
18 MYSQL *sock;
19 MYSQL_RES *results;
20 MYSQL_ROW record;
21 sock = mysql_init(0);
22 if (sock)
23 cout << "sock handle ok!" << endl;
24 else
25 {
26 cout << "sock handle failed!" << mysql_error(sock) << endl;
27 }
28
29 //connection
30 if (mysql_real_connect(sock, host, user, pass, db, 0, NULL, 0))
31 cout << "connection ok!" << endl;
32 else
33 {
34 cout << "connection fail: " << mysql_error(sock) << endl;
35 }
36 mysql_set_character_set(sock,"gb2312");
37
38 //connection character set
39 cout << "connection character set: " << mysql_character_set_name(sock) << endl;
40
41 //wait for posibility to check system/mysql sockets
42 if(mysql_query(sock,"select * from user"))
43 {
44 cout<<"very good!"<<endl;
45 }
46 results=mysql_store_result(sock);
47 printf("主机\t用户名\n");
48 while(record = mysql_fetch_row(results))
49 {
50 printf("%s\t%s \n",record[0],record[1]);
51 }
52 mysql_free_result(results);
53 //system("PAUSE");
54
55 //closing connection
56 mysql_close(sock);
57
58 return EXIT_SUCCESS;
59}
最后运行结果如下:
sock handle ok!
connection ok!
connection character set: gb2312
主机 用户名
localhost root
请按任意键继续. . .
就这点使用方法总结,望大家在VS2005上使用C++开发db应用有个顺利的入门.