1. The basic query statement is followed by a WHERE 1=1 to facilitate the query condition.
ASKSTR: = ' select * from twork where 1=1 ', if Length (Cxteworkid.text) <> 0 Then Beginaskstr: = Askstr + ' and Gworki D like ' +quotedstr ('% ' + cxteworkid.text + '% ') end;
2. Multiple-table queries can replace a table with a single string. Logically clearer.
worktable = ' Select Gworkid from twork where 1=1 '; paytable = ' select sum (gxmtotal) as Sgxmtotal,gxmtype from Tpay where Gworkid in '; Paygroupby = ' Group by Gxmtype ';
ASKSTR: = paytable + ' (' + worktable + tmpwhere + ') ' + paygroupby;
3. Define a connection function to determine if the initialization of the database is successful and easy to control.
function Tdm.initdbok:boolean;begin Result: = true; If FileExists (dbfilename) then begin Fdconn.drivername: = ' SQLite '; Fdconn.loginprompt: = false; FDConn.Params.Clear; fdconn.params.values[' Database ': = Dbfilename; fdconn.params.values[' DriverID ': = ' SQLite '; fdconn.params.values[' CharacterSet ': = ' utf8 '; Try fdconn.connected: = true; Except result: = false; End; End Else result: = False;end;
SQL query Statement splicing tips (master do not spray)