(Language C++) Using variables in mysql_query...

Discussion in 'Programming/Scripts' started by chris.zeman, Nov 16, 2006.

  1. chris.zeman

    chris.zeman New Member

    I am writing a small program in C++ that accepts data on a socket connection from SBC's. The SBC's send data in the following format: Unit Name,1,2. 1 is the unit number, and 2 is the number of cycles on that unit.

    I want to automatically create a table for each Unit Name that I receive. Here is what I have so far:
    Code:
    #include "ServerSocket.h"
    #include "SocketException.h"
    #include <string>
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <mysql/mysql.h>
    
    int main ( int argc, int argv[] )
    {
    	try
        	{
          		// Create the socket
          		ServerSocket server ( 14036 );
    		while ( true )
    		{
    			ServerSocket new_sock;
    			server.accept ( new_sock );
    
    			try
    	    		{
    	      			while ( true )
    				{
    		  			std::string data;
    		  			new_sock >> data;
    		  			new_sock << data;
    		  			using namespace std;
    		  			string trainid(data);
    
    		  			std::string unit_name, unit_num, num_cycles;
    					std::string::size_type pos = trainid.find(',');
    		  			if (pos != std::string::npos)
    		  			{
    		      				ride = trainid.substr(0,pos);
    		      				cout << unit_name << endl;
    		  			}
    
       		  			trainid = trainid.substr(pos+1);
    		  			pos = trainid.find(',');
    	   	  			if (pos != std::string::npos)
    		   			{
    		      				unit_num = trainid.substr(0,pos);
    		      				cout << unit_num << endl;
    		   			}
    
    		   			num_cycles = trainid.substr(pos+1);
    		   			cout << num_cycles << endl;
    			
    					MYSQL db_conn;
      					MYSQL_RES *result;
    					MYSQL_ROW row;
    					MYSQL_FIELD *fields;
    					unsigned int numrows;
    					unsigned int numfields;
    					int c;
    					char user[] = "user";
    					char password[] = "password";
    					char host[] = "localhost";
    					char dbname[] = "TrainID";
    					char buffer[BUFSIZ];
    
    					mysql_init(&db_conn);
    					
    					// Attempt to connect to the database.
    					if (mysql_real_connect(&db_conn, host, user, password, dbname, 0, NULL, 0) == NULL)
    					{
    						fprintf(stderr, "Could not connect to mysql server.  Error %s\n", mysql_error(&db_conn));
    					}
    		
    					// Create a table for the ride if it doesn't already exist.
    					string create_table1 = "CREATE TABLE ";
    					string create_table2 = " (Unit INT PRIMARY KEY, Cycles BIGINT);";
    					string create_table3 = create_table1 + unit_name + create_table2;
    					
    					if (!mysql_query(&db_conn, create_table3) == 0)
    					{
    						fprintf(stderr, "Could not create the table.  Error %s\n", mysql_error(&db_conn));
    					}			
    				}
    	    		}
    	  		catch ( SocketException& ) {}
    		}
    	}
    
    catch ( SocketException& e )
    	{
          		std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
        	}
    
    return 0;
    }

    I try to compile and get the following error:
    Code:
    [root@automation Devel]# make
    g++    -c -o ServerSocket.o ServerSocket.cpp
    g++    -c -o Socket.o Socket.cpp
    g++    -c -o TrainID.o TrainID.cpp
    TrainID.cpp: In function `int main(int, int*)':
    TrainID.cpp:76: error: cannot convert `std::string' to `const char*' for argument `2' to `int mysql_query(MYSQL*, const char*)'
    make: *** [TrainID.o] Error 1
    
    I understand what is happening here, but what do I need to do? I can't seem to find an answer. Am I going to run into the same thing when I try to insert values into my columns?

    Thanks! :)
    Chris
     

Share This Page