Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Datenbanken (https://www.delphipraxis.net/15-datenbanken/)
-   -   Delphi SQL-Statement escapen (https://www.delphipraxis.net/132816-sql-statement-escapen.html)

Andreas L. 20. Apr 2009 13:49

Datenbank: SQLite • Version: 3 • Zugriff über: SQLiteTable3.pas

SQL-Statement escapen
 
Hi,

ich schreibe mit folgenden Code Daten in eine SQLite-Datenbank:

Delphi-Quellcode:
      db.ExecSQL('INSERT INTO moz_cookies (id, name, value, host, path, expiry, lastAccessed, isSecure, isHTTPOnly)' +
                 ' VALUES ("' + IntToStr(DateTimeToUnix(Cookies[iCookie].Created) * 1000000) + '", "' +
                 Cookies[iCookie].Name + '", "' +
                 Cookies[iCookie].Content + '", "' +
                 Cookies[iCookie].Domain + '", "' +
                 Cookies[iCookie].Path + '", "' +
                 IntToStr(DateTimeToUnix(Cookies[iCookie].Expires)) + '", "' +
                 IntToStr(DateTimeToUnix(Cookies[iCookie].LastAccessed) * 1000000) + '", "' +
                 BoolToStr(Cookies[iCookie].Secure) + '", "' +
                 BoolToStr(Cookies[iCookie].WholeDomain) + '")');
Jetzt kann es aber sein das in einer der Variablen ein Anführungszeichen ( " ) enthalten ist. Dadurch wird das Statement natürlich nicht mehr ausführbar. Gibt es eine Möglichkeit dieses Zeichen zu escapen?

Grüße,
Andy

mkinzler 20. Apr 2009 13:51

Re: SQL-Statement escapen
 
Ich würde (SQL-)Parameter verwenden:
SQL-Code:
INSERT INTO moz_cookies (id, name, value, host, path, expiry, lastAccessed, isSecure, isHTTPOnly) VALUES ( :id, :name, ...);

Andreas L. 20. Apr 2009 13:53

Re: SQL-Statement escapen
 
Hi,

Zitat:

Zitat von mkinzler
Ich würde (SQL-)Parameter verwenden:
SQL-Code:
INSERT INTO moz_cookies (id, name, value, host, path, expiry, lastAccessed, isSecure, isHTTPOnly) VALUES ( :id, :name, ...);

wie setze ich denn die Werte für diese Parameter?

Hansa 20. Apr 2009 14:02

Re: SQL-Statement escapen
 
Mit ParamByName. => F1

khh 20. Apr 2009 14:03

Re: SQL-Statement escapen
 
Zitat:

Zitat von Andreas L.

wie setze ich denn die Werte für diese Parameter?

Query1.ParamByName('id').AsInteger := 123;


grussw KH

Andreas L. 20. Apr 2009 14:07

Re: SQL-Statement escapen
 
Zitat:

Zitat von khh
Zitat:

Zitat von Andreas L.

wie setze ich denn die Werte für diese Parameter?

Query1.ParamByName('id').AsInteger := 123;


grussw KH

Ich verwende aber nicht TQuery und möchte das auch nicht, weil Leute ohne DB-Komponenten die Unit verwenden können sollen.

EDIT: ich verwende diesen SQLite-Wrapper: http://www.itwriting.com/blog/a-simp...r-for-sqlite-3

shmia 20. Apr 2009 14:15

Re: SQL-Statement escapen
 
Zitat:

Zitat von Andreas L.
Anführungszeichen ( " ) enthalten ist. .... Gibt es eine Möglichkeit dieses Zeichen zu escapen?

Die gesuchte Funktion heisst QuoteStr() aus Unit SysUtils.
Sie packt den übergebenen String in einfache Hochkommas was im Übrigen dem SQL-Standard entspricht.

mkinzler 20. Apr 2009 14:17

Re: SQL-Statement escapen
 
Zitat:

Zitat von SQLite3.pas
//
Delphi-Quellcode:
// In the SQL strings input to sqlite3_prepare() and sqlite3_prepare16(),
// one or more literals can be replace by a wildcard "?" or ":N:" where
// N is an integer. These value of these wildcard literals can be set
// using the routines listed below.
// 
// In every case, the first parameter is a pointer to the sqlite3_stmt
// structure returned from sqlite3_prepare(). The second parameter is the
// index of the wildcard. The first "?" has an index of 1. ":N:" wildcards
// use the index N.
// 
// The fifth parameter to sqlite3_bind_blob(), sqlite3_bind_text(), and
//sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
//text after SQLite has finished with it. If the fifth argument is the
// special value SQLITE_STATIC, then the library assumes that the information
// is in static, unmanaged space and does not need to be freed. If the
// fifth argument has the value SQLITE_TRANSIENT, then SQLite makes its
// own private copy of the data.
// 
// The sqlite3_bind_* routine must be called before sqlite3_step() after
// an sqlite3_prepare() or sqlite3_reset(). Unbound wildcards are interpreted
// as NULL.
// 

type
  TSQLite3Destructor = procedure(Ptr: Pointer); cdecl;

function sqlite3_bind_blob(hStmt: TSqliteStmt; ParamNum: integer;
  ptrData: pointer; numBytes: integer; ptrDestructor: TSQLite3Destructor): integer;
cdecl; external SQLiteDLL name 'sqlite3_bind_blob';
function sqlite3_bind_text(hStmt: TSqliteStmt; ParamNum: integer;
  Text: PAnsiChar; numBytes: integer; ptrDestructor: TSQLite3Destructor): integer;
cdecl; external SQLiteDLL name 'sqlite3_bind_text';
function sqlite3_bind_double(hStmt: TSqliteStmt; ParamNum: integer; Data: Double): integer;
  cdecl; external SQLiteDLL name 'sqlite3_bind_double';
function sqlite3_bind_int(hStmt: TSqLiteStmt; ParamNum: integer; Data: integer): integer;
  cdecl; external SQLiteDLL name 'sqlite3_bind_int';
function sqlite3_bind_int64(hStmt: TSqliteStmt; ParamNum: integer; Data: int64): integer;
  cdecl; external SQLiteDLL name 'sqlite3_bind_int64';
function sqlite3_bind_null(hStmt: TSqliteStmt; ParamNum: integer): integer;
  cdecl; external SQLiteDLL name 'sqlite3_bind_null';

function sqlite3_bind_parameter_index(hStmt: TSqliteStmt; zName: PAnsiChar): integer;
  cdecl; external SQLiteDLL name 'sqlite3_bind_parameter_index';


Andreas L. 20. Apr 2009 14:23

Re: SQL-Statement escapen
 
Dieser Kommentar steht zwar bei meiner SQLite.pas nicht aber die Funktion sqlite3_prepare ist vorhanden. Werds später ausprobieren. Danke :o

mkinzler 20. Apr 2009 14:30

Re: SQL-Statement escapen
 
Dann würde ich updatetn. Dieser Kommentar stamt aus der aktuellen Version (gerade heruntergeladen)


Alle Zeitangaben in WEZ +1. Es ist jetzt 05:02 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz