开发者

What are the rules for closing prepared statements?

开发者 https://www.devze.com 2023-03-16 15:59 出处:网络
I\'m currently using SQLite3 class for PHP. I\'ve noticed SQLite3Stmt::close() as well as SQLite3Result::finalize().

I'm currently using SQLite3 class for PHP. I've noticed SQLite3Stmt::close() as well as SQLite3Result::finalize().

Is it essential to call these functions? I'm having a bit of trouble calling them because I don't exactly know when they're no longer being used in my database wrapper class as I will have to often return a SQLite3Result

For example:

function ex开发者_StackOverflow社区ecSomething(){
  $stmt = $db->prepare($sql);
  return $stmt->execute();
}

Should you always close the prepared statement?


I was curious too, because for someone unfamiliar with PHP and its SQLite3 extension, the documentation doesn't make clear what these functions actually do (i.e., what's happening in SQLite when you call them?):

SQLite3Stmt::close — Closes the prepared statement

SQLite3Result::finalize — Closes the result set

Taking a (highly unqualified) look at the source (https://github.com/php/php-src/blob/master/ext/sqlite3/sqlite3.c), at the time of this writing:

SQLite3Result::finalize, at PHP_METHOD(sqlite3result, finalize), seems to end up calling sqlite3_reset on any associated prepared statement, as well as freeing some internal data structures.

SQLite3Stmt::close, at PHP_METHOD(sqlite3stmt, close), seems to just end up freeing internal data structures for the statement.

Thus, as far as I can tell, it seems to be a question of managing internal resources and of triggering calls to sqlite3_reset, which according to the SQLite docs:

"...[resets] a prepared statement object back to its initial state, ready to be re-executed"

No comment as to what happens when your script terminates and you haven't called these, as I haven't tried to look into that.

All that said, I'm a newbie in this department and it's the first time I've cracked open the PHP source, so I'd appreciate a PHP/SQLite3 expert stepping in to correct anything wrong or misleading here.


I don't exactly know when they're no longer being used in my database wrapper class

Can you create a destructor on your PHP wrapper class that calls these methods? When your wrapper object is destroyed by PHP's garbage collector you'll know that your statements/results will no longer be used.


I'm having a bit of trouble calling them because I don't exactly know when they're no longer being used in my database wrapper class as I will have to often return a SQLite3Result

Usually you will get a new result object every time you execute a query. This means, you should close the result set as soon as you don't need this one anymore.

However, you don't have to free every resource yourself, because PHP will do it for you the time the execution ends. If you don't create many many (means something around ... don't know 50?) prepared statements, I don't see a reason, why you should close them yourself.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号