Local

..:: Home
..:: Legal
..:: Contact
..:: About
..:: RSS
..:: Log in

Links

..:: Fake Bill Gates
..:: Tap the Hive

A Random Quote

"Quidquid latine dictum sit, altum sonatur." -- Whatever is said in Latin sounds profound.

C++ MySQL Database Driver: class.mysql.cpp

I started coding (substantially) in C++ yesterday (outside of regular class assignments), and as I looked through a lot of the database driver classes, I couldn’t find any that could compare to the ease of use of current php based database drivers. I coded in C in undergrad in my OS class, but dealing with 30 year old minix code is worthless for the purpose of learning C. Comparatively, OO is incredibly new, and this ease of usability is incomparable to the jumbled mess in minix. This is tested and working. I know that it’s not simple, and a lot of very experienced C++ coders will probably balk at some of this, but after php, I’m spoiled.

#include </usr/include/mysql/mysql.h>
#include <iostream>
using namespace std;
#include <time.h>

class Database
{

private:

        char *host;
        char *database;
        char *username;
        char *password;
        MYSQL *link;

public:

        Database(char *const new_host, char *const new_database, char *const new_username, char *const new_password)
        {
                host = new_host;
                database = new_database;
                username = new_username;
                password = new_password;
                link = mysql_init(NULL);
                link = mysql_real_connect(link, host, username, password, database, 0, NULL, 0);
        }

        void print()
        {
                cout << "Host: " << host << "\n";
                cout << "Database: " << database << "\n";
                cout << "Username: " << username << "\n";
                cout << "Password: " << password << "\n";
        }

        MYSQL_RES query(char *const query)
        {
                MYSQL_RES *res_set;
                cout << "Executing query: " << query << "\n";
                mysql_query(link, query);
                res_set = mysql_store_result(link);
                return *res_set;
        }

        int num_rows(MYSQL_RES *res_set)
        {
                int i =  mysql_num_rows(res_set);
                return i;
        }

        MYSQL_ROW fetch_row(MYSQL_RES *res_set)
        {
                MYSQL_ROW row = mysql_fetch_row(res_set);
                return row;
        }

        void close()
        {
                mysql_close(link);
        }
}; 

int main()
{
        Database db = Database("localhost", "test", "username", "password");
        db.print();
        char *query = "select * from test_table";
        MYSQL_RES result = db.query(query);
        MYSQL_ROW row;
        cout << "row: id | value | timestamp\n";
        while ((row = db.fetch_row(&result)) != NULL)
        {
                cout << "row: " << row[0] << ” | ” << row[1] << ” | ” << row[2] << “\n”;
        }
        db.close();

}

Leave a Reply