Advanced Topic - Doing it differently.

Christopher Bunting cbunting99 at gmail.com
Wed Apr 6 23:12:45 New Zealand Standard Time 2005


One last reply to this thread.. Since part of this thread had to do
with Databases, and then where I mention why I use the d programming
language, I'll show the code that is needed to connect Rom to the
SQLite database using both the C API and my D API. This is why I moved
from C to D

The C API example is first followed by the same code written in D.

// C API for SQLite

#include <stdio.h>
#include <sqlite3.h>

static int callback(void *NotUsed, int argc, char **argv, char **azColName){
  int i;
  for(i=0; i<argc; i++){
    printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
  }
  printf("\n");
  return 0;
}

int main(int argc, char **argv){
  sqlite3 *db;
  char *zErrMsg = 0;
  int rc;

  if( argc!=3 ){
    fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]);
    exit(1);
  }
  rc = sqlite3_open(argv[1], &db);
  if( rc ){
    fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    sqlite3_close(db);
    exit(1);
  }
  rc = sqlite3_exec(db, argv[2], callback, 0, &zErrMsg);
  if( rc!=SQLITE_OK ){
    fprintf(stderr, "SQL error: %s\n", zErrMsg);
  }
  sqlite3_close(db);
  return 0;
}


// Here is the same exact code to connect to SQLite from Rom using D.

import sqlite3_imp;

int main()
{
   sqlite3* db;
   int code;
   
   code = sqlite3_open("file.db", &db);
   if(SQLITE_OK != code)
   {
      printf("DB create error: %s\n", sqlite3_errmsg(db));
      return 1;
   }
   printf("DB open!\n");
   
   sqlite3_close(db);
   printf("DB closed.\n");
   
   return 0;
}

Chris
-- 
ROM mailing list
ROM at rom.org
Unsubscribe here ->>> http://www.rom.org/cgi-bin/mailman/listinfo/rom


More information about the MUD-Dev mailing list