C/C++使用使用MySQL
一直找不到关于C/C++连接Mysql数据库的详细api书籍和网站,刷了下网页,找到一篇Linux 下C/C++连接数据库的博
客,留着以后自己用。
首先需要编译、安装MySQL,安装完成后,将MySQL目录中的lib目录添加到环境变量中。新建C/C++工程,把
$MYSQL_ROOT/include添加到编译环境的包含路径下面。在编译选项中,增加$MYSQL_ROOT/lib目录。在Link选项中增加-
lmysqlclient(已经把lib目录增加到系统环境变量中),或者直接引用libmysqlclient.so文件。
1 不多说了,直接上代码,注释都很详细。
2 /*
3 * MySQLManager.h
4 *
5 * Created on: Feb 18, 2009
6 * Author: Steven Wee
7 */
8
9 #ifndef MYSQLMANAGER_H_
10 #define MYSQLMANAGER_H_
11
12 #include "../Common/CheckStringTools.h"
13
14 #include < mysql.h>
15
16 #include < string>
17 #include < iostream>
18 #include < vector>
19
20 #include < string.h>
21
22 using namespace std;
23
24 class MySQLManager
25 {
26 public:
27 /*
28 * Init MySQL
29 * @param hosts: Host IP address
30
31 * @param userName: Login UserName
32 * @param password: Login Password
33 * @param dbName: Database Name
34 * @param port: Host listen port number
35 */
36 MySQLManager(std::string hosts, std::string userName, std::string password, std::string dbName, unsigned
int port);
37 ~MySQLManager();
38 void initConnection();
39 /*
40 * Making query from database
41 * @param mysql: MySQL Object
42 * @param sql: Running SQL command
43 */
44 bool runSQLCommand(std::string sql);
45 /**
46 * Destroy MySQL object
47 * @param mysql MySQL object
48 */
49 void destroyConnection();
50 bool getConnectionStatus();
51 vector< vector< string> > getResult();
52 protected:
53 void setUserName(std::string userName);
54 void setHosts(std::string hosts);
55 void setPassword(std::string password);
56 void setDBName(std::string dbName);
57 void setPort(unsigned int port);
58 private:
59 bool IsConnected;
评论0
最新资源