LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-14-2004, 12:48 PM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
Converting types


I am using the mysqlcppapi to connect to my MySQL database in which I have a string of text.

I can get the text and print it. Here's some of the code:
Code:
mysqlcppapi::Query query = con.create_Query();
query << "SELECT welcome FROM serverData";
mysqlcppapi::Result_Store result = query.store();

mysqlcppapi::Result_Store::iterator i = result.begin();
mysqlcppapi::Row row = *i;

printf("The text: %s\n", row[0]);
At this point I have the text in row[0] but I want to have it in a variable of a "normal" type, like a pointer to char for example.

I have tried using strncpy to copy the text into a char and a char* but none is working. I always get the error:
Code:
 error: conversion from `const mysqlcppapi::ColData' to `char' is ambiguous
followed by a lot of errors from the document ColData.h that I don't understand and don't see as relevant, but here they are anyway:
Code:
/usr/local/include/mysqlcppapi-2.0/mysqlcppapi/ColData.h:113: error: candidates 
   are: mysqlcppapi::ColData_Generic<T_Str>::operator const char*() const [with 
   T_Str = std::string] <near match>
/usr/local/include/mysqlcppapi-2.0/mysqlcppapi/ColData.h:118: error:            
        mysqlcppapi::ColData_Generic<T_Str>::operator signed char() const [with 
   T_Str = std::string]
/usr/local/include/mysqlcppapi-2.0/mysqlcppapi/ColData.h:123: error:            
        mysqlcppapi::ColData_Generic<T_Str>::operator unsigned char() const 
   [with T_Str = std::string]
/usr/local/include/mysqlcppapi-2.0/mysqlcppapi/ColData.h:128: error:            
        mysqlcppapi::ColData_Generic<T_Str>::operator int() const [with T_Str = 
   std::string]
/usr/local/include/mysqlcppapi-2.0/mysqlcppapi/ColData.h:133: error:            
        mysqlcppapi::ColData_Generic<T_Str>::operator unsigned int() const 
   [with T_Str = std::string]
/usr/local/include/mysqlcppapi-2.0/mysqlcppapi/ColData.h:138: error:            
        mysqlcppapi::ColData_Generic<T_Str>::operator short int() const [with 
   T_Str = std::string]
/usr/local/include/mysqlcppapi-2.0/mysqlcppapi/ColData.h:143: error:            
        mysqlcppapi::ColData_Generic<T_Str>::operator short unsigned int() 
   const [with T_Str = std::string]
/usr/local/include/mysqlcppapi-2.0/mysqlcppapi/ColData.h:148: error:            
        mysqlcppapi::ColData_Generic<T_Str>::operator long int() const [with 
   T_Str = std::string]
/usr/local/include/mysqlcppapi-2.0/mysqlcppapi/ColData.h:153: error:            
        mysqlcppapi::ColData_Generic<T_Str>::operator long unsigned int() const 
   [with T_Str = std::string]
/usr/local/include/mysqlcppapi-2.0/mysqlcppapi/ColData.h:159: error:            
        mysqlcppapi::ColData_Generic<T_Str>::operator long long int() const 
   [with T_Str = std::string]
/usr/local/include/mysqlcppapi-2.0/mysqlcppapi/ColData.h:164: error:            
        mysqlcppapi::ColData_Generic<T_Str>::operator long long unsigned int() 
   const [with T_Str = std::string]
/usr/local/include/mysqlcppapi-2.0/mysqlcppapi/ColData.h:170: error:            
        mysqlcppapi::ColData_Generic<T_Str>::operator float() const [with T_Str 
   = std::string]
/usr/local/include/mysqlcppapi-2.0/mysqlcppapi/ColData.h:175: error:            
        mysqlcppapi::ColData_Generic<T_Str>::operator double() const [with 
   T_Str = std::string]
Any ideas how to convert the "ColData" into string/char or something? :)
 
Old 12-14-2004, 01:27 PM   #2
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
There doesn't seem to be much documentation for this API but ...

try:
std::cout << row[0].get_string();

The error messsage are from ColData try to cast itself into a char* of some description but not quite finding the right operator.
Try explicitly casting it.

HTH
 
Old 12-14-2004, 01:44 PM   #3
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
Hm.. The problem still remains. The goal is to return a pointer-to-string that contains the text that are in row[0].

I know there are *too* little documentation of this and the former MySQL++ has kinda wierd documentation to. And since the fork to mysqlcppapi I can't understand how this works since they have not documentated what is changed and so on.

So I could not find out what get_string() was for.
 
Old 12-14-2004, 01:49 PM   #4
stanleyg76
LQ Newbie
 
Registered: Dec 2004
Posts: 16

Rep: Reputation: 0
try the following:

char buf[20];
strncpy(buf, (const char *)row[0], 20);
 
Old 12-14-2004, 02:09 PM   #5
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
Thanks, but I still have some problems.

Here is the thing. I have another file that uses this function to get the message from the database. Like this:

Code:
main.cc:
  char *message;
  function(message);
  send(socket, message, strlen(message), 0);

mysql.cc:
  void function(char *message) {
     ...
     strncpy(message, (const char *)row[0], 512);
  }
I can also make the function to return the message instead if taking it as an argument. The message is max 512 characters and it has to be returned to main.cc in some type that send() can use.
 
Old 12-14-2004, 02:26 PM   #6
stanleyg76
LQ Newbie
 
Registered: Dec 2004
Posts: 16

Rep: Reputation: 0
main.cc:

char message[512];
function(&message[0], sizeof(message));
send(socket, message, strlen(message), 0);

mysql.cc:

void function(char **message, int maxlen) {
...
strncpy(*message, (const char *)row[0], maxlen);
}
 
Old 12-14-2004, 02:36 PM   #7
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
Thanks,

I have not understood yet what the double asterix ** is but now I got the error that it could not convert from char* to char** at the call to function() in main, first argument.

I tried to force a conversation by adding (char**) in front of the argument:
function((char**)&message[0], sizeof(message));

And then the program could be compiled. But now when I ran it it started to listen on a port and when a connection was made (this is when the program should send() the message to the client) then the server died with the informing error: "Killed".

Still got some ideas?
 
Old 12-14-2004, 03:23 PM   #8
stanleyg76
LQ Newbie
 
Registered: Dec 2004
Posts: 16

Rep: Reputation: 0
sorry, should be
function(&message, ...)
not
function(&message[0], ...)

basically, since you are going to be filling a pointer when
doing strncpy(), you need to pass a pointer to that pointer,
hence &message. Otherwise, you pass a copy of the pointer,
which is not what you want.
 
Old 12-14-2004, 03:44 PM   #9
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
Ok, I am getting what you mean with the pointer to a pointer. :P

But I still got an error. I tried two things this time to. First what you said &message which gave error:
Code:
src/connections.cc: In function `void* new_connection(void*)':
src/connections.cc:39: error: cannot convert `char (*)[512]' to `char**' for 
   argument `1' to `void mySQLwelcome(char**, int)'
And then I tried again to force conversation with (char**) but that ended in crashing the server when it tried to execute the code.

Last edited by Ephracis; 12-14-2004 at 03:45 PM.
 
Old 12-14-2004, 04:08 PM   #10
stanleyg76
LQ Newbie
 
Registered: Dec 2004
Posts: 16

Rep: Reputation: 0
ok, sorry about that... from now on i'll test the code before
posting it online. here is a (tested) working version of the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *row[1];

void function(char **message, int len)
{
strncpy(*message, (const char *)row[0], len);
}

int send(int sock, char *msg, int sz, int flag)
{
printf("sent: %s\n", msg);
return 0;
}

#define SZ 512
int main()
{
char *message = malloc(SZ);
int socket = 0;

row[0] = "hello world";
function((char **)&message, SZ);
send(socket, message, strlen(message), 0);
return 0;
}
 
Old 12-14-2004, 04:15 PM   #11
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 50
Oh! Thanks a lot! It finally works. The only thing was that I had to force conversation since malloc() returns a pointer-to-void.

Thanks, man. Saved me there.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Types jhbumby Linux - Newbie 4 10-19-2005 06:47 AM
session types (9.1) jdblick SUSE / openSUSE 2 01-28-2005 09:38 PM
Converting filesystem types cyris Linux - General 2 04-11-2003 01:37 PM
file types jclark00001 Linux - Newbie 2 02-17-2003 03:07 PM
Converting integer types... JStew Programming 3 12-03-2002 08:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration