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".
sqlite_exec
SQLiteDatabase::exec
(PHP 5 < 5.4.0, PECL sqlite >= 1.0.3)
sqlite_exec -- SQLiteDatabase::exec — Esegue una query priva di risultato in un dato database
Descrizione
$dbhandle
, string $query
[, string &$error_msg
] )$query
, resource $dbhandle
)Stile orientato agli oggetti (metodo):
$query
[, string &$error_msg
] )
Esegue un'espressione SQL, indicata da query sul database
(specificato dal parametro
dbhandle).
SQLite esegue molteplici query separate da punto e virgola, in questo modo si può eseguire un batch di istruzioni SQL caricate da un file o racchiuse in uno script.
Elenco dei parametri
-
dbhandle -
Risorsa SQLite Database; restituita da sqlite_open () quando usato in modo procedurale. Questo parametro non è richiesto nel metodo ad oggetti.
-
query -
Query da eseguire.
I dati all'interno della query devono essere il risultato della funzione escape.
-
error_msg -
La variabile indicata sarà riempita se si verifica un errore. Questo è particolarmente importante perché gli errori di sintassi di SQL non possono essere scaricati utilizzando la funzione sqlite_last_error().
Nota: Due sintassi alternative sono supportate per compatibilità con altre estensioni di database (come MySQL). La forma preferita è la prima, dove il parametro
dbhandleè il primo parametro per la funzione.
Valori restituiti
Questa funzione restituisce un risultato booleano; TRUE per successo o FALSE per insuccesso.
Se si deve eseguire una query che restituisca un risultato, guardare sqlite_query().
I nomi delle colonne restituiti da
SQLITE_ASSOC e da SQLITE_BOTH saranno
maiuscoli o minuscoli in base al valore del parametro di configurazione
sqlite.assoc_case
.
Log delle modifiche
| Versione | Descrizione |
|---|---|
| 5.1.0 |
Aggiunto il parametro error_msg
|
Esempi
Example #1 Esempio procedurale
<?php
$dbhandle = sqlite_open('mysqlitedb');
$query = sqlite_exec($dbhandle, "UPDATE users SET email='jDoe@example.com' WHERE username='jDoe'", $error);
if (!$query) {
exit("Errore nella query: '$error'");
} else {
echo 'Numero di righe modificate: ', sqlite_changes($dbhandle);
}
?>
Example #2 Esempio ad oggetti
<?php
$dbhandle = new SQLiteDatabase('mysqlitedb');
$query = $dbhandle->queryExec("UPDATE users SET email='jDoe@example.com' WHERE username='jDoe'", $error);
if (!$query) {
exit("Errore nella query: '$error'");
} else {
echo 'Numero di righe modificate: ', $dbhandle->changes();
}
?>
Vedere anche:
- sqlite_query() - Esegue una query su un database e restituisce un puntatore al set di risultati
- sqlite_unbuffered_query() - Esegue una query senza scaricare e bufferizzare i dati
- sqlite_array_query() - Esegue una query in un dato database e restituisce una matrice
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.
