Author Topic: FreeBasic to Oxygen?  (Read 5953 times)

0 Members and 1 Guest are viewing this topic.

kryton9

  • Guest
FreeBasic to Oxygen?
« on: June 21, 2011, 09:12:45 PM »
I know that you wrote Oxygen in FreeBasic and Assembly Charles. If possible that we can use .bi files with Oxygen, can you show an example of how one does it?
I tried on my own as a test but got nowhere.

I would like to try to use sqlite in tests for keeping data-- instead of in linked lists, vectors, queues etc as in STL in c++, in sqlite.
I can't get http://www.oxygenbasic.org/forum/index.php?topic=14.0 this to work on my end.

Charles Pegge

  • Guest
Re: FreeBasic to Oxygen?
« Reply #1 on: June 21, 2011, 09:54:25 PM »

It should be possible with a few tweaks. One issue is that FreeBasic BI files assume Byval instead of ByRef. FB also uses DEF files and requires LIB *.a files to be generated for each DLL used. Oxygen doe not need this and DLL library names are always given explicitly in the source code.

I don't use SQL so I need a small working example. One written in FB would be good.

Charles

kryton9

  • Guest
Re: FreeBasic to Oxygen?
« Reply #2 on: June 21, 2011, 10:15:30 PM »
Charles, how about using sqlite with the c headers and the dll?

http://www.sqlite.org/c3ref/funclist.html

http://www.sqlite.org/download.html

jcfuller

  • Guest
Re: FreeBasic to Oxygen?
« Reply #3 on: June 22, 2011, 02:32:51 AM »
I wrote this early on.. not sure if it's still viable:

http://www.oxygenbasic.org/forum/index.php?topic=14.0

James

kryton9

  • Guest
Re: FreeBasic to Oxygen?
« Reply #4 on: June 22, 2011, 02:43:13 AM »
I linked to your thread in my first post James. I just couldn't ever get it to work. The sqlite dlls I downloaded, none of them were the right ones-- at least --that is what I think was the problem.

Charles Pegge

  • Guest
Re: FreeBasic to Oxygen?
« Reply #5 on: June 22, 2011, 03:25:27 AM »

It worked with an older version of SQLite.

For efficient development we need to start with something simple but fully functional before making any transformations.

Charles

JRS

  • Guest
Re: FreeBasic to Oxygen?
« Reply #6 on: June 22, 2011, 08:53:40 AM »
Kent & Charles,

Here is a scripted API interface to SQLite3 that should be simple enough to implement in OxygenBasic.

Code: [Select]
' SQLite3 Demo Script

DECLARE SUB DLL ALIAS "_gtk" LIB "gtk-server"
DECLARE SUB VARPTR ALIAS "varptr" LIB "gtk-server"

DLL("gtk_server_require libsqlite3.so")

DLL("gtk_server_define sqlite3_open NONE INT 2 STRING LONG")
DLL("gtk_server_define sqlite3_exec NONE INT 5 LONG STRING INT NULL PTR_STRING")
DLL("gtk_server_define sqlite3_prepare_v2 NONE INT 5 LONG STRING INT PTR_LONG NULL")
DLL("gtk_server_define sqlite3_step NONE INT 1 LONG")
DLL("gtk_server_define sqlite3_column_text NONE STRING 2 LONG INT")
DLL("gtk_server_define sqlite3_close NONE INT 1 LONG")

CONST SQLITE_ROW = 100
db = 0
dberr = 0
stmt = 0

DLL("sqlite3_open \"testsql\" " & VARPTR(db))
DLL("sqlite3_exec " & db & " \"CREATE TABLE demo(someval INTEGER,  sometxt TEXT);\" 0 0 " & VARPTR(dberr))
DLL("sqlite3_exec " & db & " \"INSERT INTO demo VALUES (123, 'Hello');\" 0 0 " & VARPTR(dberr))
DLL("sqlite3_exec " & db & " \"INSERT INTO demo VALUES (234, 'cruel');\" 0 0 " & VARPTR(dberr))
DLL("sqlite3_exec " & db & " \"INSERT INTO demo VALUES (345, 'world');\" 0 0 " & VARPTR(dberr))
result = DLL("sqlite3_prepare_v2 " & db & " \"SELECT * FROM demo;\" -1 " & VARPTR(stmt) & " 0")
SPLIT result BY " " TO ok, stmt

WHILE DLL("sqlite3_step " & stmt) = SQLITE_ROW
  PRINT DLL("sqlite3_column_text " & stmt & " " & 0) & " - " & DLL("sqlite3_column_text " & stmt & " " & 1),"\n"
WEND

DLL("sqlite3_close " & db)

jrs@Laptop:~/SB/test$ scriba sqlite3.sb
123 - Hello
234 - cruel
345 - world
jrs@Laptop:~/SB/test$

Charles Pegge

  • Guest
Re: FreeBasic to Oxygen?
« Reply #7 on: June 22, 2011, 11:29:07 AM »

Many thanks John.

Code: OxygenBasic
  1.   'SQLite3 Demo
  2.  
  3.   'needs SQLite3.dll
  4.  '
  5.  'SQLITE_API int sqlite3_open(
  6.  '  const char *filename,   /* Database filename (UTF-8) */
  7.  '  sqlite3 **ppDb          /* OUT: SQLite db handle */
  8.  ');
  9.  '
  10.  '
  11.  'SQLITE_API int sqlite3_exec(
  12.  'sqlite3*,                                  /* An open database */
  13.  'const char *sql,                           /* SQL to be evaluated */
  14.  'int (*callback)(void*,int,char**,char**),  /* Callback function */
  15.  'void *,                                    /* 1st argument to callback */
  16.  'char **errmsg                              /* Error msg written here */
  17.  ');
  18.  '
  19.  '
  20.  'SQLITE_API int sqlite3_prepare_v2(
  21.  '  sqlite3 *db,            /* Database handle */
  22.  '  const char *zSql,       /* SQL statement, UTF-8 encoded */
  23.  '  int nByte,              /* Maximum length of zSql in bytes. */
  24.  '  sqlite3_stmt **ppStmt,  /* OUT: Statement handle */
  25.  '  const char **pzTail     /* OUT: Pointer to unused portion of zSql */
  26.  ');
  27.  '
  28.  '
  29.  'SQLITE_API int sqlite3_step(sqlite3_stmt*);
  30.  '
  31.  '
  32.  'SQLITE_API const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol);
  33.  '
  34.  '
  35.  'SQLITE_API int sqlite3_close(sqlite3 *);
  36.  '
  37.  '
  38.  
  39.   extern lib "sqlite3.dll"
  40.  
  41.   sys sqlite3_open (zstring*name,sys*db)
  42.   sys sqlite3_exec (sys db,zstring* s, sys p1, sys p2, sys*dberr)
  43.   sys sqlite3_prepare_v2 (sys db, zstring*s, sys p1, sys*stmt, sys p2)
  44.   sys sqlite3_step (sys n)
  45.   zstring* sqlite3_column_text (sys row, sys col)
  46.   sys sqlite3_close (sys db)
  47.   '
  48.  end extern
  49.   '
  50.  sys SQLITE_ROW = 100
  51.   sys hdb
  52.   sys dberr
  53.   sys stmt
  54.   '
  55.  zstring *errmsg
  56.   string  cr=chr(13)+chr(10)
  57.   string  pr="DataBase Listing:" cr
  58.   '
  59.  sqlite3_open "testsql",hdb
  60.   '
  61.  sqlite3_exec hdb, "CREATE TABLE demo(someval INTEGER,  sometxt TEXT);", 0, 0, dberr
  62.   sqlite3_exec hdb, "INSERT INTO demo VALUES (123, 'Hello');", 0, 0, dberr
  63.  sqlite3_exec hdb, "INSERT INTO demo VALUES (234, 'cruel');", 0, 0, dberr
  64.  sqlite3_exec hdb, "INSERT INTO demo VALUES (345, 'world');", 0, 0, dberr
  65.  '
  66.  result = sqlite3_prepare_v2 hdb, "SELECT * FROM demo;" cr, -1, stmt, 0
  67.   '
  68.  if dberr then @errmsg=dberr : print errmsg
  69.   '
  70.  while sqlite3_step(stmt) = SQLITE_ROW
  71.     pr+=sqlite3_column_text(stmt, 0) " - " sqlite3_column_text(stmt, 1) cr
  72.   wend
  73.   '
  74.  sqlite3_close hdb
  75.  
  76.   print pr
  77.  
  78.  

Charles

kryton9

  • Guest
Re: FreeBasic to Oxygen?
« Reply #8 on: June 22, 2011, 04:26:28 PM »
Thanks John for the example and Thanks Charles for the example code too. Look forward to trying it out tonight!

kryton9

  • Guest
Re: FreeBasic to Oxygen?
« Reply #9 on: June 22, 2011, 08:15:39 PM »
That's great it ran just fine.

I am confused, why didn't it need a header file and was able to run just from the dll?

Charles Pegge

  • Guest
Re: FreeBasic to Oxygen?
« Reply #10 on: June 22, 2011, 11:09:01 PM »
Hi Kent. Glad it worked for you.

This is an all-in-one demo.

This is the header part

Code: OxygenBasic
  1.  
  2.   extern lib "sqlite3.dll"  
  3.  
  4.   sys sqlite3_open (zstring*name,sys*db)  
  5.   sys sqlite3_exec (sys db,zstring* s, sys p1, sys p2, sys*dberr)  
  6.   sys sqlite3_prepare_v2 (sys db, zstring*s, sys p1, sys*stmt, sys p2)  
  7.   sys sqlite3_step (sys n)  
  8.   zstring* sqlite3_column_text (sys row, sys col)  
  9.   sys sqlite3_close (sys db)  
  10.   '  
  11.  end extern  
  12.  

Charles

JRS

  • Guest
Re: FreeBasic to Oxygen?
« Reply #11 on: June 22, 2011, 11:10:25 PM »
Quote
I am confused, why didn't it need a header file and was able to run just from the dll?

Everything needed to make this run is defined in the O2 program.

 ;) Charles and I posted the same response at the same time.

kryton9

  • Guest
Re: FreeBasic to Oxygen?
« Reply #12 on: June 22, 2011, 11:19:50 PM »
Oh, I see now. Neat, learn something new everyday. Thanks guys.