Overview
Features
Download
Documentation
Community
Add-Ons & Services

MySQL session

Please post support and help requests here.

MySQL session

Postby starthis » 06 Feb 2009, 21:37

This is probably a dumb question, but can someone give me a simple example of how to create a MySQL session?
I'm trying to use the example from "POCO Data User Guide" (SQLite) but the example throws an exception when I try to create a session.


MySQL::Connector::registerConnector();
Session ses("MySQL", "mysql:host=localhost;username=testuser;password=xxx;dbname=test;");
//MySQL::Connector c;
//Session ses = c.createSession("MySQL", "mysql:host=localhost;username=testuser;password=xxx;dbname=test;");
MySQL::Connector::unregisterConnector();


terminate called after throwing an instance of 'Poco::Data::MySQL::MySQLException'
what(): MySQL


I'm not sure if the connectionstring is supposed to be like that or not or what to put as the first parameter for the createSession, I can't find any example.
Thanks in advance.
starthis
 
Posts: 7
Joined: 04 Feb 2009, 20:49

Re: MySQL session

Postby guenter » 06 Feb 2009, 22:47

This one works for MySQL 5:

Code: Select all
Session ses("MySQL", "user=joeuser;password=secret;db=mydatabase;compress=true;auto-reconnect=true");
guenter
 
Posts: 1129
Joined: 11 Jul 2006, 16:27
Location: Austria

Re: MySQL session

Postby alex » 06 Feb 2009, 23:03

guenter wrote:
Code: Select all
Session ses("MySQL", "user=joeuser;password=secret;db=mydatabase;compress=true;auto-reconnect=true");


That will work only if you are running MySQL on the localhost. Otherwise, you have to add host and port parameters, e.g:

Code: Select all
Session ses("MySQL", "host=myhost;port=3306;user=joeuser;password=secret;db=mydatabase;compress=true;auto-reconnect=true");
alex
 
Posts: 1130
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: MySQL session

Postby starthis » 07 Feb 2009, 08:30

Thanks guys, works like a charm!
starthis
 
Posts: 7
Joined: 04 Feb 2009, 20:49

Re: MySQL session

Postby starthis » 08 Feb 2009, 12:22

While I'm trying out further examples from the Data User guide, I came accross this problem :

The following statement executes perfectly :

string id("23");
string result;
ses << "SELECT badgecode FROM badges WHERE _id = " << id, into(result), now;
cout<<result<<endl;

But this equivalent throws a StatementException :

string id("23");
string result;
ses << "SELECT badgecode FROM badges WHERE _id = :id" , into(result), use(id), now;
cout<<result<<endl;

I tried to use an int id with the same result, it seems that as long as I use the "use()" function, the statement fails.
Any ideas?
starthis
 
Posts: 7
Joined: 04 Feb 2009, 20:49

Re: MySQL session

Postby alex » 08 Feb 2009, 15:29

starthis wrote:ses << "SELECT badgecode FROM badges WHERE _id = " << id, into(result), now;


Is this code from the documentation? I am surprised it executes because there is no placeholder of any sort (either '%s' or '?') in the SQL.

starthis wrote:ses << "SELECT badgecode FROM badges WHERE _id = :id" , into(result), use(id), now;
I tried to use an int id with the same result, it seems that as long as I use the "use()" function, the statement fails.
Any ideas?


Try to use '?' instead of ':id' in your SQL. Not all db backends support the latter format for placeholders.
alex
 
Posts: 1130
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: MySQL session

Postby starthis » 08 Feb 2009, 17:49

Thanks for the hint, with '?' as placeholder it works for MySQL.

I guess http://pocoproject.org/docs/DataUserManual.html is only meant for SQLite (/ODBC) usage.

Is this code from the documentation? I am surprised it executes because there is no placeholder of any sort (either '%s' or '?') in the SQL.


I assumed it was just a string concat that was then inserted as a Statement.

From the usermanual :

std::string aName("Peter");
ses << "INSERT INTO FORENAME VALUES(" << aName << ")", now;
starthis
 
Posts: 7
Joined: 04 Feb 2009, 20:49

Re: MySQL session

Postby alex » 08 Feb 2009, 18:21

starthis wrote:I assumed it was just a string concat that was then inserted as a Statement.


Ah, yes, something like that should work (provided string is single-quoted). Somewhat more elegant way (especially when you have multiple parameters) is to format your string like this:

Code: Select all
ses << "INSERT INTO FORENAME VALUES('%s')", aName, now;
alex
 
Posts: 1130
Joined: 11 Jul 2006, 16:27
Location: United_States


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest