delphi DISQLite3使用例子

unit main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, ComCtrls, StdCtrls, DB, DISQLite3DataSet, DISQLite3Database,
  DISQLite3Cache, DISQLite3Api;

type
  TForm5 = class(TForm)
    MainMenu1: TMainMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ListView_keydogs: TListView;
    Memo_log: TMemo;
    StatusBar1: TStatusBar;
    Button1: TButton;
    Button2: TButton;
    PopupMenu_keydog: TPopupMenu;
    N3: TMenuItem;
    N4: TMenuItem;
    sqlite3Database1: TDISQLite3Database;
    sqlite3UniDirQuery1: TDISQLite3UniDirQuery;
    procedure N2Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  PTKeyDog = ^TKeyDog;

  TKeyDog = record
    id: Integer;
    projectname: string;
    city: string;
    letter: string;
    hash: string;
    code: string;
  end;

var
  Form5: TForm5;

implementation

{$R *.dfm}

procedure TForm5.Button2Click(Sender: TObject);
var
  dbname: string;
  stmt: TDISQLite3Statement;
  pkeydog: PTKeyDog;
  list: TList;
  count: Integer;
  i: Integer;
  arrs: TArray<TKeyDog>;

begin
  // http://www.yunqa.de/delphi/doku.php/wiki/sqlite3/sqlite3_get_table
  // demo  http://www.yunqa.de/delphi/doku.php/wiki/sqlite3/disqlite3_blobs_and_streams
  // http://www.yunqa.de/delphi/doku.php/wiki/sqlite3/index?do=index
  dbname := 'keydog.db';

  sqlite3Database1.DatabaseName := dbname;
  // sqlite3Database1.Password := 'jp123456';

  if FileExists(dbname) then
  begin
    DeleteFile(dbname);
  end;

  if FileExists(dbname) = False then
  begin
    sqlite3Database1.CreateDatabase;
  end;
  sqlite3Database1.Connected := True;
  sqlite3Database1.Open;
  sqlite3Database1.Execute(
    'CREATE TABLE IF NOT EXISTS keydog(id INTEGER PRIMARY KEY AUTOINCREMENT,projectname TEXT, city TEXT,letter TEXT,hash TEXT,code TEXT); ');

  sqlite3Database1.Execute(
    'INSERT INTO keydog(projectname, city,letter,hash,code) VALUES (''xx'', ''北京'', ''beijing'', ''xxxxxx'',''code''); ');

  sqlite3Database1.Execute(
    'INSERT INTO keydog(projectname, city,letter,hash,code) VALUES (''xx'', ''上海'', ''shanghai'', ''xxxxxx'',''code''); ');

  stmt := sqlite3Database1.Prepare(
    'INSERT INTO keydog(projectname, city,letter,hash,code) VALUES (?, ?, ?, ?, ? ); ');
  try
    stmt.Bind_Str(1, 'xxx');
    stmt.Bind_Str(2, '广州');
    stmt.Bind_Str(3, 'guangzhou');
    stmt.Bind_Str(4, 'fuck');
    stmt.Bind_Str(5, 'code');
    stmt.Step;
  finally
    stmt.Free;
  end;

  stmt := sqlite3Database1.Prepare('select * from keydog;');
  count := 0;
  list := TList.Create;

  try
    while stmt.Step = SQLITE_ROW do
    begin
      New(pkeydog);
      pkeydog.id := stmt.Column_Int64(0);
      pkeydog.projectname := string(stmt.Column_Str(1));
      pkeydog.city := string(stmt.Column_Str(2));
      pkeydog.letter := string(stmt.Column_Str(3));
      pkeydog.hash := string(stmt.Column_Str(4));
      pkeydog.code := string(stmt.Column_Str(5));
      list.Add(pkeydog);

      Inc(count);
      // ShowMessage(keydog.code);
    end;
  finally
    stmt.Free;
  end;
  ShowMessage(IntToStr(count));

  for i := 0 to list.count - 1 do
  begin

    pkeydog := list.Items[i];
    ShowMessage(pkeydog.projectname+pkeydog.city+pkeydog.letter+pkeydog.hash+pkeydog.code);
    Dispose(pkeydog);
  end;
  list.Free;

  // sqlite3Database1.Open;
  // sqlite3Database1.Execute('');
  sqlite3Database1.Close;

end;

procedure TForm5.N2Click(Sender: TObject);
begin
  Application.Terminate;
end;

end.

你可能感兴趣的:(delphi DISQLite3使用例子)