Trubble in executing the cpp program...


 
Thread Tools Search this Thread
Top Forums Programming Trubble in executing the cpp program...
# 1  
Old 10-20-2008
MySQL Can't get the output...

I wrote a code like this.......

#include <iostream>
#include <stdio.h>
#include <mysql.h>
#include <string.h>
#include <stdlib.h>

using namespace std;

#include "Connection.h"


int main()
{
char *Host = (char *)"localhost";
char *Database =(char *)"sachin";
char *UserId =(char *)"root";
char *Password =NULL;
Result *result;
char *query;
Connection DbConnection(Host, Database, UserId, Password);
query="select * from the test"; // test is my database
result = DbConnection.Query(query); // perform the required database operation..


return 0;
}

Connection is the class & have connect function that just took the 4 arguments & establishes the connection to the mysql server ...Also Query is the function defined in connection class.

I m getting output as -
$g++ -c -I/usr/include/mysql Main.cpp
$ g++ -o Main Main.o -L/usr/lib/mysql -lmysqlclient
$./Main
terminate called after throwing an instance of 'char const*'
Aborted

I don't know why i m getting this output. I think it is related to the library.......

Can any one help me......

Last edited by ps_sach; 10-20-2008 at 05:45 AM..
# 2  
Old 10-21-2008
Either the constructor of Connection, or the Query() method throws an exception of type const char *. You can catch it, by enclosing the calls with try, and adding a catch block, like:

Code:
try {
    Connection DbConnection(Host, Database, UserId, Password);
    query="select * from the test"; // test is my database
    result = DbConnection.Query(query); // perform the required database operation..
}
catch (const char *errmsg)
{
    std::cerr << "error message from connection: " << errmsg << "\n";
    return 1;
}

# 3  
Old 10-21-2008
May also be related to the fact that Host, Database, etc. should probably be declared as (const char *).
# 4  
Old 10-21-2008
Quote:
Originally Posted by fpmurphy
May also be related to the fact that Host, Database, etc. should probably be declared as (const char *).
Well, I don't think so. Passing a "char*" to a function that expects "const char*" is perfectly legal. And if it wasn't, then the compiler would say so.

On the other hand, if the function would in fact require a "char*" it might expect the value to be writable, which is not the case for string literals. But I don't think that is the case here.

Also, the meaning of the message "terminate called after throwing an instance of 'char const*'" is pretty clear.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Error in executing the C program

Hello Friends, I have written a code for the unisex bathroom which makes a policy that when a woman is in the bathroom only other women may enter, but not men, and vice versa. This program consists of four functions which a user defines but these functions are not properly working while... (4 Replies)
Discussion started by: Ravi Tej
4 Replies

2. Programming

Error with shared lIBMpi.so.1 when compiling CPP program

Hello, Met a problem when compiling a C++ program from source code without error, but when ran it there was always an error message: ./Ray: error while loading shared libraries: libmpi_cxx.so.1: cannot open shared object file: No such file or directoryAs the error points to openmpi which was... (0 Replies)
Discussion started by: yifangt
0 Replies

3. UNIX for Dummies Questions & Answers

executing a different program

What system calls or commands do I need to use in order to execute a different program from an already running process? (1 Reply)
Discussion started by: justOne21
1 Replies

4. Shell Programming and Scripting

Executing WIN32OLE program

Hello, Please help me out to execute this perl program: #!/usr/bin/perl -w use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; $Win32::OLE::Warn = 3; # die on errors... # get already active Excel application or open new my... (6 Replies)
Discussion started by: suvenduperl
6 Replies

5. Shell Programming and Scripting

Executing a Java Program

I am entirely new to shell scripting and would like to create a script to execute a java program called Main. I've already compiled it and placed the .java and .class files at /root/javaTest. Next I made a shell script that simply contained: java /root/javaTest/Main . I made the script... (2 Replies)
Discussion started by: hypnotic_meat
2 Replies

6. Solaris

How to know the size of the program currently executing in memory

hey everybody, i am currently working on solaris 10 os on a m5000 server. my problem is when i want the exact size of a program in execution, i am unable to do it. earlier i thought the RSS field of prstat but because of its large size it cant be the size. pmap -x shows some output but it includes... (2 Replies)
Discussion started by: aryansheikh
2 Replies

7. Shell Programming and Scripting

convert cpp program to c shell script ?

Hi guys I tried to convert this c++ code to c shell script but there are some bugs and I don't know how to solve it. This code prints the three variables in decreasing order: int main() { int x,y,z; cin >> x >> y >>z; if ( x < y ) if ( x < z ) if ( y < z ) cout << x <<" " <<... (2 Replies)
Discussion started by: domain
2 Replies

8. Programming

Executing an .ec program in different informix versions

Hi all, I tried writing an .ec program connecting to informix database from solaris platform Sun 5.7 informix version that i had to used when i compiled the program was 9.21.UC3 the binary when i ported solaris box with informix version 9.40.UC5 i am unable to run that i am encountering... (0 Replies)
Discussion started by: matrixmadhan
0 Replies

9. UNIX for Dummies Questions & Answers

executing the su command from a java program.

Say in unix (AIX) m/c, I am logged in with s1 user and want to start process p1 with user credentials of s2. I can do manually in this way: #su - s2 #enter password for s2> somePassword $ p1 But all this I have to do through a java program. How to pass the password through program. One... (1 Reply)
Discussion started by: shailendrat
1 Replies

10. Programming

executing a program within a program

Read the title: how do i do it? (4 Replies)
Discussion started by: Gekko
4 Replies
Login or Register to Ask a Question