blackgoddess vive le troll ! | la lib utilisé ... c la dll fournie avec le serveur mysql sous windows
le compilo : vc++7
le code entier ... le voici :
mysqlconnection.h
Code :
- #ifndef mysqlconnection_h
- #define mysqlconnection_h
- #define NO_CLIENT_LONG_LONG
- #define __WIN__
- #pragma comment(lib, "libmySQL.lib" )
- #include <mysql.h> // Headers for MySQL usage
- namespace mysql
- {
- class Exception : private exception
- {
- string _reason;
- string _host;
- string _database;
- public:
- Exception();
- Exception(const string & reason);
- Exception(const string & reason, const string & host, const string & database);
- Exception(MYSQL* db, const string & host, const string & database);
- Exception(const Exception & source);
- const Exception & operator = (const Exception & source);
- const string & Reason();
- const string & Host();
- const string & DataBase();
- };
- class Connection
- {
- MYSQL _db;
- bool _isopen;
- string _host;
- string _database;
- string _username;
- string _userpass;
- public:
- Connection();
- Connection(const string & host, const string & database, const string & username, const string & userpass);
- ~Connection();
- void Open();
- void Open(const string & host, const string & database, const string & username, const string & userpass);
- void Close();
- string PrepareString(const string & str);
- unsigned long ExecuteQuery(const string & query, unsigned long* InsertID);
- unsigned long ExecuteQuery(const string & query);
- };
- }
- #endif
|
mysqconnection.cpp
Code :
- #include <winsock.h>
- #include <iostream>
- #include <string>
- using namespace std;
- #include "mysqlconnection.h"
- namespace mysql
- {
- Exception::Exception()
- {
- }
- Exception::Exception(const string & reason)
- : _reason(reason)
- {
- }
- Exception::Exception(const string & reason, const string & host, const string & database)
- : _reason(reason), _host(host), _database(database)
- {
- }
- Exception::Exception(MYSQL* db, const string & host, const string & database)
- : _reason(mysql_error(db)), _host(host), _database(database)
- {
- }
- Exception::Exception(const Exception & source)
- : _reason(source._reason), _host(source._host), _database(source._database)
- {
- }
- const Exception & Exception::operator = (const Exception & source)
- {
- _reason = source._reason;
- _host = source._host;
- _database = source._database;
- return *this;
- }
- const string & Exception::Reason()
- {
- return _reason;
- }
- const string & Exception::Host()
- {
- return _host;
- }
- const string & Exception::DataBase()
- {
- return _database;
- }
- Connection::Connection()
- : _isopen(false)
- {
- }
- Connection::Connection(const string & host, const string & database, const string & username, const string & userpass)
- : _isopen(false), _host(host), _database(database), _username(username), _userpass(userpass)
- {
- }
- Connection::~Connection()
- {
- if(_isopen)
- Close();
- }
- void Connection::Open()
- {
- if(_isopen)
- throw Exception("already open", _host, _database);
- if(!mysql_connect(&_db, _host.c_str(), _username.c_str(), _userpass.c_str()))
- throw Exception(&_db, _host, _database);
- if(mysql_select_db(&_db, _database.c_str()))
- {
- mysql_close(&_db);
- throw Exception(&_db, _host, _database);
- }
- _isopen = true;
- }
- void Connection::Open(const string & host, const string & database, const string & username, const string & userpass)
- {
- _host = host;
- _database = database;
- _username = username;
- _userpass = userpass;
- Open();
- }
- void Connection::Close()
- {
- if(_isopen)
- {
- mysql_close(&_db);
- }
- else
- throw Exception("not open" );
- }
- string Connection::PrepareString(const string & str)
- {
- char* encdata = new char[2*str.length() + 1];
- int datasize = mysql_real_escape_string(&_db, encdata, str.c_str(), (unsigned long)str.length());
- string ret(encdata, datasize);
- delete encdata;
- return ret;
- }
- unsigned long Connection::ExecuteQuery(const string & query)
- {
- return ExecuteQuery(query, 0);
- }
- unsigned long Connection::ExecuteQuery(const string & query, unsigned long* InsertID)
- {
- if(!_isopen)
- throw Exception("not open" );
- if(mysql_real_query(&_db, query.c_str(), (unsigned int)query.length()))
- throw Exception(&_db, _host, _database);
- if(InsertID) *InsertID = mysql_insert_id(&_db);
- return mysql_affected_rows(&_db);
- }
- }
|
main.cpp
Code :
- #include <winsock.h>
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- #include "mysqlconnection.h"
- int main()
- {
- mysql::Connection c("serveur", "test", "root", "" );;
- try
- {
- c.Open();
- }
- catch(mysql::Exception &e)
- {
- cout << e.Reason();
- }
- }
|
Message édité par blackgoddess le 25-10-2003 à 00:52:52 ---------------
-( BlackGoddess )-
|