Home > database >  GBase C API application example
GBase C API application example

Time:11-26

1. Using GBase C API to create a connection
GBASE * GBASE=NULL;
/* initialization GBASE structure */
if(! (gbase=gbase_init (0)))
{
Fprintf (stderr, "cannot be initialized GBASE structure! \n");
exit(1);
}
Database connection *//*
if(! Gbase_real_connect (gbase, host, user, passwd, db, port, NULL, 0))
{
Fprintf (stderr, "\ n \ n % s", gbase_error (gbase));
exit(1);
}
/* release database connection handle */
Gbase_close (gbase);
2. Using GBase C API executing SQL statements
Database connection *//*
if(! Gbase_real_connect (gbase, host, user, passwd, db, port,
NULL, 0))
{
Fprintf (stderr, "\ n \ n % s", gbase_error (gbase));
exit(1);
}
Gbase - & gt; Reconnect=1;
/* create table in the database */
If (gbase_query (gbase, sql_drop_table))
{
Fprintf (stderr, "% s", gbase_error (gbase));
exit(1);
}
If (gbase_query (gbase, sql_create_table))
{
Fprintf (stderr, "\ n \ n % s", gbase_error (gbase));
exit(1);
}
/* inserts data to a database table */
for(i=0; i{
Id [I]=I;
Sprintf (name [I], "name_ % d", I);
}
for(i=0; i{
[I] sprintf (sql_insert insert_mode, id, name [I]);
If (gbase_query (gbase, sql_insert))
{
Fprintf (stderr, "\ n \ n % s", gbase_error (gbase));
exit(1);
}
}
/* selected data from a database table, and displays a */
If (gbase_query (gbase, sql_select))
{
Fprintf (stderr, "\ n \ n % s", gbase_error (gbase));
exit(1);
}
Res=gbase_store_result (gbase);
Printf (" the content of the table after the insert \ n ");
If (res)
{
Num_fields=gbase_num_fields (res);
While (fields=gbase_fetch_field (res))
{
Printf (" \ t | % s ", fields - & gt; Name);
}
Printf (" \ t | \ n ");
While ((row=gbase_fetch_row (res))!=NULL)
{
for(i=0; i {
Printf (" \ t | % s ", the row [I]);
}
Printf (" \ t | \ n ");
}
}
Update the data in the database table/* */
Sprintf (sql_update update_mode, id [0]).
If (gbase_query (gbase, sql_update))
{
Fprintf (stderr, "\ n \ n % s", gbase_error (gbase));
exit(1);
}
Printf (" the content of the table after the update operation \ n ");
Print_table (gbase, "g_demo1");
/* */delete data in a database table
Sprintf (sql_delete delete_mode, id [1]).
If (gbase_query (gbase, sql_delete))
{
Fprintf (stderr, "\ n \ n % s", gbase_error (gbase));
exit(1);
}
Printf (" delete the contents of the table after \ n ");
Print_table (gbase, "g_demo1");
/* release result set */
Gbase_free_result (res);
/* release database connection handle */
Gbase_close (gbase);
}
  • Related