首页 / C++ / C/C++使用MySQL
C/C++使用MySQL
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C/C++使用MySQL,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6318字,纯文字阅读大概需要10分钟。
内容图文
一直找不到关于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 22usingnamespace std; 23 24class MySQLManager 25{ 26public: 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(); 38void initConnection(); 39/* 40 * Making query from database 41 * @param mysql: MySQL Object 42 * @param sql: Running SQL command 43*/ 44bool runSQLCommand(std::string sql); 45/** 46 * Destroy MySQL object 47 * @param mysql MySQL object 48*/ 49void destroyConnection(); 50bool getConnectionStatus(); 51 vector< vector< string> > getResult(); 52protected: 53void setUserName(std::string userName); 54void setHosts(std::string hosts); 55void setPassword(std::string password); 56void setDBName(std::string dbName); 57void setPort(unsigned int port); 58private: 59bool IsConnected; 60 vector< vector< string> > resultList; 61 MYSQL mySQLClient; 62 unsigned int DEFAULTPORT; 63char * HOSTS; 64char * USERNAME; 65char * PASSWORD; 66char * 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; 83this ->setHosts(hosts); // 设置主机IP地址 84this ->setUserName(userName); // 设置登录用户名 85this ->setPassword(password); // 设置登录密码 86this ->setDBName(dbName); // 设置数据库名 87this ->setPort(port); // 设置端口号 88} 89 90 MySQLManager::~MySQLManager() 91{ 92this ->destroyConnection(); 93} 94 95void MySQLManager::setDBName(string dbName) 96{ 97if ( dbName.empty() ) 98 {// 用户没有指定数据库名 99 std::cout < < "DBName is null! Used default value: mysql" < < std::endl; 100this ->DBNAME = newchar[5]; 101 strcpy(this ->DBNAME, "mysql"); 102 } 103else104 { 105this ->DBNAME = newchar[dbName.length()]; 106 strcpy(this ->DBNAME, dbName.c_str()); 107 } 108} 109110void MySQLManager::setHosts(string hosts) 111{ 112if ( hosts.empty() ) 113 {// 用户没有指定数据库IP地址 114 std::cout < < "Hosts is null! Used default value: localhost" < < std::endl; 115this ->HOSTS = newchar[9]; 116 strcpy(this ->HOSTS, "localhost"); 117 } 118else119 { 120this ->HOSTS = newchar[hosts.length()]; 121 strcpy(this ->HOSTS, hosts.c_str()); 122 } 123} 124125void MySQLManager::setPassword(string password) 126 {// 用户没有指定密码 127if ( password.empty() ) 128 { 129130 std::cout < < "Password is null! Used default value: " < < std::endl; 131this ->PASSWORD = newchar[1]; 132 strcpy(this ->PASSWORD, ""); 133 } 134else135 { 136this ->PASSWORD = newchar[password.length()]; 137 strcpy(this ->PASSWORD, password.c_str()); 138 } 139} 140141void MySQLManager::setPort(unsigned int port) 142 {// 用户没有指定端口号,使用默认端口号 143if ( port ) 144 { 145 std::cout < < "Port number is null! Used default value: 0" < < std::endl; 146this ->DEFAULTPORT = 0; 147 } 148else149 { 150this ->DEFAULTPORT = port; 151 } 152} 153154void MySQLManager::setUserName(string userName) 155 {// 用户没有指定登录用户名 156if ( userName.empty() ) 157 { 158 std::cout < < "UserName is null! Used default value: root" < < std::endl; 159this ->USERNAME = newchar[4]; 160 strcpy(this ->USERNAME, "root"); 161 } 162else163 { 164this ->USERNAME = newchar[userName.length()]; 165 strcpy(this ->USERNAME, userName.c_str()); 166 } 167} 168169void MySQLManager::initConnection() 170{ 171if ( IsConnected ) 172 {// 已经连接到服务器 173 std::cout < < "Is connected to server!" < < std::endl; 174return; 175 } 176 mysql_init(&mySQLClient);// 初始化相关对象 177if ( !mysql_real_connect( &mySQLClient, HOSTS, USERNAME, PASSWORD, DBNAME, DEFAULTPORT, NULL, 0) ) 178179 {// 连接到服务器 180 std::cout < < "Error connection to database: %s\n" < < mysql_error(&mySQLClient) < < std::endl; 181 } 182 IsConnected = true;// 修改连接标识 183} 184185bool MySQLManager::runSQLCommand(string sql) 186{ 187if ( !IsConnected ) 188 {// 没有连接到服务器 189 std::cout < < "Not connect to database!" < < std::endl; 190returnfalse; 191 } 192if ( sql.empty() ) 193 {// SQL语句为空 194 std::cout < < "SQL is null!" < < std::endl; 195returnfalse; 196 } 197198 MYSQL_RES *res; 199 MYSQL_ROW row; 200201 unsigned int i,j = 0; 202203 StringTools stringTools; 204 sql = stringTools.filterString(sql); 205206 i = mysql_real_query(&mySQLClient,sql.c_str(),(unsigned int)strlen(sql.c_str()));// 执行查询 207if ( i ) 208 { 209 std::cout < < "Error query from database: %s\n" < < mysql_error(&mySQLClient) < < std::endl; 210returnfalse; 211 } 212 res = mysql_store_result(&mySQLClient); 213 vector< string> objectValue; 214while( (row = mysql_fetch_row(res)) ) 215 {// 遍历结果集 216 objectValue.clear(); 217for ( j = 0 ; j < mysql_num_fields(res) ; j ) 218 { 219 objectValue.push_back(row[j]); 220 } 221this ->resultList.push_back(objectValue); 222 } 223 mysql_free_result(res); //free result after you get the result 224225226227returntrue; 228} 229230 vector< vector< string> > MySQLManager::getResult() 231{ 232return resultList; 233} 234235void MySQLManager::destroyConnection() 236{ 237 mysql_close(&mySQLClient); 238this ->IsConnected = false; 239} 240241bool MySQLManager::getConnectionStatus() 242{ 243return IsConnected; 244 }
来自:http://club.topsage.com/forum.php?mod=viewthread&tid=2129419
原文:http://www.cnblogs.com/houjun/p/4873099.html
内容总结
以上是互联网集市为您收集整理的C/C++使用MySQL全部内容,希望文章能够帮你解决C/C++使用MySQL所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。