Delphi FireDAC 下的 Sqlite(五) 資料的插入、更新和刪除

來源:互聯網
上載者:User

先在空白表單上添加: TFDConnection、TFDPhysSQLiteDriverLink、TFDGUIxWaitCursor、TFDQuery、TDataSource、TDBGrid(並在設計時關聯好). 你也可以複製下面文字框中的內容, 然後直接往表單上貼, 以快速完成以上的添加過程: object DBGrid1: TDBGrid Left = 16 Top = 88 Width = 361 Height = 329 DataSource = DataSource1 TabOrder = 0 TitleFont.Charset = DEFAULT_CHARSET TitleFont.Color = clWindowText TitleFont.Height = -11 TitleFont.Name = 'Tahoma' TitleFont.Style = [] end object FDConnection1: TFDConnection Left = 34 Top = 24 end object FDPhysSQLiteDriverLink1: TFDPhysSQLiteDriverLink Left = 143 Top = 24 end object FDGUIxWaitCursor1: TFDGUIxWaitCursor Provider = 'Forms' Left = 260 Top = 24 end object FDQuery1: TFDQuery Connection = FDConnection1 Left = 344 Top = 24 end object DataSource1: TDataSource DataSet = FDQuery1 Left = 420 Top = 24 end object Button1: TButton Left = 400 Top = 88 Width = 75 Height = 25 Caption = 'Button1' TabOrder = 1 OnClick = Button1Click end object Button2: TButton Left = 400 Top = 136 Width = 75 Height = 25 Caption = 'Button2' TabOrder = 2 OnClick = Button2Click end object Button3: TButton Left = 400 Top = 192 Width = 75 Height = 25 Caption = 'Button3' TabOrder = 3 OnClick = Button3Click end object Button4: TButton Left = 400 Top = 240 Width = 75 Height = 25 Caption = 'Button4' TabOrder = 4 OnClick = Button4Click end end

更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Delphi/

代碼:

{建立}procedure TForm1.FormCreate(Sender: TObject);const  dbPath = 'C:\Temp\SQLiteTest.sdb';  strTable = 'CREATE TABLE MyTable(Id integer PRIMARY KEY AUTOINCREMENT, Name string(10), Age byte)'; //Id, Name, Age 三個欄位  //integer PRIMARY KEY AUTOINCREMENT: 自增欄位begin  if FileExists(dbPath) then DeleteFile(dbPath);  FDConnection1.ConnectionString := 'DriverID=SQLite; Database=' + dbPath;  FDConnection1.ExecSQL(strTable);  FDQuery1.Open('SELECT * FROM MyTable');end;{插入}procedure TForm1.Button1Click(Sender: TObject);const  strInsert = 'INSERT INTO MyTable(Name, Age) VALUES(:name, :age)'; //:name, :age 的方式(後面還要以數組的方式給出相應的值), 這比字串的 Format 函數還要方便.begin  FDConnection1.ExecSQL(strInsert, ['AAA', 11]);  FDConnection1.ExecSQL(strInsert, ['BBB', 22]);  FDConnection1.ExecSQL(strInsert, ['CCC', 33]);  FDConnection1.ExecSQL(strInsert, ['DDD', 44]);  FDConnection1.ExecSQL(strInsert, ['EEE', 55]);  FDQuery1.Refresh;end;{更新}procedure TForm1.Button2Click(Sender: TObject);begin  FDConnection1.ExecSQL('UPDATE MyTable SET Age=:a WHERE Name=:n', [Random(100), 'AAA']);  FDQuery1.Refresh;end;{刪除}procedure TForm1.Button3Click(Sender: TObject);begin  FDConnection1.ExecSQL('DELETE FROM MyTable WHERE Age>33');  FDQuery1.Refresh;end;{查詢合格第一個結果}procedure TForm1.Button4Click(Sender: TObject);var  V: Variant;begin  V := FDConnection1.ExecSQLScalar('SELECT Age FROM MyTable WHERE Name = :x', ['BBB']);  ShowMessage(V);end;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.