File: ex_sql_query.c

package info (click to toggle)
db5.3 5.3.28%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 158,500 kB
  • sloc: ansic: 448,411; java: 111,824; tcl: 80,544; sh: 44,264; cs: 33,697; cpp: 21,604; perl: 14,557; xml: 10,799; makefile: 4,077; javascript: 1,998; yacc: 1,003; awk: 965; sql: 801; erlang: 342; python: 216; php: 24; asm: 14
file content (94 lines) | stat: -rw-r--r-- 2,096 bytes parent folder | download | duplicates (8)
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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1997, 2013 Oracle and/or its affiliates.  All rights reserved.
 *
 */

#include "ex_sql_utils.h"

/*
 * This example demonstrates how to execute queries.
 * Example 1. Single Select.
 * Example 2. Using 'WHERE' and 'ORDER BY'.
 * Example 3. Using 'GROUP BY'.
 * Example 4. Subquery.
 * Example 5. SQL function.
 */

/* Example body. */
static int
ex_sql_query(db)
	db_handle *db;
{
	const char* sql;

	/*
	 * Example 1: Single Select.
	 * -  Select rank and name from table university.
	 */
	echo_info("1. Single Select");
	sql = "\tSELECT rank, name from university;";
	exec_sql(db, sql);

	/*
	 * Example 2: Use 'WHERE' and 'ORDER BY' to arrange results
	 * - Select rank, name and country from table university where 
	 *   region is Europe. ORDER by country.
	 */
	echo_info("2. Using 'WHERE' and 'ORDER BY'");
	sql = "\tSELECT rank, country, name \n"
	      "\tFROM university \n"
	      "\tWHERE region = 'Europe' \n"
	      "\tORDER BY country;";
	exec_sql(db, sql);

	/*
	 * Example 3: Using 'Group by'.
	 */
	echo_info("3. Group by");
	sql = "\tSELECT region, count(*) from university \n"
	      "\tGROUP BY region \n"	
	      "\tORDER BY region";
	exec_sql(db, sql);

	/*
	 * Example 4 Subquery
	 * - Select rank, name and country from table university where 
	 *   country's fullname is 'USA'.
	 */
	echo_info("4. Subquery");
	sql = "\tSELECT rank, country, name from university \n"
	      "\tWHERE country = (SELECT abbr from country \n"
 				"\t\t\tWHERE country = 'USA');";
	exec_sql(db, sql);

	/*
	 * Example 5: SQL functions
	 * - Output current system date.
	 */
	echo_info("5. SQL functions: Echo current date");
	sql = "\tSELECT date('now');";
	exec_sql(db, sql);

	return 0;
}

int
main()
{
	db_handle *db;

	/* Setup environment and preload data. */
	db = setup("./ex_sql_query.db");
	load_table_from_file(db, university_sample_data, 1/* Silent */);
	load_table_from_file(db, country_sample_data, 1/* Silent */);

	/* Run example. */
	ex_sql_query(db);

	/* End. */
	cleanup(db);
	return 0;
}