词条 | Prepared statement |
释义 |
In database management systems (DBMS), a prepared statement or parameterized statement is a feature used to execute the same or similar database statements repeatedly with high efficiency. Typically used with SQL statements such as queries or updates, the prepared statement takes the form of a template into which certain constant values are substituted during each execution. The typical workflow of using a prepared statement is as follows:
As compared to executing statements directly, prepared statements offer two main advantages:[1]
On the other hand, if a query is executed only once, server-side prepared statements can be slower because of the additional round-trip to the server.[3] Implementation limitations may also lead to performance penalties; for example, some versions of MySQL did not cache results of prepared queries.[4] A stored procedure, which is also precompiled and stored on the server for later execution, has similar advantages. Unlike a stored procedure, a prepared statement is not normally written in a procedural language and cannot use or modify variables or use control flow structures, relying instead on the declarative database query language. Due to their simplicity and client-side emulation, prepared statements are more portable across vendors. Software supportMajor DBMSs, including MySQL,[5] Oracle,[6] DB2,[7] Microsoft SQL Server[8] and PostgreSQL[9] widely support prepared statements. Prepared statements are normally executed through a non-SQL binary protocol for efficiency and protection from SQL injection, but with some DBMSs such as MySQL prepared statements are also available using a SQL syntax for debugging purposes.[10] A number of programming languages support prepared statements in their standard libraries and will emulate them on the client side even if the underlying DBMS does not support them, including Java's JDBC,[11] Perl's DBI,[12] PHP's PDO [1] and Python's DB-API.[13] Client-side emulation can be faster for queries which are executed only once, by reducing the number of round trips to the server, but is usually slower for queries executed many times. It resists SQL injection attacks equally effectively. Many types of SQL injection attacks can be eliminated by disabling literals, effectively requiring the use of prepared statements; {{as of | 2007 | lc = on}} only H2 supports this feature.[14] ExamplesJava JDBCThis example uses Java and JDBC: Java PHP PDOThis example uses PHP and PDO: Perl DBIThis example uses Perl and DBI: C# ADO.NETThis example uses C# and ADO.NET: ADO.NET However, the AddWithValue method should not be used with variable length data types, like varchar and nvarchar. This is because .NET assumes the length of the parameter to be the length of the given value, rather than getting the actual length from the database via reflection. The consequence of this is that a different query plan is compiled and stored for each different length. In general, the maximum number of "duplicate" plans is the product of the lengths of the variable length columns as specified in the database. For this reason, it is important to use the standard Add method for variable length columns: {{code|lang=csharp|1=command.Parameters.Add(ParamName, VarChar, ParamLength).Value = ParamValue}}, where ParamLength is the length as specified in the database.Since the standard Add method needs to be used for variable length data types, it is a good habit to use it for all parameter types. Python DB-APIThis example uses Python and DB-API: Magic Direct SQLThis example uses Direct SQL from Fourth generation language like eDeveloper, uniPaaS and magic XPA from Magic Software Enterprises Virtual username Alpha 20 init: 'sister' Virtual password Alpha 20 init: 'yellow' SQL Command: Input Arguments: 1: username 2: password PureBasicPureBasic (since v5.40 LTS) can manage 7 types of link with the following commands SetDatabaseBlob, SetDatabaseDouble, SetDatabaseFloat, SetDatabaseLong, SetDatabaseNull, SetDatabaseQuad, SetDatabaseString There are 2 different methods depending on the type of database For SQLite, ODBC, MariaDB/Mysql use: ? SetDatabaseString(#Database, 0, "test") If DatabaseQuery(#Database, "SELECT * FROM employee WHERE id=?") ; ... EndIf For PostgreSQL use: $1, $2, $3, ... SetDatabaseString(#Database, 0, "Smith") ; -> $1 SetDatabaseString(#Database, 1, "Yes") ; -> $2 SetDatabaseLong (#Database, 2, 50) ; -> $3 If DatabaseQuery(#Database, "SELECT * FROM employee WHERE id=$1 AND active=$2 AND years>$3") ; ... EndIf References1. ^1 {{cite web|last=The PHP Documentation Group|title=Prepared statements and stored procedures|url=http://php.net/manual/en/pdo.prepared-statements.php|work=PHP Manual|accessdate=25 September 2011}} 2. ^{{cite web|last=Petrunia|first=Sergey|title=MySQL Optimizer and Prepared Statements|url=http://s.petrunia.net/blog/?p=16|work=Sergey Petrunia's blog|date=28 April 2007|accessdate=25 September 2011}} 3. ^{{cite web|last=Zaitsev|first=Peter|title=MySQL Prepared Statements|url=http://www.mysqlperformanceblog.com/2006/08/02/mysql-prepared-statements/|work=MySQL Performance Blog|date=2 August 2006|accessdate=25 September 2011}} 4. ^{{cite web|title=7.6.3.1. How the Query Cache Operates|url=http://dev.mysql.com/doc/refman/5.1/en/query-cache-operation.html|work=MySQL 5.1 Reference Manual|publisher=Oracle|accessdate=26 September 2011}} 5. ^{{cite web|last= Oracle|title= 20.9.4. C API Prepared Statements|url= http://dev.mysql.com/doc/refman/5.5/en/c-api-prepared-statements.html|work= MySQL 5.5 Reference Manual|accessdate= 27 March 2012}} 6. ^{{cite web|title= 13 Oracle Dynamic SQL|url= http://download.oracle.com/docs/cd/B10501_01/appdev.920/a97269/pc_13dyn.htm|work= Pro*C/C++ Precompiler Programmer's Guide, Release 9.2|publisher= Oracle|accessdate= 25 September 2011}} 7. ^{{cite web|title =Using the PREPARE and EXECUTE statements|url= http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=%2Fsqlp%2Frbafyplepexc.htm|work=i5/OS Information Center, Version 5 Release 4|publisher= IBM|accessdate= 25 September 2011}} 8. ^{{cite web|title= SQL Server 2008 R2: Preparing SQL Statements|url= http://msdn.microsoft.com/en-us/library/ms175528.aspx|work= MSDN Library|publisher= Microsoft|accessdate= 25 September 2011}} 9. ^{{cite web|title= PREPARE|url= http://www.postgresql.org/docs/9.5/static/sql-prepare.html|work= PostgreSQL 9.5.1 Documentation|publisher= PostgreSQL Global Development Group|accessdate= 27 February 2016}} 10. ^{{cite web|last= Oracle|title= 12.6. SQL Syntax for Prepared Statements|url= http://dev.mysql.com/doc/refman/5.5/en/sql-syntax-prepared-statements.html|work= MySQL 5.5 Reference Manual|accessdate= 27 March 2012}} 11. ^{{cite web|title= Using Prepared Statements|url= http://download.oracle.com/javase/tutorial/jdbc/basics/prepared.html|work= The Java Tutorials|publisher= Oracle|accessdate= 25 September 2011}} 12. ^{{cite web|last= Bunce|first= Tim|title= DBI-1.616 specification|url= https://metacpan.org/module/DBI#prepare|work= CPAN|accessdate= 26 September 2011}} 13. ^{{cite web|title= Python PEP 289: Python Database API Specification v2.0|url= https://www.python.org/dev/peps/pep-0249/ }} 14. ^{{cite web |url= http://thecodist.com/article/sql-injections-how-not-to-get |title= SQL Injections: How Not To Get Stuck |publisher= The Codist |date= 8 May 2007 |accessdate= February 1, 2010}} 2 : Databases|SQL |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。