Thread: FREE TDS with C

Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    9
    Rep Power
    0

    FREE TDS with C


    Can someone tell me if they know what I need to do to compile a C program with the FREE TDS libraries?

    I have looked for hours and cannot find a compile command to make it happen.

    Here is the web site for FREE TDS http://freetds.org

    Or even if someone can tell me where to get more documentation relating to C programming with FreeTDS.

    Thanks in advance.

    Mike
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    You need to tell the compiler to link to libsybdb. I've successfully connected from Linux, FreeBSD and OpenBSD to a MSSQL server running on Windoze, using freetds and C. Try passing a -lsybdb argument to the compiler.
    gcc -I/usr/local/include -lsybdb <rest of arguments> file.c

    Don't forget that if you installed freetds within its own directory, you'll need to pass -I and -L arguments to specify the directory that it is installed in.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    9
    Rep Power
    0
    Thanks for the info.. One more thing, do you know where I can get some info on how the function calls work for freetds? I am familiar with msql and mysql programming, but there seems not be a lot of documentation on freetds.

    Or maybe can you send me a source from something your already made so I can get a quick look on the calls?

    Thanks again!

    Mike
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    I learned how to program for freetds, using the MSDN site! I used the dblib interface (not CTlib)
    http://msdn.microsoft.com/library/de...dc04d_8mjy.asp

    You need to call dbinit() first, then dbopen() and then you're rolling :). See the MSDN site for examples. I wrote a small API of my own (one for C and another for C++). Let me know if you want it.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    9
    Rep Power
    0
    If you could send me or put up the C source so I can check it out. I am making some headway, but an example would help so I can see how the rows are retrieved.
    Thanks again!
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    Here you go --- The code is purely C and I commented what I'm doing at each stage, so you should have no trouble figuring it out. BTW you should also check the MSDN documentation for each function (dbopen(), dbcmd(), dbfcmd(), dbsqlexec() etc.) to get some more notes.
    Code:
    #include <unistd.h>
    #include <sys/param.h>
    #include <sybfront.h>
    #include <sybdb.h>
    #include <syberror.h>
    #include <stdio.h>
    
    #define  UID       "yourlogin"
    #define  PWD       "yourpassword"
    #define  PROGNAME  "DemoProg"
    #define  DBSERVER  "192.168.1.10"
    #define  DBNAME    "DatabaseName"
    
    int main(void) {
      LOGINREC *login;
      DBPROCESS *dbconn;
      char hostname[MAXHOSTNAMELEN];
      int max_len = MAXHOSTNAMELEN;
      DBINT customer_id;
      DBCHAR company_name[255];
      DBFLT8 avg_income;
    
      /* Init the DB library */
      if (dbinit() == FAIL) {
        fprintf(stderr, "Could not init db.\n");
        return 1;
      }
    
      /* Allocate a login params structure */
      if ((login = dblogin()) == FAIL) {
        fprintf(stderr, "Could not initialize dblogin() structure.\n");
        return 2;
      }
    
      /* Initialize the login params in the structure */
      DBSETLUSER(login, UID);
      DBSETLPWD(login, PWD);
      DBSETLAPP(login, PROGNAME);
      if (gethostname(hostname, max_len) == 0)
        DBSETLHOST(login, hostname);
    
      /* Now connect to the DB Server */
      if ((dbconn = dbopen(login, DBSERVER)) == NULL) {
        fprintf(stderr, "Could not connect to DB Server: %s\n", DBSERVER);
        return 3;
      }
    
      /* Now switch to the correct database */
      if ((dbuse(dbconn, DBNAME)) == FAIL) {
        fprintf(stderr, "Could not switch to database %s on DB Server %s\n", DBNAME, DBSERVER);
        return 4;
      }
    
      /* You can free the login structure now, as it is no longer needed after logging in */
      dbloginfree(login);
    
      /* Now prepare a SQL statement */
      dbcmd(dbconn, "SELECT id, company_name, avg_income FROM Customer WITH (NOLOCK)");
      /* Can use dbfcmd instead, if you want to format the SQL
         E.g. 
         dbfcmd(dbconn, "SELECT id FROM Customer WHERE company_name='%s' AND avg_income > %f", 
                company_name, avg_income);
      */
    
      /* Now execute the SQL statement */
      if (dbsqlexec(dbconn) == FAIL) {
        fprintf(stderr, "Could not execute the sql statement\n");
        return 5;
      }
      dbresults(dbconn); /* Very important to call this! :) */
    
      /* Now bind the returned columns to the variables */
      dbbind(dbconn, 1, INTBIND, 0, (BYTE *)&customer_id);
      dbbind(dbconn, 2, NTBSTRINGBIND, 0, (BYTE *)&company_name);
      dbbind(dbconn, 3, FLT8BIND, 0, (BYTE*)&avg_income);
    
      /* Loop thru the result set */
      while (dbnextrow(dbconn) != NO_MORE_ROWS) {
        /* print out the data */
        printf("%d %s %f\n", customer_id, company_name, avg_income);
      }
    
      /* Clean up*/
      dbfreebuf(dbconn);
      dbclose(dbconn);
      dbexit();
      
      return 0;
    }
    I actually have most of these API functions wrapped around other functions, so that my actual code is cleaner. Will release that code soon, once I finish putting a BSD license on it. Till then, this should get you started. Hope this helps :)
    Last edited by Scorpions4ever; July 7th, 2003 at 05:02 PM.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    9
    Rep Power
    0
    Thank you for your help! Much appreciated!

    Mike
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    9
    Rep Power
    0
    One more thing. I have been searching the MSDN site for a way to specify a port for the connect and I can not find anything. Do you know of a way?

    Thansk again,
    Mike
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    9
    Rep Power
    0
    Please disreguard last post about port. Figured it out. Just appended the post number to the host ip.

    Thanks.

    Mike
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Help Solicited.


    hello all,I have just started with Freetds and need help from u ppl 2 get started.I would like to know how to integrate my app(C code) running on linux platform and using MSSQL DB with Freetds.I would appreciate if one could provide a sample code snippet allowing me to connect to the DB, to execute a query and to implement a procedure.
    thank you.

    Comments on this post

    • RAJ_55555 disagrees : Don't raise deadbodies. Start your own thread
  20. #11
  21. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    Originally Posted by satan111
    hello all,I have just started with Freetds and need help from u ppl 2 get started.I would like to know how to integrate my app(C code) running on linux platform and using MSSQL DB with Freetds.I would appreciate if one could provide a sample code snippet allowing me to connect to the DB, to execute a query and to implement a procedure.
    thank you.
    Dude, if you're dumb enough to not scroll up and read all the posts in the thread, you might as well look for another line of work.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0
    Originally Posted by Scorpions4ever
    Dude, if you're dumb enough to not scroll up and read all the posts in the thread, you might as well look for another line of work.
    guess u r right...how could i miss that!!..newaz thnks a lot for enlightening me...it sure was a gr8 help..
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0
    I'm sorry to bring up such an old topic, but I have tried compiling the given example in a freshly installed Ubuntu 12.04 LTS on VMware with freshly compiled and installed FreeTDS_dev.0.92.571. FreeTDS has been tested with tsql against my MSSQL server so I know the FreeTDS installation is working.

    I think the compiler can find all the includes (it does not complain about files not found) but still something goes wrong.

    I use these parameters for gcc:
    gcc -lsybdb testsql.c -o testsql

    This is the output:

    /tmp/ccN1kt7k.o: In function `main':
    testsql.c:(.text+0x25): undefined reference to `dbinit'
    testsql.c:(.text+0x5c): undefined reference to `dblogin'
    testsql.c:(.text+0xb4): undefined reference to `dbsetlname'
    testsql.c:(.text+0xcd): undefined reference to `dbsetlname'
    testsql.c:(.text+0xe6): undefined reference to `dbsetlname'
    testsql.c:(.text+0x11d): undefined reference to `dbsetlname'
    testsql.c:(.text+0x136): undefined reference to `tdsdbopen'
    testsql.c:(.text+0x186): undefined reference to `dbuse'
    testsql.c:(.text+0x1c6): undefined reference to `dbloginfree'
    testsql.c:(.text+0x1da): undefined reference to `dbcmd'
    testsql.c:(.text+0x1e9): undefined reference to `dbsqlexec'
    testsql.c:(.text+0x22a): undefined reference to `dbresults'
    testsql.c:(.text+0x252): undefined reference to `dbbind'
    testsql.c:(.text+0x27a): undefined reference to `dbbind'
    testsql.c:(.text+0x2a2): undefined reference to `dbbind'
    testsql.c:(.text+0x2dc): undefined reference to `dbnextrow'
    testsql.c:(.text+0x2f0): undefined reference to `dbfreebuf'
    testsql.c:(.text+0x2ff): undefined reference to `dbclose'
    testsql.c:(.text+0x304): undefined reference to `dbexit'
    collect2: ld returned 1 exit status

    The example was pasted from a Chrome browser in Windows 7 to gedit in the virtual machine (Ubuntu) and then saved as "testsql.c".

    Please be advised, I'm still pretty new to Linux and compiling programs, so please use verbose mode for explanations :-)
  26. #14
  27. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,894
    Rep Power
    481
    Problem might be solved by changing the link order. Try the command like this:

    gcc -o testsql testsql.c -lsybdb
    [code]Code tags[/code] are essential for python code and Makefiles!
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Problem might be solved by changing the link order. Try the command like this:

    gcc -o testsql testsql.c -lsybdb
    That just worked! Thanks alot! And thanks for a quick reply!

    Seems like I might need to learn more about gcc and how compiling and linking works :)
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo