473,406 Members | 2,867 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,406 software developers and data experts.

Bad Character from const char

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 2795
Banfa
9,065 Expert Mod 8TB
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
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...
2
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
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...
8
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...
18
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
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...
3
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
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...
19
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
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...

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.