disqlite3_demos_common.pas
来自「DELPHI 访问SQLITE3 数据库的VCL控件」· PAS 代码 · 共 159 行
PAS
159 行
{ DISQLite3 demos common unit.
Visit the DISQLite3 Internet site for latest information and updates:
http://www.yunqa.de/delphi/
Copyright (c) 2005-2007 Ralf Junker, The Delphi Inspiration <delphi@yunqa.de>
------------------------------------------------------------------------------ }
unit DISQLite3_Demos_Common;
interface
uses
DISQLite3Api;
const
CRLF = #13#10;
procedure Close_Demo_Database;
procedure Create_Demo_Tables;
procedure Open_Demo_Database;
function Demo_Database_Name: AnsiString;
{ Writes a prepared statement to StdOut. }
procedure WritePreparedStatement(
const DB: TDISQLite3DatabaseHandle;
const Stmt: TDISQLite3StatementHandle);
{ Creates a string composed of random letters from A to Z. }
function RandomString(const AMaxLength: Integer): AnsiString;
var
DB: TDISQLite3DatabaseHandle;
implementation
uses
SysUtils;
//------------------------------------------------------------------------------
procedure Close_Demo_Database;
begin
sqlite3_check(sqlite3_close(DB), DB);
end;
//------------------------------------------------------------------------------
procedure Create_Demo_Tables;
begin
sqlite3_exec_fast(DB,
'CREATE TABLE IF NOT EXISTS RandomTable (' +
'RandomText TEXT,' +
'RandomInt INTEGER);');
end;
//------------------------------------------------------------------------------
procedure Open_Demo_Database;
begin
sqlite3_check(sqlite3_open(PAnsiChar(Demo_Database_Name), @DB), DB);
end;
//------------------------------------------------------------------------------
function Demo_Database_Name: AnsiString;
begin
Result := ExpandFileName(ExtractFilePath(ParamStr(0)) + '\..\Demo.db3');
end;
//------------------------------------------------------------------------------
procedure WritePreparedStatement(
const DB: TDISQLite3DatabaseHandle;
const Stmt: TDISQLite3StatementHandle);
var
ColumnCount: Integer;
e: Integer;
i: Integer;
begin
{ Did our SQL query compile into a prepared SQL statement? }
if Assigned(Stmt) then
begin
{ sqlite3_step executes the prepared statement. }
e := sqlite3_check(sqlite3_step(Stmt), DB);
{ Check if the query returnd at least one row of data. }
if e = SQLITE_ROW then
begin
{ If we have data rows, store the number of columns returned. }
ColumnCount := sqlite3_column_count(Stmt);
{ First write a single line with the column names. }
for i := 0 to ColumnCount - 1 do
begin
if i > 0 then Write('|');
Write(sqlite3_column_name(Stmt, i));
end;
WriteLn; WriteLn;
{ Next iterate all data rows and columns, retrieve the data and
write it to the console. }
repeat
for i := 0 to ColumnCount - 1 do
begin
if i > 0 then Write('|');
{ We optimize the data output according to the column type. }
case sqlite3_column_type(Stmt, i) of
SQLITE_INTEGER:
Write(sqlite3_column_int64(Stmt, i));
SQLITE_FLOAT:
Write(sqlite3_column_double(Stmt, i));
SQLITE_TEXT:
Write(sqlite3_column_str(Stmt, i));
SQLITE_BLOB:
Write('BLOB'); // Don't write BLOB data.
SQLITE_NULL:
Write('NULL'); // Write NULL indicator.
end;
end;
WriteLn;
{ Retrieve next row of data and continue until no more rows found. }
e := sqlite3_check(sqlite3_step(Stmt), DB);
until e <> SQLITE_ROW;
end;
end;
end;
//------------------------------------------------------------------------------
function RandomString(const AMaxLength: Integer): AnsiString;
var
l: Integer;
p: PAnsiChar;
begin
Assert(AMaxLength > 0);
repeat
l := Random(AMaxLength);
until l > 0;
SetString(Result, nil, l);
p := Pointer(Result);
repeat
p^ := AnsiChar(Ord('A') + Random(24));
Inc(p); Dec(l);
until l = 0;
end;
end.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?