MySQL Header

I threw together a useful little header that's intended to make MySQL quick and easy to manage from within a C++ application. Just wanted to post it here and get some opinions on it...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// EsKueEl.h

#include <iostream> // input/output stream
#include <mysql.h> // so the ksql class can use mysql o.o
#include <string> // so we can use strings
using namespace std; // the standard namespace o.o

class qksql { // QKSQL : Quick MySQL
	public: // the public functions and variables
		void connect(char host[1000], char usr[1000], char pass[1000], char db[1000]); // the connect function...
		void disconnect(); // time to go bye bye!
		int query(char myquery[10000]); // query my query pl0x
		char query_result(); // a char-returning function... O.o
		int query_state; // the query_state o.o yeah, its public.
		MYSQL *connection, mysql; // mysql stuff...
		MYSQL_RES *result; // mysql stuff....
		MYSQL_ROW row; // mysql stuff...
		void max_res(int x); // use this function to set the max results, this is good for a simple querying program
	private: // getting to the private variables!
		int i; // the count up int :O
		int t; // the function'd row
}; // the end... :O

void qksql::connect(char host[1000], char usr[1000], char pass[1000], char db[1000]) { // connection with defined variables
	mysql_init(&mysql); // initiating mysql :O
	//connection = mysql_real_connect(&mysql,"host","user","password","database",0,0,0);
	connection = mysql_real_connect(&mysql,host,usr,pass,db,0,0,0);
	if (connection == NULL) { // if the connection fails
		cout << mysql_error(&mysql) << endl; // give us an error
	}
}

void qksql::disconnect() { // disconnectz0r
	mysql_free_result(result); // freeing query results
	mysql_close(connection); // dropping the connection
}

int qksql::query(char myquery[10000]) { // query teh stringz
	query_state = mysql_query(connection, myquery); // querying!
	if (query_state != 0) { // if the query state has an error...
		cout << mysql_error(connection) << endl; // show us what happened.
		return 1; // returning with error code 1 :O
	}
}

char qksql::query_result() {
	result = mysql_store_result(connection); // results!
	while ( ( row = mysql_fetch_row(result)) != NULL ) { // OMG! lets see if the results are working
		fast: // heh... yes, ik, ik... goto is very bad...
		if (i < t) { // if i < t
			cout << row[i] << endl;; // output row[i]
			++i; // add 1 to i
			goto fast; // go to fast :O
		} // O.o why was this a char again?
	} // seriously, it has no return value
} // whatever... i forget :D

void qksql::max_res(int x) { // the max res function
	t = x; // private T is now equal to incoming X
} // bye bye! 



NOTE :
Compiling with a UNIX command line compiler? Include the code below at the end of your compiler command.


COMMAND :
-L/usr/include/mysql -lmysqlclient -I/usr/include/mysql


EXAMPLE (g++) :
g++ file.cpp -L/usr/include/mysql -lmysqlclient -I/usr/include/mysql
Last edited on
Two things to start off with:
1. This isn't really a header is it?
2. Why are you using char arrays?
1) i used it as one and it worked fine. Is there really a qualification for what a header does? it just saves you time so you don't have to type all of this out. its as simple as
qksql [variablename];
and you can use functions.
2) Because that's how you put input into all of these o.o, what else would you use? strings don't work with them.
are you aware of all the capabilities strings offer to you?...
If myquery was an std::string, you could do mysql_query(connection, myquery.c_str()).
hmm, thanks helios, that solved should solve some of the problems i was having with it.
@Incubbus,
you are aware that a char array is essentially a string, right?
also, i just wanted a change of pace, i ALWAYS use strings.
Last edited on
Incubbus was talking about std::strings, or any other kind of string object, not null-terminated char arrays.

Unless you're doing some sort of high speed string processing (and when I say high speed, I mean high speed) it's not worth to risk having all the potential problems std::strings prevent.
With regard to the Is it or is it not a header thing - I was trying to point out
that if you #include "EsKueEl.h" in more than one file - you will end up with
the linker giving redefinition errors.
even if myquery is not an null-terminated string you could do this by using string::data() its the string::c_str() without the NULL-character at the end...

so you could use it for the mysql_query and for real connect etc...

i never ment to offend you - if it did sound like...


and as guestgulkan said... dont forget the include warden/guard
To avoid the issue guestgulkan pointed out...

#ifndef _MY_HEADER_H_
#define _MY_HEADER_H_

myCode;

#endif // _MY_HEADER_H_ not defined

... should work.
alright, thanks chris, i'll add that to the one i've got saved. although i have yet to add the changes everyone's mentioned, i didn' have internet for most of today, and the thing helios mentioned originally i didn't add yet. i guess i'll add it now or in a bit. thanks though.

{EDIT} Just incorperated the changes into my file. Anything else? (if that's it, i might as well replace the goto with a for statement :O)
Last edited on
Topic archived. No new replies allowed.