Re: Advanced Topic - Doing it differently.

Christopher Bunting <[email protected]>
Newsgroups gmane.games.devel.mud.rom
Message-ID <[email protected]>
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
[email protected]
Unsubscribe here ->>> http://www.rom.org/cgi-bin/mailman/listinfo/rom
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.