码迷,mamicode.com
首页 > 数据库 > 详细

C/C++使用MySQL

时间:2015-10-12 23:56:39      阅读:325      评论:0      收藏:0      [点我收藏+]

标签:

  一直找不到关于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;    
 60         vector<  vector< string> > resultList;    
 61         MYSQL mySQLClient;    
 62         unsigned int DEFAULTPORT;    
 63         char * HOSTS;    
 64         char * USERNAME;    
 65         char * PASSWORD;    
 66         char * DBNAME;    
 67 };    
 68 
 69 #endif /* MYSQLMANAGER_H_ */ 
 70 
 71 /*    
 72 * MySQLManager.cpp    
 73 *    
 74 *    Created on: Feb 18, 2009    
 75 *            Author: Steven Wee    
 76 */    
 77 #include "MySQLManager.h"    
 78 
 79 MySQLManager::MySQLManager(string hosts, string userName, string password, string dbName, unsigned int port)    
 80 
 81 {    
 82         IsConnected = false;    
 83         this ->setHosts(hosts);            //    设置主机IP地址    
 84         this ->setUserName(userName);            //    设置登录用户名    
 85         this ->setPassword(password);            //    设置登录密码    
 86         this ->setDBName(dbName);            //    设置数据库名    
 87         this ->setPort(port);            //    设置端口号    
 88 }    
 89 
 90 MySQLManager::~MySQLManager()    
 91 {    
 92         this ->destroyConnection();    
 93 }    
 94 
 95 void MySQLManager::setDBName(string dbName)    
 96 {    
 97         if ( dbName.empty() )    
 98         {//        用户没有指定数据库名    
 99                 std::cout < <  "DBName is null! Used default value: mysql" < <  std::endl;    
100                 this ->DBNAME = new char[5];    
101                 strcpy(this ->DBNAME, "mysql");    
102         }    
103         else    
104         {    
105                 this ->DBNAME = new char[dbName.length()];    
106                 strcpy(this ->DBNAME, dbName.c_str());    
107         }    
108 }    
109 
110 void MySQLManager::setHosts(string hosts)    
111 {    
112         if ( hosts.empty() )    
113         {//    用户没有指定数据库IP地址    
114                 std::cout < <  "Hosts is null! Used default value: localhost" < <  std::endl;    
115                 this ->HOSTS = new char[9];    
116                 strcpy(this ->HOSTS, "localhost");    
117         }    
118         else    
119         {    
120                 this ->HOSTS = new char[hosts.length()];    
121                 strcpy(this ->HOSTS, hosts.c_str());    
122         }    
123 }    
124 
125 void MySQLManager::setPassword(string password)    
126 {//    用户没有指定密码    
127         if ( password.empty() )    
128         {    
129 
130                 std::cout < <  "Password is null! Used default value: " < <  std::endl;    
131                 this ->PASSWORD = new char[1];    
132                 strcpy(this ->PASSWORD, "");    
133         }    
134         else    
135         {    
136                 this ->PASSWORD = new char[password.length()];    
137                 strcpy(this ->PASSWORD, password.c_str());    
138         }    
139 }    
140 
141 void MySQLManager::setPort(unsigned int port)    
142 {//    用户没有指定端口号,使用默认端口号    
143         if ( port )    
144         {    
145                 std::cout < <  "Port number is null! Used default value: 0" < <  std::endl;    
146                 this ->DEFAULTPORT = 0;    
147         }    
148         else    
149         {    
150                 this ->DEFAULTPORT = port;    
151         }    
152 }    
153 
154 void MySQLManager::setUserName(string userName)    
155 {//    用户没有指定登录用户名    
156         if ( userName.empty() )    
157         {    
158                 std::cout < <  "UserName is null! Used default value: root" < <  std::endl;    
159                 this ->USERNAME = new char[4];    
160                 strcpy(this ->USERNAME, "root");    
161         }    
162         else    
163         {    
164                 this ->USERNAME = new char[userName.length()];    
165                 strcpy(this ->USERNAME, userName.c_str());    
166         }    
167 }    
168 
169 void MySQLManager::initConnection()    
170 {    
171         if ( IsConnected )    
172         {//    已经连接到服务器    
173                 std::cout < <  "Is connected to server!" < < std::endl;    
174                 return;    
175         }    
176         mysql_init(&mySQLClient);//    初始化相关对象    
177         if ( !mysql_real_connect( &mySQLClient, HOSTS, USERNAME, PASSWORD, DBNAME, DEFAULTPORT, NULL, 0) )    
178 
179         {//    连接到服务器    
180                 std::cout < <  "Error connection to database: %s\n" < <  mysql_error(&mySQLClient) < <  std::endl;    
181         }    
182         IsConnected = true;//    修改连接标识    
183 }    
184 
185 bool MySQLManager::runSQLCommand(string sql)    
186 {    
187         if ( !IsConnected )    
188         {//    没有连接到服务器    
189                 std::cout < <  "Not connect to database!" < <  std::endl;    
190                 return false;    
191         }    
192         if ( sql.empty() )    
193         {//    SQL语句为空    
194                 std::cout < <  "SQL is null!" < <  std::endl;    
195                 return false;    
196         }    
197 
198         MYSQL_RES *res;    
199         MYSQL_ROW row;    
200 
201         unsigned int i,j = 0;    
202 
203         StringTools stringTools;    
204         sql = stringTools.filterString(sql);    
205 
206         i = mysql_real_query(&mySQLClient,sql.c_str(),(unsigned int)strlen(sql.c_str()));//    执行查询    
207         if ( i )    
208         {    
209                 std::cout < <  "Error query from database: %s\n" < <  mysql_error(&mySQLClient) < <  std::endl;    
210                 return false;    
211         }    
212         res = mysql_store_result(&mySQLClient);    
213         vector< string> objectValue;    
214         while( (row = mysql_fetch_row(res)) )    
215         {//    遍历结果集    
216                 objectValue.clear();    
217                 for ( j = 0 ; j <  mysql_num_fields(res) ; j   )    
218                 {    
219                         objectValue.push_back(row[j]);    
220                 }    
221                 this ->resultList.push_back(objectValue);    
222         }    
223         mysql_free_result(res);         //free result after you get the result    
224 
225 
226 
227         return true;    
228 }    
229 
230 vector<  vector< string> > MySQLManager::getResult()    
231 {    
232         return resultList;    
233 }    
234 
235 void MySQLManager::destroyConnection()    
236 {    
237         mysql_close(&mySQLClient);    
238         this ->IsConnected = false;    
239 }    
240 
241 bool MySQLManager::getConnectionStatus()    
242 {    
243         return IsConnected;    
244 } 

来自:http://club.topsage.com/forum.php?mod=viewthread&tid=2129419

C/C++使用MySQL

标签:

原文地址:http://www.cnblogs.com/houjun/p/4873099.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!