SQLite3::exec

(PHP 5 >= 5.3.0, PHP 7)

SQLite3::execExecutes a result-less query against a given database

说明

public bool SQLite3::exec ( string $query )

Executes a result-less query against a given database.

参数

query

The SQL query to execute (typically an INSERT, UPDATE, or DELETE query).

返回值

Returns TRUE if the query succeeded, FALSE on failure.

范例

Example #1 SQLite3::exec() example

<?php
$db 
= new SQLite3('mysqlitedb.db');

$db->exec('CREATE TABLE bar (bar STRING)');
?>

User Contributed Notes

alexandre dot schmidt at gmail dot com 14-Feb-2016 02:38
I was getting "database locked" all the time until I found out some features of sqlite3 must be set by using SQL special instructions (i.e. using PRAGMA keyword). For instance, what apparently solved my problem with "database locked" was to set journal_mode to 'wal' (it is defaulting to 'delete', as stated here: https://www.sqlite.org/wal.html (see Activating  And Configuring WAL Mode)).

So basically what I had to do was creating a connection to the database and setting journal_mode with the SQL statement. Example:

<?php
$db
= new SQLite3('/my/sqlite/file.sqlite3');
$db->busyTimeout(5000);
// WAL mode has better control over concurrency.
// Source: https://www.sqlite.org/wal.html
$db->exec('PRAGMA journal_mode = wal;');
?>

Hope that helps.
namibj 27-Oct-2014 11:52
If you use WAL (Write-Ahead Log) (link to sqlite doc for this: http://sqlite.org/wal.html ), it needs write acces because of the implementation of multi-thread access to one db. You can turn it off, if you do not want to give the dir write access, but you can also create a certain file it needs (see the link for a description, as it is explained very well there) that you need write access to and you may get away using WAL without giving write accesss to the dir.
Rob Haverkort 04-Nov-2013 04:57
Actually, sqlite creates a journal-file when changing data and so it needs the write-permissions in the directory.
gamag 07-Mar-2013 06:02
SQLite needs to create some temp-files (journals...) to execute certain statements, so php needs write-permission in your db-directory.
info at tellmatic dot org 17-Jan-2013 12:50
IMPORTANT! just a note:

weird behaviour when doing an exec on a sqlite db!!!

if want to execute a query on a sqlite db with exec, and your dbfile already was e.g. mode 777, and you get some php errors saying

"SQLite3::exec(): unable to open database file in ...."

and you get crazy while debugging, just add write üermissions to the whole directory for the user the webserver/php runs.

this behaviour makes absolutely NO sense, and is a source of frustration.
at least a more meaningful errormessage would be nice.
i couldnt figure out why sqlite needs write permissions for the whole dir instead of only one file. this is stupid and must be a bug!
(to be secure you have to create a directory with write permissions only for php/apache)
moodsey211 at gmail dot com 17-Dec-2010 12:29
If you get the error message saying "SQLite3::exec. database locked." You just need to define a busyTimeout to work around this.