As of July 2010, there are two ways to use SQLite from PHP:
- procedural: sqlite (=sqlite2), sqlite3
- object-oriented: SQLite3, PDO
SQLite
- Introdução
- Instalação/Configuração
- Constantes pré-definidas
- SQLite Funções
- sqlite_array_query — Executa uma query ao banco de dados e retorna uma matriz
- sqlite_busy_timeout — Define o tempo de espera quando o banco de dados estiver ocupado
- sqlite_changes — Retorna o número de linhas que foram alteradas pela SQL mais recente.
- sqlite_close — Fecha um banco de dados SQLite que esteja aberto.
- sqlite_column — Retorna uma coluna da linha atual do conjunto de resultados.
- sqlite_create_aggregate — Registra uma função agregada UDF para usar em SQLs
- sqlite_create_function — Registra uma Função Definida pelo Usuário (UDF) para usar em SQL.
- sqlite_current — Retorna a linha atual do resultado como uma matriz.
- sqlite_error_string — Retorna a descrição textual de um código de erro
- sqlite_escape_string — Escapa uma string para ser usada como um parâmetro em uma query
- sqlite_exec — Executes a result-less query against a given database
- sqlite_factory — Opens an SQLite database and returns an SQLiteDatabase object
- sqlite_fetch_all — Fetches all rows from a result set as an array of arrays
- sqlite_fetch_array — Obtém a próxima linha de um resultado como uma matriz.
- sqlite_fetch_column_types — Return an array of column types from a particular table
- sqlite_fetch_object — Fetches the next row from a result set as an object
- sqlite_fetch_single — Retorna o resultado da primeira coluna como string.
- sqlite_fetch_string — Sinônimo de sqlite_fetch_single
- sqlite_field_name — Retorna o nome de um campo em particular.
- sqlite_has_more — Retorna quando existem ou não mais linhas disponíveis
- sqlite_has_prev — Returns whether or not a previous row is available
- sqlite_key — Returns the current row index
- sqlite_last_error — Retorna o código de erro para o último erro em um banco de dados
- sqlite_last_insert_rowid — Retorna o rowid da ultima linha inserida.
- sqlite_libencoding — Retorna a codificação da biblioteca SQLite
- sqlite_libversion — Retorna a versão da biblioteca SQLite
- sqlite_next — Move para a próxima linha.
- sqlite_num_fields — Retorna o número de campos no conjunto de resultados.
- sqlite_num_rows — Retorna o número de linhas em um conjunto de resultados.
- sqlite_open — Abre um banco de dados SQLite. Irá criar o banco de dados se ele não existir
- sqlite_popen — Abre uma conexão persistente com um banco de dados SQLite. Irá criar um se ele não existir.
- sqlite_prev — Seek to the previous row number of a result set
- sqlite_query — Executa uma query em um banco de dados e retorna o manipulador de resultados
- sqlite_rewind — Vai para a primeira linha.
- sqlite_seek — Move o ponteiro interno para uma linha em um conjunto de resultados
- sqlite_single_query — Executes a query and returns either an array for one single column or the value of the first row
- sqlite_udf_decode_binary — Decodifica dados binários passados como parâmetros para uma UDF
- sqlite_udf_encode_binary — Codifica dados binários para utilizar com uma UDF
- sqlite_unbuffered_query — Executa uma query que não guarda em buffer os resultados
- sqlite_valid — Returns whether more rows are available
Anonymous ¶
2 years ago
nosdudefr at gmail dot com ¶
2 years ago
Regarding the table creation you can optimize this code a bit by using the built in " CREATE TABLE IF NOT EXISTS <tablename>". This will let the table creation decision to the sqlite engine.
Then, if i may, your code could be something like :
<?php
if ($db = new SQLiteDatabase('filename')) {
// first let the engine check table, and create it eventualy
$q = @$db->query('CREATE TABLE IF NOT EXISTS tablename (id int, requests int, PRIMARY KEY (id))';
//the rest of the code, according error checks etc
// ...
?>
For more "tweaks" feel free to look at sqlite language ref : http://www.sqlite.org/lang_createtable.html
Have fun with this powerfull&simple engine :)
Andrew Paul Dickey ¶
3 years ago
If you intend to implement 2.x releases of SQLite with your development you are in the right place, as this library is suitable for use with your application (reference http://us.php.net/manual/en/book.sqlite.php).
If you intend to use SQLite 3.x releases of SQLite with your development please refer to the section on PHP Data Objects, and specifically the PDO-SQLite implementation available at(references: http://us.php.net/manual/en/book.pdo.php , http://au2.php.net/manual/en/ref.pdo-sqlite.php).
It is my hope that this post will save both new users and experienced developers time during their initial or a new implementation of PHP & SQLite by encouraging them to use the appropriate libraries.
saivert at saivert dot com ¶
5 years ago
How to open a database, create a table if it doesn't exist and inserting initial value.
<?php
if ($db = new SQLiteDatabase('filename')) {
$q = @$db->query('SELECT requests FROM tablename WHERE id = 1');
if ($q === false) {
$db->queryExec('CREATE TABLE tablename (id int, requests int, PRIMARY KEY (id)); INSERT INTO tablename VALUES (1,1)');
$hits = 1;
} else {
$result = $q->fetchSingle();
$hits = $result+1;
}
$db->queryExec("UPDATE tablename SET requests = '$hits' WHERE id = 1");
} else {
die($err);
}
?>
Use this as boilerplate code for any new project using SQLite.
