I have written a class that manages all the database connection information/methods I need to connect to a database and query off of it. In order to make my application dynamic so that it can read from multiple tables I setup 4 variables that I will populate at another time. These variables hold the data for database I want to connect to.
Currently when I run the code, I end up with this as the output. -
test
-
�y�������
-
DB connection failed: Bad character '�' in TCP/IP address
-
The data the bad characters is holding is the name of the database(test). The correct output is the first line from above is set/output on lines 14 and 16.
Any help with this is greatly appreciated.
Here is my code so far
DbConnect.h -
#ifndef DBCONNECT_H_
-
#define DBCONNECT_H_
-
#include <string>
-
#include <mysql++.h>
-
-
class DbConnect
-
{
-
public:
-
std::string keep_value; // private data value
-
-
const char* datasource;
-
const char* hostname;
-
const char* username;
-
const char* password;
-
-
void db_set(std::string db_value); // public method to set value
-
std::string db_get(void); // public method to get value
-
mysqlpp::StoreQueryResult db_query(std::string sqlQuery); // query connection
-
-
};
-
-
#endif /*DBCONNECT_H_*/
-
test2.cpp -
#include "test2.h"
-
#include "DbConnect.h"
-
#include <mysql++.h>
-
#include <string>
-
#include <iomanip>
-
-
-
// Main application method
-
-
int DataParse::mainParse(void){
-
-
DbConnect qryAction, sqlQuery, db;
-
-
db.datasource = "test";
-
-
std::cout << db.datasource << std::endl;
-
-
-
-
sqlQuery.db_set("SELECT * from test");
-
mysqlpp::StoreQueryResult qryResult = qryAction.db_query(sqlQuery.db_get());
-
-
mysqlpp::StoreQueryResult::const_iterator it;
-
for (it = qryResult.begin(); it != qryResult.end(); ++it) {
-
mysqlpp::Row row = *it;
-
std::cout << '\t' << row[0] << std::endl;
-
std::cout << '\t' << row[1] << std::endl;
-
}
-
-
}
-
DbConnect.cpp -
#include "DbConnect.h"
-
#include <mysql++.h>
-
#include <string>
-
#include <iomanip>
-
#include <iostream>
-
-
-
// Setter for db connection variables
-
void DbConnect::db_set(std::string db_value){
-
keep_value = db_value;
-
}
-
-
// Getter for db connection variables
-
std::string DbConnect::db_get(void){
-
return (keep_value);
-
}
-
-
-
// Query server for data, returns query object
-
mysqlpp::StoreQueryResult DbConnect::db_query(std::string sqlQuery){
-
-
// connect to database server
-
mysqlpp::Connection conn(false);
-
-
std::cout << DbConnect::datasource << std::endl;
-
-
if (conn.connect(datasource, hostname, username, password)) {
-
// Retrieve a subset of the sample stock table set up by resetdb
-
// and display it.
-
mysqlpp::Query query = conn.query(sqlQuery);
-
if (mysqlpp::StoreQueryResult result = query.store()) {
-
mysqlpp::StoreQueryResult::const_iterator it;
-
return result;
-
}
-
else {
-
std::cerr << "Failed to get item list: " << query.error() << std::endl;
-
}
-
}
-
else {
-
std::cerr << "DB connection failed: " << conn.error() << std::endl;
-
}
-
}
-
1 2816 Banfa 9,065
Recognized Expert Moderator Expert
You are not setting hostname (or username or password for that matter) anywhere in this code.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Phillip Farber |
last post by:
Hello,
I'm posting here with a somewhat technical question in the hope
of finding someone with experience coding C++ against the SP_API
in OpenSP 1.5.
I have an app that uses the SP_API to parse XML and record file
offsets for elements and attribute values. It works fine with
ISO-8859-1 encoded data. However, in UTF-8 encoded XML data, it
corrupts element and attribute names composed of characters that
|
by: Job Lot |
last post by:
I have an enumeration as follows
Public Enum Delimiters
Tab
Semicolon
Comma
Space
End Enum
How can I return character equivalent of the elements in the enumeration?
|
by: James Aguilar |
last post by:
Hey all,
I'm working on an encoding scheme where I am running into a problem with
reading a file off a stream. Looking at the binary encoding of the file
(using a simple hex editor), there is no problem, and the whole file is
there. However, when I try to read it from cin, at certain times, cin stops
reading. I cannot force cin to go around the bad character, nor, indeed, do
I know what the bad character is.
I am including code at...
|
by: Sharon |
last post by:
hi,
I want to compare character, if the string contains character "-" then it
will print Hello on the screen
however, neither method (1) nor method (2) work in the code below:
so what the correct code should be? thanks!
#include <stdio.h>
#include <stdlib.h>
int main()
|
by: Toto |
last post by:
Hello,
my problem is quite simple to explain.
I have the following string:
"table+camera"
and I want to remove the + sign:
"tablecamera".
How do i do that ?
| |
by: Stefan Krah |
last post by:
Hello,
I am currently writing code where it is convenient to convert
char to int . The conversion function relies on
a character set with contiguous alphabets.
int set_mesg(Key *key, char *s)
{
char *x;
|
by: Maileen |
last post by:
Hi,
I would like to know how can i do if i want to display the copyright
character (the C into a circle) into my About window ?
thanks a lot,
Maileen
|
by: Old Wolf |
last post by:
I have two strings of corresponding characters, for example:
s1 = "abc"
s2 = "XYZ"
Is there an algorithm so that whenever a character from S1 occurs
in a string, it is replaced by the corresponding character from S2?
eg. running "crabs" through this function will producs "ZrXYs".
|
by: bowlderyu |
last post by:
Hello, all.
If a struct contains a character strings, there are two methods to
define the struct, one by character array, another by character pointer.
E.g,
//Program for struct includeing character strings, test1.c
#include <stdio.h>
#define LEN 20
struct info {
|
by: nyc680 |
last post by:
for this c program, i'd like to remove the substring "cc" and character "=" from input string "aaccbbccdd" the code below can only remove "cc" once, can somebody help me figure out how to remove other "cc" in the string also how to remove multiple character '=' ?
#include <stdio.h>
#include <string.h>
char *Mystrcpy1(char *s, const char *cs);
void func1(char *src, char *sub);
int main(void)
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |