Re: Unable to insert String constants through my function

2003-12-17 Thread Michael Stassen


shiva shankar wrote:

Hi, 

I USE THIS FUNCTION TO INSERT VALUES TO DIFFERENT
VALUES OF MY DATABASE..
void InsertValues(const string tableName, const
string values)
{
 char ValuesToStore[255];
  long datasize;
  char *EncodedData;
  char *query;
  strcpy(ValuesToStore, values.c_str());
  EncodedData = new char[strlen(ValuesToStore)*2 + 1];
  datasize = mysql_real_escape_string(conn,
EncodedData, ValuesToStore, strlen(ValuesToStore));
  query = new char[datasize+255];
  sprintf(query, INSERT INTO %s %s VALUES %s, 
 tableName.c_str(),
(lookup[tableName.c_str()]).c_str(), EncodedData); 
  mysql_real_query(conn, query, strlen(query)+255);

  // if the insertion involves an auto-incremented
attribute, 
  //   the following line displays the value of that
attribute.
  cout  tableName   ID inserted =  
mysql_insert_id(conn)  endl;
 coutquery;
  delete [] EncodedData;
  delete [] query;
  return;
}

NOW WHEN I CALL THIS FUNCTION IN MAIN TO INSERT VALUES
LIKE,
  InsertValues(comm_protos, (AwaterKent, 56));
  
  InsertValues(sensor, (1, 1, 0, 0, 0, 3, 0));
 
  InsertValues(map_sensor_platform, (1, 1));
  

Only the first InsertValues doesnt work,  It is unable
to understand the string constant 'AwaterKent'
When I print out the query in InsertValues it reads as

INSERT INTO comm_protos (name, datarate) VALUES
(AtwaterKent, 56)Values inserted into 'comm_protos'
I changed to this and none of these works as well:

InsertValues(comm_protos, ('AtwaterKent', 56));
The query prints out as
INSERT INTO comm_protos (name, datarate) VALUES
(\'AtwaterKent\', 56)Values inserted into
'comm_protos'
When I tried this,

InsertValues(comm_protos, (\'AtwaterKent\', 56));

The query printed as:
INSERT INTO comm_protos (name, datarate) VALUES
(\'AtwaterKent\', 56)Values inserted into
'comm_protos'


PLEASE LET ME KNOW HOW I NEED TO PASS THE STRING
CONSTANT OR IF I NEED TO MAKE ANY CHANGE IN MY
INSERTVALUES FUNCTION..
THANKS A LOT,
SHIV
The query needs to look like this:

INSERT INTO comm_protos (name, datarate) VALUES ('AtwaterKent', 56)

I'd say you need to fix your InsertValues function, as it mishandles 
strings.  It should either quote them, or it should expect them to 
arrive quoted and then NOT escape the quotes.  Personally, I think the 
former is better, but then the question arises as to how to tell which 
things should be quoted.  Either way, the string needs to be escaped 
before being wrapped in quotes, so that King's Cross becomes 'King\'s 
Cross' rather than 'King's Cross' or \'King\'s Cross\'.

Michael

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]


Unable to insert String constants through my function

2003-12-16 Thread shiva shankar
Hi, 

I USE THIS FUNCTION TO INSERT VALUES TO DIFFERENT
VALUES OF MY DATABASE..


void InsertValues(const string tableName, const
string values)
{
 char ValuesToStore[255];
  long datasize;
  char *EncodedData;
  char *query;

  strcpy(ValuesToStore, values.c_str());
  EncodedData = new char[strlen(ValuesToStore)*2 + 1];
  datasize = mysql_real_escape_string(conn,
EncodedData, ValuesToStore, strlen(ValuesToStore));
  query = new char[datasize+255];
  sprintf(query, INSERT INTO %s %s VALUES %s, 
 tableName.c_str(),
(lookup[tableName.c_str()]).c_str(), EncodedData); 
  mysql_real_query(conn, query, strlen(query)+255);

  // if the insertion involves an auto-incremented
attribute, 
  //   the following line displays the value of that
attribute.
  cout  tableName   ID inserted =  
mysql_insert_id(conn)  endl;
 coutquery;
  delete [] EncodedData;
  delete [] query;
  return;
}

NOW WHEN I CALL THIS FUNCTION IN MAIN TO INSERT VALUES
LIKE,


  InsertValues(comm_protos, (AwaterKent, 56));
  
  InsertValues(sensor, (1, 1, 0, 0, 0, 3, 0));
 
  InsertValues(map_sensor_platform, (1, 1));
  

Only the first InsertValues doesnt work,  It is unable
to understand the string constant 'AwaterKent'

When I print out the query in InsertValues it reads as

INSERT INTO comm_protos (name, datarate) VALUES
(AtwaterKent, 56)Values inserted into 'comm_protos'


I changed to this and none of these works as well:

InsertValues(comm_protos, ('AtwaterKent', 56));
The query prints out as

INSERT INTO comm_protos (name, datarate) VALUES
(\'AtwaterKent\', 56)Values inserted into
'comm_protos'


When I tried this,

InsertValues(comm_protos, (\'AtwaterKent\', 56));

The query printed as:
INSERT INTO comm_protos (name, datarate) VALUES
(\'AtwaterKent\', 56)Values inserted into
'comm_protos'




PLEASE LET ME KNOW HOW I NEED TO PASS THE STRING
CONSTANT OR IF I NEED TO MAKE ANY CHANGE IN MY
INSERTVALUES FUNCTION..


THANKS A LOT,
SHIV


__
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/

-- 
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]