Practices
  Home arrow Practices arrow Page 4 - Database Independent Development in C
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PRACTICES

Database Independent Development in C
By: Clay Dowling
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 10
    2004-09-27


    Table of Contents:
  • Database Independent Development in C
  • Making the Connection
  • Getting Results
  • An Example
  • Extending libdbi

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Database Independent Development in C - An Example
    ( Page 4 of 5 )

    This is a very simple utility that I created to query the user list for an application I've been working on. Most applications will be a little more complex.

    #include <stdio.h> #include <dbi/dbi.h>

    int main() {

     dbi_conn db;  dbi_result result;

     unsigned long user_id;  const char* name;  const char* email;  int drivercount;

     drivercount = dbi_initialize("/usr/local/lib/dbd");  printf("Loaded %d drivers\n", drivercount);  db = dbi_conn_new("mysql");

     dbi_conn_set_option(db, "host", "localhost");  dbi_conn_set_option(db, "username", "user");  dbi_conn_set_option(db, "password", "secret");  dbi_conn_set_option(db, "dbname", "bigapp");

     dbi_conn_connect(db);  result = dbi_conn_query(db, "SELECT user_id, name, email FROM author");    while (dbi_result_next_row(result)) {   user_id = dbi_result_get_ulong(result, "user_id");   name = dbi_result_get_string(result, "name");   email = dbi_result_get_string(result, "email");   printf("%2ld) %s <%s>\n", user_id, name, email);  }

     dbi_result_free(result);  dbi_conn_close(db);  dbi_shutdown();

     return 0;

    }

    On my installation, providing that I called this userlist.c, my command line would look like this:

    cc userlist.c -o userlist -I/usr/local/include -L/usr/local/lib -ldbi

    As you can see, this program is blissfully short. It's so short in fact that it makes C a very good candidate for handling some of the routine daily reporting work that every IT department has to suffer through. Generate some HTML output instead of the plain text that I showed and you start to put out very nice looking nightly reports. In later articles I'll also discuss the use of libraries for generating PDF output. With that under your belt you'll have a reporting tool that can rival what commercial solutions can provide.



     
     
    >>> More Practices Articles          >>> More By Clay Dowling
     

       

    PRACTICES ARTICLES

    - More Techniques for Finding Things
    - Finding Things
    - Finishing the System`s Outlines
    - The System in So Many Words
    - Basic Data Types and Calculations
    - What`s the Address? Pointers
    - Design with ArgoUML
    - Pragmatic Guidelines: Diagrams That Work
    - Five-Step UML: OOAD for Short Attention Span...
    - Five-Step UML: OOAD for Short Attention Span...
    - Introducing UML: Object-Oriented Analysis an...
    - Class and Object Diagrams
    - Class Relationships
    - Classes
    - Basic Ideas





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT