September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,114 @@
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
static const char* db_file = ":memory:"; // Create an in-memory database.
void check_error(int result_code, sqlite3 *db);
int select_callback(void* data, int column_count, char** columns, char** column_names);
int main(void) {
sqlite3 *db;
int result_code;
char *sql;
char *insert_statements[4];
sqlite3_stmt *compiled_statement;
int i;
// Open the database.
result_code = sqlite3_open(db_file, &db);
check_error(result_code, db);
// Create the players table in the database.
sql = "create table players("
"id integer primary key asc, "
"name text, "
"score real, "
"active integer, " // Store the bool value as integer (see https://sqlite.org/datatype3.html chapter 2.1).
"jerseyNum integer);";
result_code = sqlite3_exec(db, sql, NULL, NULL, NULL);
check_error(result_code, db);
// Insert some values into the players table.
insert_statements[0] = "insert into players (name, score, active, jerseyNum) "
"values ('Roethlisberger, Ben', 94.1, 1, 7);";
insert_statements[1] = "insert into players (name, score, active, jerseyNum) "
"values ('Smith, Alex', 85.3, 1, 11);";
insert_statements[2] = "insert into players (name, score, active, jerseyNum) "
"values ('Manning, Payton', 96.5, 0, 18);";
insert_statements[3] = "insert into players (name, score, active, jerseyNum) "
"values ('Doe, John', 15, 0, 99);";
for (i=0; i<4; i++) {
result_code = sqlite3_exec(db, insert_statements[i], NULL, NULL, NULL);
check_error(result_code, db);
}
// Display the contents of the players table.
printf("Before update:\n");
sql = "select * from players;";
result_code = sqlite3_exec(db, sql, select_callback, NULL, NULL);
check_error(result_code, db);
// Prepare the parametrized SQL statement to update player #99.
sql = "update players set name=?, score=?, active=? where jerseyNum=?;";
result_code = sqlite3_prepare_v2(db, sql, -1, &compiled_statement, NULL);
check_error(result_code, db);
// Bind the values to the parameters (see https://sqlite.org/c3ref/bind_blob.html).
result_code = sqlite3_bind_text(compiled_statement, 1, "Smith, Steve", -1, NULL);
check_error(result_code, db);
result_code = sqlite3_bind_double(compiled_statement, 2, 42);
check_error(result_code, db);
result_code = sqlite3_bind_int(compiled_statement, 3, 1);
check_error(result_code, db);
result_code = sqlite3_bind_int(compiled_statement, 4, 99);
check_error(result_code, db);
// Evaluate the prepared SQL statement.
result_code = sqlite3_step(compiled_statement);
if (result_code != SQLITE_DONE) {
printf("Error #%d: %s\n", result_code, sqlite3_errmsg(db));
sqlite3_close(db);
return result_code;
}
// Destroy the prepared statement object.
result_code = sqlite3_finalize(compiled_statement);
check_error(result_code, db);
// Display the contents of the players table.
printf("After update:\n");
sql = "select * from players;";
result_code = sqlite3_exec(db, sql, select_callback, NULL, NULL);
check_error(result_code, db);
// Close the database connection.
sqlite3_close(db);
return EXIT_SUCCESS;
}
/*
Checks the result code from an SQLite operation.
If it contains an error code then this function prints the error message,
closes the database and exits.
*/
void check_error(int result_code, sqlite3 *db) {
if (result_code != SQLITE_OK) {
printf("Error #%d: %s\n", result_code, sqlite3_errmsg(db));
sqlite3_close(db);
exit(result_code);
}
}
/* This callback function prints the results of the select statement. */
int select_callback(void* data, int column_count, char** columns, char** column_names) {
int i;
for (i=0; i<column_count; i++) {
printf(columns[i]);
if (i < column_count-1) printf(" | ");
}
printf("\n");
}

View file

@ -0,0 +1,124 @@
program Parametrized_SQL_Statement;
uses
sqlite3, sysutils;
const
DB_FILE : PChar = ':memory:'; // Create an in-memory database.
var
DB :Psqlite3;
ResultCode :Integer;
SQL :PChar;
InsertStatements :array [1..4] of PChar;
CompiledStatement :Psqlite3_stmt;
i :integer;
{ CheckError
Checks the result code from an SQLite operation.
If it contains an error code then this procedure prints the error message,
closes the database and halts the program. }
procedure CheckError(ResultCode: integer; DB: Psqlite3);
begin
if ResultCode <> SQLITE_OK then
begin
writeln(format('Error #%d: %s', [ResultCode, sqlite3_errmsg(db)]));
sqlite3_close(DB);
halt(ResultCode);
end;
end;
{ SelectCallback
This callback function prints the results of the select statement.}
function SelectCallback(Data: pointer; ColumnCount: longint; Columns: PPChar; ColumnNames: PPChar):longint; cdecl;
var
i :longint;
col :PPChar;
begin
col := Columns;
for i:=0 to ColumnCount-1 do
begin
write(col^); // Print the current column value.
inc(col); // Advance the pointer.
if i<>ColumnCount-1 then write(' | ');
end;
writeln;
end;
begin
// Open the database.
ResultCode := sqlite3_open(DB_FILE, @DB);
CheckError(ResultCode, DB);
// Create the players table in the database.
SQL := 'create table players(' +
'id integer primary key asc, ' +
'name text, ' +
'score real, ' +
'active integer, ' + // Store the bool value as integer (see https://sqlite.org/datatype3.html chapter 2.1).
'jerseyNum integer);';
ResultCode := sqlite3_exec(DB, SQL, nil, nil, nil);
CheckError(ResultCode, DB);
// Insert some values into the players table.
InsertStatements[1] := 'insert into players (name, score, active, jerseyNum) ' +
'values (''Roethlisberger, Ben'', 94.1, 1, 7);';
InsertStatements[2] := 'insert into players (name, score, active, jerseyNum) ' +
'values (''Smith, Alex'', 85.3, 1, 11);';
InsertStatements[3] := 'insert into players (name, score, active, jerseyNum) ' +
'values (''Manning, Payton'', 96.5, 0, 18);';
InsertStatements[4] := 'insert into players (name, score, active, jerseyNum) ' +
'values (''Doe, John'', 15, 0, 99);';
for i:=1 to 4 do
begin
ResultCode := sqlite3_exec(DB, InsertStatements[i], nil, nil, nil);
CheckError(ResultCode, DB);
end;
// Display the contents of the players table.
writeln('Before update:');
SQL := 'select * from players;';
ResultCode := sqlite3_exec(DB, SQL, @SelectCallback, nil, nil);
CheckError(ResultCode, DB);
// Prepare the parametrized SQL statement to update player #99.
SQL := 'update players set name=?, score=?, active=? where jerseyNum=?;';
ResultCode := sqlite3_prepare_v2(DB, SQL, -1, @CompiledStatement, nil);
CheckError(ResultCode, DB);
// Bind the values to the parameters (see https://sqlite.org/c3ref/bind_blob.html).
ResultCode := sqlite3_bind_text(CompiledStatement, 1, PChar('Smith, Steve'), -1, nil);
CheckError(ResultCode, DB);
ResultCode := sqlite3_bind_double(CompiledStatement, 2, 42);
CheckError(ResultCode, DB);
ResultCode := sqlite3_bind_int(CompiledStatement, 3, 1);
CheckError(ResultCode, DB);
ResultCode := sqlite3_bind_int(CompiledStatement, 4, 99);
CheckError(ResultCode, DB);
// Evaluate the prepared SQL statement.
ResultCode := sqlite3_step(CompiledStatement);
if ResultCode <> SQLITE_DONE then
begin
writeln(format('Error #%d: %s', [ResultCode, sqlite3_errmsg(db)]));
sqlite3_close(DB);
halt(ResultCode);
end;
// Destroy the prepared statement object.
ResultCode := sqlite3_finalize(CompiledStatement);
CheckError(ResultCode, DB);
// Display the contents of the players table.
writeln('After update:');
SQL := 'select * from players;';
ResultCode := sqlite3_exec(DB, SQL, @SelectCallback, nil, nil);
CheckError(ResultCode, DB);
// Close the database connection.
sqlite3_close(db);
end.

View file

@ -0,0 +1,40 @@
-- This works in Oracle's SQL*Plus command line utility
VARIABLE P_NAME VARCHAR2(20);
VARIABLE P_SCORE NUMBER;
VARIABLE P_ACTIVE VARCHAR2(5);
VARIABLE P_JERSEYNUM NUMBER;
begin
:P_NAME := 'Smith, Steve';
:P_SCORE := 42;
:P_ACTIVE := 'TRUE';
:P_JERSEYNUM := 99;
end;
/
drop table players;
create table players
(
NAME VARCHAR2(20),
SCORE NUMBER,
ACTIVE VARCHAR2(5),
JERSEYNUM NUMBER
);
insert into players values ('No name',0,'FALSE',99);
commit;
select * from players;
UPDATE players
SET name = :P_NAME, score = :P_SCORE, active = :P_ACTIVE
WHERE jerseyNum = :P_JERSEYNUM;
commit;
select * from players;