1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Full example: GNOME Data Access 5 manual</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="GNOME Data Access 5 manual">
<link rel="up" href="getting_started.html" title="Code examples">
<link rel="prev" href="managing-errors.html" title="Managing connection's events and errors">
<link rel="next" href="ddl_example.html" title="DDL example">
<meta name="generator" content="GTK-Doc V1.32 (XML mode)">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="5"><tr valign="middle">
<td width="100%" align="left" class="shortcuts"></td>
<td><a accesskey="h" href="index.html"><img src="home.png" width="16" height="16" border="0" alt="Home"></a></td>
<td><a accesskey="u" href="getting_started.html"><img src="up.png" width="16" height="16" border="0" alt="Up"></a></td>
<td><a accesskey="p" href="managing-errors.html"><img src="left.png" width="16" height="16" border="0" alt="Prev"></a></td>
<td><a accesskey="n" href="ddl_example.html"><img src="right.png" width="16" height="16" border="0" alt="Next"></a></td>
</tr></table>
<div class="sect1">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="main_example"></a>Full example</h2></div></div></div>
<p>
The following example is a complete program which creates a <code class="filename">example_db.db</code>
SQLite database in the current
directory, creates a "products" table,add a few rows to it, and then displays its contents. The example uses
an SQLite database as it does not require any server setup and can thus be run as-is.
That example can be found in the <code class="filename">samples/SimpleExample/example.c</code>.
</p>
<p>
To compile it, run:
</p>
<pre class="programlisting">
cc -o example example.c `pkg-config --cflags --libs libgda-3.0`
</pre>
<p>
</p>
<p>
The code is:
</p>
<pre class="programlisting">
#include <libgda/libgda.h>
#include <sql-parser/gda-sql-parser.h>
GdaConnection *open_connection (void);
void display_products_contents (GdaConnection *cnc);
void create_table (GdaConnection *cnc);
void insert_data (GdaConnection *cnc);
void update_data (GdaConnection *cnc);
void delete_data (GdaConnection *cnc);
void run_sql_non_select (GdaConnection *cnc, const gchar *sql);
int
main (int argc, char *argv[])
{
gda_init ();
GdaConnection *cnc;
/* open connections */
cnc = open_connection ();
create_table (cnc);
insert_data (cnc);
display_products_contents (cnc);
update_data (cnc);
display_products_contents (cnc);
delete_data (cnc);
display_products_contents (cnc);
gda_connection_close (cnc);
return 0;
}
/*
* Open a connection to the example.db file
*/
GdaConnection *
open_connection ()
{
GdaConnection *cnc;
GError *error = NULL;
GdaSqlParser *parser;
/* open connection */
cnc = gda_connection_open_from_string ("SQLite", "DB_DIR=.;DB_NAME=example_db", NULL,
GDA_CONNECTION_OPTIONS_NONE,
&error);
if (!cnc) {
g_print ("Could not open connection to SQLite database in example_db.db file: %s\n",
error && error->message ? error->message : "No detail");
exit (1);
}
/* create an SQL parser */
parser = gda_connection_create_parser (cnc);
if (!parser) /* @cnc does not provide its own parser => use default one */
parser = gda_sql_parser_new ();
/* attach the parser object to the connection */
g_object_set_data_full (G_OBJECT (cnc), "parser", parser, g_object_unref);
return cnc;
}
/*
* Create a "products" table
*/
void
create_table (GdaConnection *cnc)
{
run_sql_non_select (cnc, "DROP table IF EXISTS products");
run_sql_non_select (cnc, "CREATE table products (ref string not null primary key, "
"name string not null, price real)");
}
/*
* Insert some data
*
* Even though it is possible to use SQL text which includes the values to insert into the
* table, it's better to use variables (place holders), or as is done here, convenience functions
* to avoid SQL injection problems.
*/
void
insert_data (GdaConnection *cnc)
{
typedef struct {
gchar *ref;
gchar *name;
gboolean price_is_null;
gfloat price;
} RowData;
RowData data [] = {
{"p1", "chair", FALSE, 2.0},
{"p2", "table", FALSE, 5.0},
{"p3", "glass", FALSE, 1.1},
{"p1000", "???", TRUE, 0.},
{"p1001", "???", TRUE, 0.},
};
gint i;
gboolean res;
GError *error = NULL;
GValue *v1, *v2, *v3;
for (i = 0; i < sizeof (data) / sizeof (RowData); i++) {
v1 = gda_value_new_from_string (data[i].ref, G_TYPE_STRING);
v2 = gda_value_new_from_string (data[i].name, G_TYPE_STRING);
if (data[i].price_is_null)
v3 = NULL;
else {
v3 = gda_value_new (G_TYPE_FLOAT);
g_value_set_float (v3, data[i].price);
}
res = gda_connection_insert_row_into_table (cnc, "products", &error, "ref", v1, "name", v2, "price", v3, NULL);
if (!res) {
g_error ("Could not INSERT data into the 'products' table: %s\n",
error && error->message ? error->message : "No detail");
}
gda_value_free (v1);
gda_value_free (v2);
if (v3)
gda_value_free (v3);
}
}
/*
* Update some data
*/
void
update_data (GdaConnection *cnc)
{
gboolean res;
GError *error = NULL;
GValue *v1, *v2, *v3;
/* update data where ref is 'p1000' */
v1 = gda_value_new_from_string ("p1000", G_TYPE_STRING);
v2 = gda_value_new_from_string ("flowers", G_TYPE_STRING);
v3 = gda_value_new (G_TYPE_FLOAT);
g_value_set_float (v3, 1.99);
res = gda_connection_update_row_in_table (cnc, "products", "ref", v1, &error, "name", v2, "price", v3, NULL);
if (!res) {
g_error ("Could not UPDATE data in the 'products' table: %s\n",
error && error->message ? error->message : "No detail");
}
gda_value_free (v1);
gda_value_free (v2);
gda_value_free (v3);
}
/*
* Delete some data
*/
void
delete_data (GdaConnection *cnc)
{
gboolean res;
GError *error = NULL;
GValue *v;
/* delete data where name is 'table' */
v = gda_value_new_from_string ("table", G_TYPE_STRING);
res = gda_connection_delete_row_from_table (cnc, "products", "name", v, &error);
if (!res) {
g_error ("Could not DELETE data from the 'products' table: %s\n",
error && error->message ? error->message : "No detail");
}
gda_value_free (v);
/* delete data where price is NULL */
res = gda_connection_delete_row_from_table (cnc, "products", "price", NULL, &error);
if (!res) {
g_error ("Could not DELETE data from the 'products' table: %s\n",
error && error->message ? error->message : "No detail");
}
}
/*
* display the contents of the 'products' table
*/
void
display_products_contents (GdaConnection *cnc)
{
GdaDataModel *data_model;
GdaSqlParser *parser;
GdaStatement *stmt;
gchar *sql = "SELECT ref, name, price FROM products";
GError *error = NULL;
parser = g_object_get_data (G_OBJECT (cnc), "parser");
stmt = gda_sql_parser_parse_string (parser, sql, NULL, NULL);
data_model = gda_connection_statement_execute_select (cnc, stmt, NULL, &error);
g_object_unref (stmt);
if (!data_model)
g_error ("Could not get the contents of the 'products' table: %s\n",
error && error->message ? error->message : "No detail");
gda_data_model_dump (data_model, stdout);
g_object_unref (data_model);
}
/*
* run a non SELECT command and stops if an error occurs
*/
void
run_sql_non_select (GdaConnection *cnc, const gchar *sql)
{
GdaStatement *stmt;
GError *error = NULL;
gint nrows;
const gchar *remain;
GdaSqlParser *parser;
parser = g_object_get_data (G_OBJECT (cnc), "parser");
stmt = gda_sql_parser_parse_string (parser, sql, &remain, &error);
if (remain)
g_print ("REMAINS: %s\n", remain);
nrows = gda_connection_statement_execute_non_select (cnc, stmt, NULL, NULL, &error);
if (nrows == -1)
g_error ("NON SELECT error: %s\n", error && error->message ? error->message : "no detail");
g_object_unref (stmt);
}
</pre>
<p>
and executing should output something like:
</p>
<pre class="programlisting">> ./example
ref | name | price
------+-------+---------
p1 | chair | 2.000000
p2 | table | 5.000000
p3 | glass | 1.100000
p1000 | ??? | NULL
p1001 | ??? | NULL
(5 rows)
ref | name | price
------+---------+---------
p1 | chair | 2.000000
p2 | table | 5.000000
p3 | glass | 1.100000
p1000 | flowers | 1.990000
p1001 | ??? | NULL
(5 rows)
ref | name | price
------+---------+---------
p1 | chair | 2.000000
p3 | glass | 1.100000
p1000 | flowers | 1.990000
(3 rows)
</pre>
<p>
</p>
</div>
<div class="footer">
<hr>Generated by GTK-Doc V1.32</div>
</body>
</html>
|