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
(No version information available, might be only in CVS)
SQLiteDatabase->exec — Ejecuta una consulta que no produce resultado
Descripción
Método que sigue el estilo orientado a objetos:
Ejecuta la sentencia SQL indicada por el parámetro consulta en la base de datos identificada por el parámetro manejador_bd .
SQLite permite ejecutar múltiples consultas seguidas separadas por un punto y coma. De esta forma, se pueden ejecutar de una vez una serie de consultas SQL que se han cargado por ejemplo de un archivo o que se han incluido en un script.
Lista de parámetros
- consulta
-
La consulta que se quiere ejecutar.
- manejador_bd
-
El recurso que identifica la base de datos SQLite (y que es el que devuelve la función sqlite_open()). Este parámetro no se requiere cuando se emplea el método orientado a objetos.
Note: Two alternative syntaxes are supported for compatibility with other database extensions (such as MySQL). The preferred form is the first, where the dbhandle parameter is the first parameter to the function.
Valores retornados
La función devuelve un resultado booleano: TRUE si tiene éxito y FALSE en caso contrario. Si se requiere que la consulta ejecutada devuelva las filas en el resultado, se debe emplear la función sqlite_query().
The column names returned by SQLITE_ASSOC and SQLITE_BOTH will be case-folded according to the value of the sqlite.assoc_case configuration option.
Ejemplos
Example #1 Ejemplo no orientado a objetos
<?php
$manejador_bd = sqlite_open('mysqlitedb');
$consulta = sqlite_exec($manejador_bd, "UPDATE usuarios SET email='pedroperez@example.com' WHERE nombre_usuario='pedroperez'");
if (!$consulta) {
exit('Error en la consulta.');
} else {
echo 'Numero de filas modificadas: ', sqlite_changes($manejador_bd);
}
?>
Example #2 Ejemplo orientado a objetos
<?php
$manejador_bd = new SQLiteDatabase('mysqlitedb');
$consulta = $dbhandle->exec("UPDATE usuarios SET email='pedroperez@example.com' WHERE nombre_usuario='pedroperez'");
if (!$consulta) {
exit('Error en la consulta.');
} else {
echo 'Numero de filas modificadas: ', $manejador_bd->changes();
}
?>
sqlite_exec
17-Apr-2008 09:01
24-Jun-2007 01:33
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.
