0

I am trying to write a C++ script, on a remote Ubuntu machine, that would connect to a database in order to retrieve some data. I created my database on Microsoft SQL Server on an other remote machine.

I have installed SQLAPI++ and read the SQL server guide and the SQL Server (ODBC) Guide to try to connect without any success. I think the problem might be in the connect function but not sure why and I haven't found any examples to learn what I've been doing wrong.

The first time I saw the connect function being used was in this way:

con.Connect ("test",    // database name
             "tester",  // user name
             "tester",  // password
             SA_SQLServer_Client); //SQL Server Client

But I need to somehow give the IP adress of the machine where my database is stored. So I found the following few lines on (https://www.sqlapi.com/ApiDoc/mssql_odbc/) which allows me to pass on the IP of my DB in the sDBString.

void Connect(
    const SAString &sDBString, 
    const SAString &sUserID, 
    const SAString &sPassword, 
    SAClient_t eSAClient = SA_Client_NotSpecified);

Here is the code I have been using:

#include<stdio.h>
#include<SQLAPI.h>
int main(int argc, char* argv[])
{
    // create connection object to connect to database
    SAConnection con;
    try
    {
        // connect to database
        // in this example, it is Oracle,
        // but can also be Sybase, Informix, DB2
        // SQLServer, InterBase, SQLBase and ODBC

        con.Connect(
                    const SAString &"[<16.0.0.1>][<SDB>][;<TrustServerCertificate=yes>]",
                    const SAString &"sa", 
                    const SAString &"****", 
                    SAClient_t eSAClient = SA_SQLServer_Client);
        printf("We are connected!\n");
  
        // Disconnect is optional
        // autodisconnect will occur in destructor if needed
        con.Disconnect();
        printf("We are disconnected!\n");
    }
  
    catch(SAException & x)
    {
        // SAConnection::Rollback()
        // can also throw an exception
        // (if a network error for example),
        // we will be ready
        try
        {
            // on error rollback changes
            con.Rollback ();
        }
        catch(SAException &)
        {
        }
        // print error message
        printf("%s\n", (const char*)x.ErrText());
    }
}

When trying to compile I get these errors:

ConnectToSQLServer.cpp:16:21: error: expected primary-expression before ‘const’
   16 |                     const SAString &"[<16.0.0.1>][<SDB>][;<TrustServerCertificate>]",
      |                     ^~~~~
ConnectToSQLServer.cpp:17:21: error: expected primary-expression before ‘const’
   17 |                     const SAString &"sa",
      |                     ^~~~~
ConnectToSQLServer.cpp:18:21: error: expected primary-expression before ‘const’
   18 |                     const SAString &"SyAd@CoTe1",
      |                     ^~~~~
ConnectToSQLServer.cpp:19:32: error: expected primary-expression before ‘eSAClient’
   19 |                     SAClient_t eSAClient = SA_SQLServer_Client);
      |                                ^~~~~~~~~

Thank you for your time.

Update :

The error was solved and now I got a new one !

undefined reference to `SAConnection::SAConnection()'

This has to do with linking libraries in c++ which looks very complicated and I don't really understand what it means. To get to use the SQLAPI.h I included it with its path (#include "/path/SQLAPI.h") like I would do an import in python since I was to afraid to get into linking libraries.

I would really appreciate some explanation on why what I did was wrong or how to solve this issue.

Thank you for your time.

  • 2
    Are you new to C++? – kiner_shah Apr 03 '23 at 09:59
  • 1
    you don't send types stuff into a c++ function call. just send the actual strings... con.Connect( &"[<16.0.0.1>][][;]", &"sa", &"****", SA_SQLServer_Client); – siggemannen Apr 03 '23 at 09:59
  • 1
    @kiner_shah yes I'm new to c++, I've been using python and trying to switch to c++. – Emeric Villette Apr 03 '23 at 11:02
  • @EmericVillette, since you want to send it to some specific IP, maybe you need to check docs again and see if the current format will work. If it works, great but if it doesn't then you will have to do some more research. – kiner_shah Apr 03 '23 at 11:05

1 Answers1

0

Your Connect function should be something like:

con.Connect("[<16.0.0.1>][<SDB>][;<TrustServerCertificate=yes>]","sa", "****", SA_SQLServer_Client);
Alex P
  • 48
  • 1
  • 1
  • 7