Put numbers in std::string

T

TBass

Hi,

I've been switching a C program over to C++, and using std::string for
all the string members of the class.

One question I'm having trouble finding documentation on is how to put
numeric varaible data into a string. For example, in the C program I
did the following:

sprintf( sql, "SELECT * FROM DEVICES WHERE RKEY=\'%d\'", device_id );

But, in C++, I'm trying to do something similar to the following:

std::string sql;
int device_id;

sql = "SELECT * FROM DEVICES WHERE RKEY = ' ".... ?? HOW DO I INSERT
THE VALUE OF device_id?

I'm trying to do things the proper "C++" way.

Thanks in advance,
T
 
N

Nick Keighley

I've been switching a C program over to C++, and using std::string for
all the string members of the class.

One question I'm having trouble finding documentation on is how to put
numeric varaible data into a string. For example, in the C program I
did the following:

sprintf( sql, "SELECT * FROM DEVICES WHERE RKEY=\'%d\'", device_id );

But, in C++, I'm trying to do something similar to the following:

std::string sql;
int device_id;

sql = "SELECT * FROM DEVICES WHERE RKEY = ' ".... ?? HOW DO I INSERT
THE VALUE OF device_id?

I'm trying to do things the proper "C++" way.

#include <sstream>

std::eek:stringstream sql;
sql << "SELECT * FROM DEVICES WHERE RKEY=\'" << device_id << "\'";
do_sql (sql.str().c_str()); // assuming do_sql() takes a char*


that's from memory so check the documentation
 
T

Tim Love

One question I'm having trouble finding documentation on is how to put
numeric varaible data into a string.

Read about stringstream. Once you can do I/O to and from files, I/O to and from strings is very similar.
 
T

TBass

#include <sstream>

std::eek:stringstream sql;
sql << "SELECT * FROM DEVICES WHERE RKEY=\'" << device_id << "\'";
do_sql (sql.str().c_str()); // assuming do_sql() takes a char*

that's from memory so check the documentation

Thanks, guys. That's exactly what I need.

Much appreciated,
T
 
B

Boris

Hi,

I've been switching a C program over to C++, and using std::string for
all the string members of the class.

One question I'm having trouble finding documentation on is how to put
numeric varaible data into a string. For example, in the C program I
did the following:

sprintf( sql, "SELECT * FROM DEVICES WHERE RKEY=\'%d\'", device_id );

But, in C++, I'm trying to do something similar to the following:

std::string sql;
int device_id;

sql = "SELECT * FROM DEVICES WHERE RKEY = ' ".... ?? HOW DO I INSERT
THE VALUE OF device_id?

I'm trying to do things the proper "C++" way.

If you are using Boost C++ Libraries already Boost.Format might be an
option: http://www.boost.org/libs/format/index.html

Your code would look like this then:

std::string sql = boost::str(boost::format("SELECT * FROM DEVICES WHERE
RKEY=\'%1%\'") % device_id);

Boris
 
C

Christian Hackl

Boris said:
If you are using Boost C++ Libraries already Boost.Format might be an
option: http://www.boost.org/libs/format/index.html

There's also boost::lexical_cast:

try
{
string sql = "SELECT * FROM DEVICES WHERE RKEY = '" +
lexical_cast<string>(device_id) + "'";
}
catch (bad_lexical_cast &)
{
// error converting device_id to string
}


(By the way, this isn't related to C++, but you may check if your DBMS
allows you to use prepared statements instead of constructing query
strings manually like this.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top