dbTalk Databases Forums  

Exception of type std::length_error

mailing.database.mysql-plusplus mailing.database.mysql-plusplus


Discuss Exception of type std::length_error in the mailing.database.mysql-plusplus forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Mathieu Pagé
 
Posts: n/a

Default Exception of type std::length_error - 11-12-2006 , 06:39 PM






Hi everyone,

I hope this has not been asked before, I searched the online archives of
the list and could not find any references to my problem.

I'm trying to run a simple test program based on the simple1 example. I
can get connected to the DB, I create a query and execute it. q.size()
return 4 wich is the number of row I expected so the query is fine. Then
I try to enumerate the result of the query like this :

<code>
mysqlpp::Row row;
mysqlpp::Row::size_type i;
for (i = 0; row = res.at(i); ++i)
cout <<row.at(0) <<endl;
</code>

however I get an length_error exception on "row = res.at(i)".

Since I'm new to MySQL++, maybe i'm doing something stupid and someone
can point me to it.

Thanks in advance.

Mathieu Pagé








Complete code :
#include <mysql++.h>

#include <iostream>
#include <exception>

using namespace std;

int main(int argc, char* argv[])
{
try
{
// On se connecte à la DB
mysqlpp::Connection con(true);
con.connect("chess", "localhost", "root", "SomePassword");

if (!con)
{
cout <<"erreur" <<endl
<<con.error() <<endl;

return 1;
}

// On créer une requête
mysqlpp::Query q = con.query();
q <<"SELECT NomJoueur FROM tblJoueurs";

// On exécute la requête
mysqlpp::Result res = q.store();

cout <<"Il y as : " <<res.size() <<" lignes." <<endl;

// On affiche la liste des joueurs
if (res && res.size() > 0)
{
mysqlpp::Row row;
mysqlpp::Row::size_type i;
for (i = 0; row = res.at(i); ++i)
{
cout <<row.at(0) <<endl;
}
}
else
{
cout <<"Erreur dans la requête : " <<q.error() <<endl;
return 1;
}


}
catch (mysqlpp::ConnectionFailed e)
{
cout <<e.what() <<endl;
}

return 0;
}

--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw


Reply With Quote
  #2  
Old   
Warren Young
 
Posts: n/a

Default Re: Exception of type std::length_error - 11-13-2006 , 05:26 PM






Mathieu Pagé wrote:
Quote:
code
mysqlpp::Row row;
mysqlpp::Row::size_type i;
for (i = 0; row = res.at(i); ++i)
cout <<row.at(0) <<endl;
/code

however I get an length_error exception on "row = res.at(i)".
I think you're expecting Row::at() to return false (or, rather, an
object that can be converted to false) when you walk off the end of the
result set. MySQL++ doesn't do that sort of thing, by design. It
signals errors with exceptions, just as you saw.

That is the fall-back behavior when you turn off exceptions, however.
Read the user manual for details.

You might also look at examples/simple3.cpp. It operates in a way very
similar to what you're doing, without needing special treatment. It
does that by using a different type of query than you are using.

--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw



Reply With Quote
  #3  
Old   
Mathieu Pagé
 
Posts: n/a

Default Re: Exception of type std::length_error - 11-13-2006 , 07:31 PM



Warren Young a écrit :
Quote:
Mathieu Pagé wrote:

code
mysqlpp::Row row;
mysqlpp::Row::size_type i;
for (i = 0; row = res.at(i); ++i)
cout <<row.at(0) <<endl;
/code

however I get an length_error exception on "row = res.at(i)".

I think you're expecting Row::at() to return false (or, rather, an
object that can be converted to false) when you walk off the end of
the result set. MySQL++ doesn't do that sort of thing, by design. It
signals errors with exceptions, just as you saw.

That is the fall-back behavior when you turn off exceptions, however.
Read the user manual for details.

You might also look at examples/simple3.cpp. It operates in a way
very similar to what you're doing, without needing special treatment.
It does that by using a different type of query than you are using.

Hi M. Young

Thanks you for your answer.

I looked at simple.cpp and tried to do the same thing in my code (using
"res.fetch_row()") and I got the same error. Actually I just realised,
looking at the stack trace when the error occurs, that the exception is
trown during the execution of the operator= function that copy the row.

I also tried to execute example/resetdb tonight and it raise an
exception too. Maybe there is a problem with my compiled version of MySQL++.

--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw



Reply With Quote
  #4  
Old   
Mathieu Pagé
 
Posts: n/a

Default Re: Exception of type std::length_error - 11-19-2006 , 04:23 PM



Warren Young a écrit :
Quote:
Mathieu Pagé wrote:

I looked at simple.cpp and tried to do the same thing in my code
(using "res.fetch_row()") and I got the same error.

Yes, I realized after I sent it that the simple*.cpp examples all turn
off exceptions. I didn't post about that because I wasn't sure it
mattered, and didn't have time to test it. Sorry for the hassle.

Hi,

Thanks anyway for trying to help. It was appreciated.

It turns out I was not using the right mysql++.lib files. I was using a
release one to link into a debug executable. That's what caused the
exception, probably because the object structures were differents
between a realease and debug build.

--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw



Reply With Quote
  #5  
Old   
Warren Young
 
Posts: n/a

Default Re: Exception of type std::length_error - 11-20-2006 , 01:34 PM



Mathieu Pagé wrote:
Quote:
It turns out I was not using the right mysql++.lib files. I was using a
release one to link into a debug executable.
Yep. That'll cause all kinds of havoc.

--
MySQL++ Mailing List
For list archives: http://lists.mysql.com/plusplus
To unsubscribe: http://lists.mysql.com/plusplus?unsu...ie.nctu.edu.tw



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.