MySQL connection strange behavior (again...)

miazo miazo at no.spam.please
Wed Feb 22 04:06:41 PST 2012


Hello,

I'm facing a strange problem(s) with connecting from D to MySQL
database under Windows XP. I've read almost everything on this
newsgroup concerning database connectivity, I've checked D
libraries written by others, I've read MySQL documentation on its
C API, I searched other sites for similar problems and still I
have no clue what's going on. Perhaps it's something really
trivial?

So, here is what I have done:

- I have MySQL database (version 5.5.20) running on localhost (I
can query it without any problems with its command line tool, so
it's OK).

- I converted libmysql.dll to libmysql.lib with implib.exe tool
with /system option and put resulting libmysql.lib file in
windows\lib directory of DMD compiler.

- The following test code:

	pragma(lib, "libmysql");

	extern (C) {
		alias void MYSQL;
		MYSQL* mysql_init(MYSQL* mysql);
	}

	void main() {
		MYSQL* c = mysql_init(null);
	}

compiles and runs but ends with object.Error: Access Violation
message. The "strange" part is that:

- mysql_init function runs fine - as I understand its purpose, it
just allocates memory for MySQL connection and I can check that
it returns a not-null pointer

- If I put mysql_init in another function like:

	pragma(lib, "libmysql");

	extern (C) {
		alias void MYSQL;
		MYSQL* mysql_init(MYSQL* mysql);
	}

	MYSQL* init() {
		return mysql_init(null);
	}

	void main() {
		MYSQL* c = init();
	}

the program runs without any error message.

My guess is that it's something I'm doing wrong with binding from
C but I have no idea what it can be exactly, as:

- I have come exactly the same way (i.e. C bindings + making of
.lib file) with connecting to SQLite, Postgres and via ODBC and I
have not encounter such problems (I can connect and execute
queries).

- I can actually connect to MySQL and execute queries - the above
example is simplified. But as I work with MySQL database I see
more and more strange behavior, for instance: I use a variable to
store number of fields in a result (mysql_num_fields function
gives me that) and when I later want to get that number I see
something like 18454345 (but earlier it was just: 2 fields). So
it looks like something have corrupted the memory where I put my
data? Can it be GC (but I have no such problems with other DB
engines)? Or is it MySQL library? The most annoying part is that
very often I can't reproduce the problem - simply changing the
order of two commands in a program can fix or brake it.

- There was a known problem with unsigned long in MySQL bindings
but what I'm using is just uint.

- Maybe it has something to do with the database encodings? But
my database uses utf-8 and I set it to utf-8 for session just
after opening a new connection (with mysql_set_character_set
function).

Thank you for your help and any ideas that can help me to debug
it further.

PS. I am aware of the fact that there are (were?) efforts to make
a common database library for D; nonetheless I just wanted to
learn D and as I have database background writing that kind of
library seemed most familiar to me.



More information about the Digitalmars-d-learn mailing list