后来又把这些函数简单的封装了一下,方便以后直接用。
/************************************************************************* > File Name: myDB.h > Author: Tanswer_ > Mail: 98duxm@gmail.com > Created Time: 2017年05月28日 星期日 22时26分22秒 ************************************************************************/ #ifndef _MYDB_H #define _MYDB_H #include <string> #include <iostream> #include <mysql/mysql.h> using namespace std; class MyDB { public: MyDB(); ~MyDB(); bool InitDB(string host,string user,string pwd,string dbname); bool ExeSQL(string sql); private: MYSQL* mysql; MYSQL* mysql; MYSQL_ROW row; MYSQL_RES* result; MYSQL_FIELD* field; }; #endif /************************************************************************* > File Name: myDB.cpp > Author: Tanswer_ > Mail: 98duxm@gmail.com > Created Time: 2017年05月28日 星期日 22时27分18秒 ************************************************************************/ #include <iostream> #include <string> #include <stack> #include <algorithm> #include <mysql/mysql.h> #include "myDB.h" using namespace std; MyDB::MyDB() { mysql = mysql_init(NULL); if(mysql == NULL) { ┊ cout << "Error: " << mysql_error(mysql); ┊ exit(-1); } } MyDB::~MyDB() { if(!mysql) { ┊ mysql_close(mysql); } } bool MyDB::InitDB(string host,string user,string pwd,string dbname) { /*连接数据库*/ if(!mysql_real_connect(mysql,host.c_str(),user.c_str(),pwd.c_str(),dbname.c_str(),0,NULL,0)) { ┊ cout << "connect fial: " << mysql_error(mysql); ┊ exit(-1); } return true; } bool MyDB::ExeSQL(string sql) { /*执行失败*/ if(mysql_query(mysql,sql.c_str())) { ┊ cout << "query fail: " << mysql_error(mysql); ┊ exit(1); } else { ┊ /*获取结果集*/ ┊ result = mysql_store_result(mysql); ┊ int fieldnum = mysql_num_fields(result); ┊ for(int i=0; i<fieldnum; i++) ┊ { ┊ ┊ row = mysql_fetch_row(result); ┊ ┊ if(row <= 0) ┊ ┊ ┊ break; ┊ ┊ for(int j=0; j<fieldnum; j++) ┊ ┊ { ┊ ┊ ┊ cout << row[j] << "\t\t"; ┊ ┊ } ┊ ┊ cout << endl; ┊ } ┊ mysql_free_result(result); } return true; } /************************************************************************* > File Name: main.cpp > Author: Tanswer_ > Mail: 98duxm@gmail.com > Created Time: 2017年05月28日 星期日 22时53分43秒 ************************************************************************/ #include <iostream> #include <string> #include <stack> #include <algorithm> #include <mysql/mysql.h> #include "myDB.h" using namespace std; int main() { MyDB db; db.InitDB("localhost","root","xxxxxx","Student"); db.ExeSQL("SELECT * FROM Infor;"); return 0; }
以下是运行结果:
下面是遇到的问题:
1. 编译时出错
没有那个文件或目录
#include<mysql/mysql.h> ^
编译中断。
解决:除了mysql-client和mysql-server,又安装了mysql-devel,然后就解决了。
2. 自定义的变量传入sql语句时,出现问题
在网上查找到这样一种格式,
string sql = "INSERT INTO Infor (name,sex,tel,addr,age) values('"+fname+"','"+fsex+"','"+ftel+"','"+faddr+"', "+IntToStr(fage)+");";
然后string类型的可以成功,整型的变量还是不行,我又写了个函数把int转为string。
string IntToStr(int num) { stringstream ss; ss.clear(); ss << num; return ss.str(); }
大概就是这样,门卫大叔很凶,我也很绝望,就写到这吧,有问题再补充。
更多Mysql内容来自木庄网络博客
标签:Mysql
相关阅读 >>
更多相关阅读请进入《mysql》频道 >>

数据库系统概念 第6版
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。