Vijay Samuel's Blog

Hello,World with libdrizzle

Posted on: August 31, 2012

Here is a piece of code that illustrates how to create a Drizzle object, a connection object, establish a connection, execute a query, buffer its result and do some basic operations on it. The code is pretty straight forward. DRIZZLE_RETURN_OK indicates the successful execution of a function. Make sure that you have libdrizzle installed in your machine.

#include <libdrizzle/libdrizzle.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
drizzle_st *drizzle;
drizzle_con_st *con;

if ((drizzle= drizzle_create(NULL)) == NULL)
{
printf(“Error creating drizzle object\n”);
return -1;
}
con= drizzle_con_add_tcp(drizzle, NULL, “localhost”, 4427, “root”, “password”, “db”, DRIZZLE_NON_BLOCKING);

drizzle_return_t ret= drizzle_con_connect(con);
if (ret == DRIZZLE_RETURN_OK)
{
printf(“Connection Established\n”);
}

drizzle_result_st *result;
int count= 0;

result= drizzle_query(con, result, “SHOW DATABASES”, strlen(“SHOW DATABASES”), &ret);
ret= drizzle_result_buffer(result);
printf(“%d\n”, (int)drizzle_result_row_count(result));
drizzle_con_free(con);
drizzle_free(drizzle);
return 0;
}

To compile this program, one needs to link to libdrizzle. Use the following command to compile the code file. (Assuming that the code file is connect.c and that you installed libdrizzle-1.0 headers at /usr/include)
gcc -o connect connect.c -ldrizzle -I /usr/include/libdrizzle-1.0 -L /usr/lib

Hope this helps. Happy hacking folks!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

My Timeline

August 2012
M T W T F S S
« Apr   Sep »
 12345
6789101112
13141516171819
20212223242526
2728293031  

I, Me and Myself

My Blog Stats

  • 6,358 hits

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 4 other followers

%d bloggers like this: