[futurebasic] MySQL - C functions - how to make this work in FB

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : July 2011 : Group Archive : Group : All Groups

From: Thomas Peters <thomasg_peters@...>
Date: Sun, 24 Jul 2011 09:19:57 -0400
Thanks Robert,

This worksŠ

Yes, I have mysql server installed.  I use it allot with java and php
programming. I have mysql include folder for MYSQL Connector/C 6.0.2
install included in my /usr/include/mysql.  I have also included ­Imysql
in my 'more compiler options' under the advanced.

Getting a connection to mysql and getting results in a command lineŠ

 Good start   :-)  however, I am interested in understanding more.  I
would like to be able to get the results from the query into a
multi-dimentional array.  I would like it returned from the C function
into FB array if possible.  Perhaps having row(0) as the column names of
the query.  BTW:  SQL allows you to specify alias names for real column

The array would have some meta-data such as column count, row count and
perhaps another another array of column types that represents the types of
data for each column.  The index id of the column tyes array would match
the index id of the associated column in the multi-dimentional array.

I am trying to see if I can create a few reusable function for FB users to
have access to some light weight MySQL functions.  I would like to see a
function that returns a MYSQL Connection, one that takes a structure with
table names, and column names and the connection and returns a
multi-dimentional array of values that represent the result set.

I seems to me this stuff would need FB wrappers around the C code.

Thoughts, anyone???

From: Robert Purves <listrp@...>
Date: Mon, 21 Feb 2011 19:28:42 +1300

Thomas Peters wrote:

> This is a simple snippet of C code to test a connection to MySQL
>database.> It uses the native C API from MySQL library.  I am interested
>in learning> how this can be converted into a FB Function.  I noticed the
>WebServices> example uses BeginCFunction >> EndC code block.> > <snip>

You would first have to obtain and install mysql, because it is not part
of OS X.
Then something like this should compile (but with link-time errors):

include "CommandLineTool"

#include <mysql/mysql.h>
#include <stdio.h>
#include <string.h>

int Test();

int Test() 
MYSQL mysql;
char query[80];

sprintf(query,"SELECT src,dst FROM ipaudit");
mysql_real_query(&mysql,query,(unsigned int)strlen(query));
res = mysql_use_result(&mysql);

while(row = mysql_fetch_row(res))
printf("%s %sn",row[0],row[1]);
return 0;

toolbox fn Test = SInt32

// main 
fn Test

Lastly, for correct linking you will need to put a link option in 'More
compiler options' in FBtoC settings. This may be as simple as -lmysql.

Robert P.