RejectedSoftware Forums

Sign up

mysql-native help

I'm trying to understand how to use mysql-native driver.

I have a database named "gksu" and a table named "news", the third column has the type "text". writeln() just outputs an array of some numbers. How can I convert them to the normal text/string?

Here is a part of my code:

        auto mdb = new MysqlDB("localhost", "gksu", "pass", "gksu");
	auto c = mdb.lockConnection();
	scope(exit){
		c.close();
	}
	
	try {
		c.selectDB("gksu");
		
		auto com = Command(c);
		
		com.sql = "SELECT * FROM news LIMIT 0, 30";
		ResultSet rs = com.execSQLResult();
		writeln(rs[0][2].toString());
	} catch(Exception x) {
		writeln("Execption: ", x.msg);
	}

test

Re: mysql-native help

Am 18.01.2014 16:11, schrieb Anton Alexeev:

I'm trying to understand how to use mysql-native driver.

I have a database named "gksu" and a table named "news", the third column has the type "text". writeln() just outputs an array of some numbers. How can I convert them to the normal text/string?

Here is a part of my code:

         auto mdb = new MysqlDB("localhost", "gksu", "pass", "gksu");
	auto c = mdb.lockConnection();
	scope(exit){
		c.close();
	}
	
	try {
		c.selectDB("gksu");
		
		auto com = Command(c);
		
		com.sql = "SELECT * FROM news LIMIT 0, 30";
		ResultSet rs = com.execSQLResult();
		writeln(rs[0][2].toString());
	} catch(Exception x) {
		writeln("Execption: ", x.msg);
	}

Sorry, I can't help with the direct question right now, because,
ironically, I haven't used the mysql driver myself (I would actually
rather like to pass the repository ownership to one of the maintainers).
Just a little comment about the snippet above - the scope(exit) isn't
necessary because c is already using RAII to free up the connection
for later reuse. Explicitly calling close will open a new connection
for each command that is sent to the server.

If nobody else can answer this quickly in the mean time, I'll have a
look at the source later to see what goes wrong.

Re: mysql-native help

On Sun, 19 Jan 2014 14:09:44 +0100, Sönke Ludwig wrote:

If nobody else can answer this quickly in the mean time, I'll have a
look at the source later to see what goes wrong.

OK, thank you for your reply. I will wait.

Re: mysql-native help

On Sun, 19 Jan 2014 16:58:40 GMT, Anton Alexeev wrote:

On Sun, 19 Jan 2014 14:09:44 +0100, Sönke Ludwig wrote:

If nobody else can answer this quickly in the mean time, I'll have a
look at the source later to see what goes wrong.

OK, thank you for your reply. I will wait.

Sorry that this is taking so long, I still didn't manage to install a MySQL server for testing.

A blind guess is that TEXT columns are stored internally as a ubyte[] instead of as string. In this case using cast(string)rs[0][2].get!(ubyte[]) instead of rs[0][2].toString() should do the trick.

Re: mysql-native help

On 2/9/2014 5:47 AM, Sönke Ludwig wrote:

On Sun, 19 Jan 2014 16:58:40 GMT, Anton Alexeev wrote:

On Sun, 19 Jan 2014 14:09:44 +0100, Sönke Ludwig wrote:

If nobody else can answer this quickly in the mean time, I'll have a
look at the source later to see what goes wrong.

OK, thank you for your reply. I will wait.

Sorry that this is taking so long, I still didn't manage to install a MySQL server for testing.

A blind guess is that TEXT columns are stored internally as a ubyte[] instead of as string. In this case using cast(string)rs[0][2].get!(ubyte[]) instead of rs[0][2].toString() should do the trick.

Sönke is correct. TEXT is stored using 'ubyte[]', and you can fix the
issue like he says:

cast(string)rs[0][2].get!(ubyte[])

[1] There's some internal reason it's ubyte[] instead of string, but I
don't recall ATM. IIRC, I think it's something to do with how MySQL
considers TEXT to merely be a slight variation on BLOB. FWIW, VARCHAR is
stored as string instead of ubyte[].