SunshinePHP Developer Conference 2015

sqlite_exec

SQLiteDatabase::exec

(PHP 5 < 5.4.0, PECL sqlite >= 1.0.3)

sqlite_exec -- SQLiteDatabase::execExecutes a result-less query against a given database

Descrição

bool sqlite_exec ( resource $dbhandle , string $query [, string &$error_msg ] )
bool sqlite_exec ( string $query , resource $dbhandle )

Estilo orientado à objeto (method):

public bool SQLiteDatabase::queryExec ( string $query [, string &$error_msg ] )

Executes an SQL statement given by the query against a given database handle (specified by the dbhandle parameter).

Aviso

SQLite will execute multiple queries separated by semicolons, so you can use it to execute a batch of SQL that you have loaded from a file or have embedded in a script.

Parâmetros

dbhandle

The SQLite Database resource; returned from sqlite_open() when used procedurally. This parameter is not required when using the object-oriented method.

query

The query to be executed.

Data inside the query should be properly escaped.

error_msg

The specified variable will be filled if an error occurs. This is specially important because SQL syntax errors can't be fetched using the sqlite_last_error() function.

Nota: Duas sintaxes alternativas são suportaas para compatibilidade com outras extensões de banco de dados( como o MySQL). A forma preferida é a primeira, aonde o parâmetro dbhandle é o primeiro parãmetro da função.

Valor Retornado

This function will return a boolean result; TRUE for success or FALSE for failure. If you need to run a query that returns rows, see sqlite_query().

Os nomes de colunas retornados por SQLITE_ASSOC e SQLITE_BOTH serão serão modificados para maiúsculas/minúsculas de acordo com o valor da opção de configuração sqlite.assoc_case .

Changelog

Versão Descrição
5.1.0 Added the error_msg parameter

Exemplos

Exemplo #1 Procedural example

<?php
$dbhandle 
sqlite_open('mysqlitedb');
$query sqlite_exec($dbhandle"UPDATE users SET email='jDoe@example.com' WHERE username='jDoe'"$error);
if (!
$query) {
    exit(
"Error in query: '$error'");
} else {
    echo 
'Number of rows modified: 'sqlite_changes($dbhandle);
}
?>

Exemplo #2 Object-oriented example

<?php
$dbhandle 
= new SQLiteDatabase('mysqlitedb');
$query $dbhandle->queryExec("UPDATE users SET email='jDoe@example.com' WHERE username='jDoe'"$error);
if (!
$query) {
    exit(
"Error in query: '$error'");
} else {
    echo 
'Number of rows modified: '$dbhandle->changes();
}
?>

Veja Também

add a note add a note

User Contributed Notes 2 notes

up
0
nonproffessional at clockworkgeek dot com
6 years ago
Just to point out that "CREATE TABLE IF NOT EXISTS" is only supported in SQLite version 3.3.0 or above. And PHP (currently 5.2.5) only comes with SQLite version 2.1.

Executing a create table like this will throw an error as will creating a table that already exists. Instead execute a normal "CREATE TABLE" command and catch it with "try {..} catch".
up
0
rrf5000 at psu dot edu
7 years ago
If you run a multiline SQL command (an INSERT, for example), and there is a SQL error in any of the lines, this function will recognize the error and return FALSE.  However, any correct commands before the one with the error will still execute.  Additionally, if you run changes() after such an incident, it will report that 0 rows have been changed, even though there were rows added to the table by the successful commands.

An example would be:

<?php

// create new database (OO interface)
$dbo = new SQLiteDatabase("db/database.sqlite");

// create table foo
$dbo->query("CREATE TABLE foo(id INTEGER PRIMARY KEY, name CHAR(255));");

// insert sample data
$ins_query = "INSERT INTO foo (name) VALUES ('Ilia1');
INSERT INTO foo (name) VALUES('Ilia2');
INSECT INTO foo (name) VALUES('Ilia3');"
;
$dbo->queryExec($ins_query);

// get number of rows changed
$changes = $dbo->changes();
echo
"<br />Rows changed:  $changes<br />";

// Get and show inputted data
$tableArray = $dbo->arrayQuery("SELECT * FROM foo;");
echo
"Table Contents\n";
echo
"<pre>\n";
print_r($tableArray);
echo
"\n</pre>";

?>

The above code should show that 0 rows have been changed, but that there is new data in the table.
To Top