List:MySQL++« Previous MessageNext Message »
From:Warren Young Date:January 25 2006 5:50am
Subject:Re: query.execute not working
View as plain text  
Chuck Haines wrote:
> 
> query = conn.query();
>     ttime = time( NULL );
>     sprintf( dTime, "%d000", ttime );
>     sql = "insert into Survey values ('";
>     sql += dTime;
>     sql += "', '";
>     sql += dTime;
>     sql += "', 'Information', 'Information')";
>     query << sql;

*Dude*....  Please read a book that covers C++ idioms.  This looks like 
it's trying to be Perl or BASIC.  I'm not trying to pick on you.  It's 
just that if you write C++ as though it were some other language, it 
will not work very well.  Rewriting it into proper C++:

     query = conn.query();
     snprintf( dTime, sizeof(dTime), "%d000", time(0) );
     query << "insert into Survey values (" << mysqlpp::quote <<
          dTime << ", " << mysqlpp::quote << dTime <<
          ", 'Information', 'Information')";
     query.execute();

Notice that I changed your sprintf() to snprintf().  sprintf() should 
not be used any more; it is a prime candidate for buffer overflow 
errors.  snprintf() is new in C99, and most compilers offer something 
like it, if not exactly like it.  (Microsoft prefers _snprintf(), for 
instance.)  My rewrite assumes that dTime is an array, and not a pointer 
to dynamically-allocated memory.

Also, I think you could profit from looking into MySQL++'s SSQLS 
feature.  It reduces the drudgery in the code above.

>     cout << "Error: " << query.error() << endl;

Unless I'm missing something, this won't ever give you anything useful. 
  Any true errors will be signaled with exceptions, totally bypassing 
this code.
Thread
query.execute not workingChuck Haines24 Jan
  • Re: query.execute not workingWarren Young25 Jan
    • Re: query.execute not workingChuck Haines25 Jan