Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Expected Primary-Expression

error c-string bind_param

  • Please log in to reply
3 replies to this topic

#1 agnl666

agnl666

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 172 posts
  • Programming Language:C, Java, C++
  • Learning:Python, Assembly

Posted 17 April 2012 - 04:06 PM

Hello,

I am currently working on a piece of code that is giving me a lot of strife though after about an hour and a half of crawling google I am still at a loss.

The troubleing piece of code is rather straight forward in concept. I am writing a data object to a mysql database using the C mysql connector API. I have a previous version of this code working using only C code though have been rewritting it in C++.

The data object is as fallows.


/*
name : dawson reid
date : April 12th, 2012
*/

#include <string>
using namespace std;

#ifndef MYOBJ_H
#define	MYOBJ_H

class MyObj
{
	public :
	
		// DATA MEMBERS

		string
			Field_1,
			Field_2,
			Field_3; 	
};

#endif	/* MYOBJ_H */


The method that is giving me trouble is


bool writeMyObjToDb
(
	MyObj obj,
	MYSQL *conn,
	MYSQL_STMT *stmt_handle
)
{
	if (conn == NULL) return false;
	else if (stmt_handle == NULL) return false;

	MYSQL_BIND bind[3]; memset(bind, 0, sizeof(bind));
	
	// bind all variables
	bind[0] = add_string_parameter((char*)obj.Field_1.c_str(), obj.Field_1.size());
	bind[1] = add_string_parameter((char*)obj.Field_2.c_str(), obj.Field_2.size());
	bind[2] = add_string_parameter((char*)obj.Field_3.c_str(), obj.Field_3.size());

	// handle execution and errors
	int mysql_return_code = mysql_stmt_bind_param(stmt_handle, bind);
	if (0 != mysql_return_code)
	{
		cout << "Error : Stmt bind failed." << endl;
		mysql_stmt_close(stmt_handle);
		return false;
	}
  
	mysql_return_code = mysql_stmt_execute(stmt_handle);
	if (0 != mysql_return_code)
	{
		cout << "Error : Stmt execute failed." << endl;
		mysql_stmt_close(stmt_handle);
		return false;
	}
	return true;
}


And the add_string_parameter method :


MYSQL_BIND add_string_parameter(char* param, long unsigned int *param_length)
{
	MYSQL_BIND temp_bind;
	memset(&temp_bind, 0, sizeof (temp_bind));
	
	temp_bind.buffer_type = MYSQL_TYPE_VAR_STRING;
	temp_bind.is_null = 0;
	temp_bind.length = param_length;
	temp_bind.buffer_length = *param_length;
	temp_bind.buffer = param;
	
	return temp_bind;
}


The MyObj class also has several methods and a constructor. It can also be assumed that MYSQL *conn and MYSQL_STMT *stmt_handle have been prepaired and initialized properly and that the data members of the MyObj object have been initialized.

Further more the error I am recieving is :

"error: expected primary-expression before ‘.’ token"

Which is occuring several times and is refering to "bind[0] = add_string_parameter((char*)obj.Field_1.c_str(), obj.Field_1.SourceIP.size());" lines.
  • 0

#2 so1i

so1i

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 297 posts

Posted 17 April 2012 - 04:29 PM

I'm relatively inexperienced at C, so I've no idea if this will be any help - but where exactly is the SourceIP being set? Have you given us all the necessary code here?
  • 0
My Company - My Homepage - My Twitter - My Google+ - My LinkedIn

"Things don’t have to change the world to be important.” - Steve Jobs

#3 agnl666

agnl666

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 172 posts
  • Programming Language:C, Java, C++
  • Learning:Python, Assembly

Posted 17 April 2012 - 04:42 PM

Sorry, I intended to remove that. All the needed code is here and it can be assumed that the data members of the MyObj object have been initialized.
  • 0

#4 agnl666

agnl666

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 172 posts
  • Programming Language:C, Java, C++
  • Learning:Python, Assembly

Posted 18 April 2012 - 11:29 AM

I solved the problem and I appolagize as I was wrong in my previous statement.

I changed the names of the objects and functions to more generic names as this pretains to an important piece of code in mine. In doing so I actually removed the error that was occuring. In my initial version I had named the MyObj variable being passed into the writeMyObjToDb method myObj. In doing so when writing the bindings I had called myObj MyObj. The compiler therefor thought I was referencing the class and not the instance.

Sorry for the mistake and the waste of forum space. I hope I didn't confuse anyone.
  • 0





Also tagged with one or more of these keywords: error, c-string, bind_param

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download