473,609 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Bad Character from const char

46 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. test
  2. �y�������
  3. DB connection failed: Bad character '�' in TCP/IP address
  4.  
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
Expand|Select|Wrap|Line Numbers
  1. #ifndef DBCONNECT_H_
  2. #define DBCONNECT_H_
  3. #include <string>
  4. #include <mysql++.h>
  5.  
  6. class DbConnect
  7. {
  8. public:
  9.     std::string keep_value;                // private data value
  10.  
  11.     const char* datasource;
  12.     const char* hostname;
  13.     const char* username;
  14.     const char* password;
  15.  
  16.     void db_set(std::string db_value);    // public method to set value
  17.     std::string db_get(void);            // public method to get value
  18.     mysqlpp::StoreQueryResult db_query(std::string sqlQuery); // query connection
  19.  
  20. };
  21.  
  22. #endif /*DBCONNECT_H_*/
  23.  
test2.cpp
Expand|Select|Wrap|Line Numbers
  1. #include "test2.h"
  2. #include "DbConnect.h"
  3. #include <mysql++.h>
  4. #include <string>
  5. #include <iomanip>
  6.  
  7.  
  8. // Main application method
  9.  
  10. int DataParse::mainParse(void){
  11.  
  12.     DbConnect qryAction, sqlQuery, db;
  13.  
  14.     db.datasource = "test";
  15.  
  16.     std::cout << db.datasource << std::endl;
  17.  
  18.  
  19.  
  20.     sqlQuery.db_set("SELECT * from test");
  21.     mysqlpp::StoreQueryResult qryResult = qryAction.db_query(sqlQuery.db_get());
  22.  
  23.     mysqlpp::StoreQueryResult::const_iterator it;
  24.     for (it = qryResult.begin(); it != qryResult.end(); ++it) {
  25.         mysqlpp::Row row = *it;
  26.         std::cout << '\t' << row[0] << std::endl;
  27.         std::cout << '\t' << row[1] << std::endl;
  28.     }
  29.  
  30. }
  31.  
DbConnect.cpp
Expand|Select|Wrap|Line Numbers
  1. #include "DbConnect.h"
  2. #include <mysql++.h>
  3. #include <string>
  4. #include <iomanip>
  5. #include <iostream>
  6.  
  7.  
  8. // Setter for db connection variables
  9. void DbConnect::db_set(std::string db_value){
  10.     keep_value = db_value;
  11. }
  12.  
  13. // Getter for db connection variables
  14. std::string DbConnect::db_get(void){
  15.     return (keep_value);
  16. }
  17.  
  18.  
  19. // Query server for data, returns query object
  20. mysqlpp::StoreQueryResult DbConnect::db_query(std::string sqlQuery){
  21.  
  22.     // connect to database server
  23.     mysqlpp::Connection conn(false);
  24.  
  25.     std::cout << DbConnect::datasource << std::endl;
  26.  
  27.     if (conn.connect(datasource, hostname, username, password)) {
  28.         // Retrieve a subset of the sample stock table set up by resetdb
  29.         // and display it.
  30.         mysqlpp::Query query = conn.query(sqlQuery);
  31.         if (mysqlpp::StoreQueryResult result = query.store()) {
  32.             mysqlpp::StoreQueryResult::const_iterator it;
  33.             return result;
  34.         }
  35.         else {
  36.             std::cerr << "Failed to get item list: " << query.error() << std::endl;
  37.         }
  38.     }
  39.     else {
  40.         std::cerr << "DB connection failed: " << conn.error() << std::endl;
  41.     }    
  42. }
  43.  
Dec 4 '08 #1
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.
Dec 5 '08 #2

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

Similar topics

0
1987
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
2
2118
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?
4
8035
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...
8
62646
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()
18
1691
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 ?
5
6647
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;
3
2603
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
3
3110
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".
19
2035
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 {
1
4091
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)
0
8067
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,...
0
8567
Oralloy
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...
0
8398
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 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...
0
6993
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, 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...
1
6053
isladogs
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...
0
4015
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...
0
4076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2529
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
1
1658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.