473,396 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C++, Ubuntu, Mysql, query to variable

Hi,

I last wrote software 13years ago in the pascal, dbase, clipper days.
I have recently built a Ubuntu Box and with C++ (and help from the web) setup a serial barcode reader program using Mysql database tables.

All works well, and I can do what I need except I am having some trouble with applying an MySQL query result into a defined variable. This would seem like such an easy thing to do.
The only examples I can find out there are about passing variable parameters into MySQL queries, not the reverse.

The following code (subset) using "cout" works perfectly, but displays results to the screen.
I want to to read the query as 3 variables (either text, integer or whatever), store them to be used(compared) as parameters later in the program.

It is important to note that this table has only 1 record, that is used for config info. Other tables in the database contain the database transactions.
Also that this code works (displays) the info to the screen, no trouble.

The question is limited to how to/commands pass the "cout" data to variables instead of screen output.

Any help is appreciated,


regards,

Rod.


// open the mysql database
std::string db, server, user, pass;
mysqlpp::Connection conn(false);
db = "dairy";
server = "localhost";
user = "root";
pass = "rod";
string res = "";
string port = "";

if (conn.connect(db.c_str(), server.c_str(), user.c_str(), pass.c_str())) //connect and get comms parameters
{
fputs("Connected To SQL Database\n\n",output);
mysqlpp::Query query = conn.query();
query << "SELECT * FROM control";
mysqlpp::Result res = query.store();

if (res)
{
char buf(100);
mysqlpp::Row row;
mysqlpp::Row::size_type i;
for (i = 0; row =res.at(i); ++i)
{
cout << "Printer Port :" << row["Pport"] << "\r\n";
cout << "Alarm Byte :" << row["Alarmbyte"] << "\r\n";
cout << "Feeder Byte :" << row["Feederbyte"]<< "\r\n";
fputs("Configuration Data Good\n\n",output);
}
}

else
fputs("Error Getting Configuration Data\n\n",output);
}
}
Sep 20 '08 #1
1 4817
weaknessforcats
9,208 Expert Mod 8TB
This code:
char buf(100);
defines one char with a value of 100. Did you mean: char buf[100]?

You say it works with cout? Then I would use a stringstream intead:
Expand|Select|Wrap|Line Numbers
  1. cout << "Hello" << 123;
  2. string stream ss;
  3. ss << "Hello" << 123;
  4.  
Now you can use the operator>> on ss to get you your variables:
Expand|Select|Wrap|Line Numbers
  1. string data;
  2. int value;
  3. ss >> data >> value;
  4.  
Sep 20 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Lenz Grimmer | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, MySQL 4.0.14, a new version of the popular Open Source/Free Software Database, has been released. It is now available in source and binary...
0
by: I.P. | last post by:
Hi, it's my story. I have two 4.0.14 mysql server on one machine with win XP Professional polish version. First acts as master: on port 3300 Second acts as slave: on port 3301 below my...
0
by: I.P. | last post by:
No one has replied to my post. ----- Original Message ----- From: "I.P." <jancio_wodnik@wp.pl> To: <mysql@lists.mysql.com> Sent: Monday, August 18, 2003 1:01 PM Subject: mysql 4.0.14 +...
2
by: Alan Hewat | last post by:
I have a relatively modest mySQL database with a PHP interface (70,000 entries). On a standard Dell Dimension 4400 (1.9 GHz with 512 Mbytes) running Suse 8.1 a typical query takes 0.66 sec with no...
1
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer...
1
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work....
2
by: Flic | last post by:
Hi, I have a basic db that I access with MySQL query browser. Everything seems fine to me but I am using this db as part of a php shopping basket and when I try to add an item I get: Notice:...
2
by: Boujii | last post by:
Greetings, I have been attempting to make a drop down menu of countries. From this menu I wish to create a variable in order to INPUT into mysql database. I have no trouble making the drop down menu,...
1
by: sasimca007 | last post by:
Hi friends, I installed mysql in ubuntu but, i don't know how to connect to that mysql. Actually we connect to postgresql by psql command, but when i tried to connect mysql with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.