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 — Belirtilen veritabanı üzerinde sonuç döndürmeyen bir sorgu çalıştırır
Açıklama
$db
, string $sorgu
[, string &$hata_iletisi
] )$sorgu
, resource $db
)Nesne yönelimli kullanım
$sorgu
[, string &$hata_iletisi
] )
db ile belirtilen veritabanı üzerinde
sorgu ile belirtilen SQL deyimini çalıştırır.
SQLite, noktalı virgüllerle ayrılmış birden fazla sorguyu çalıştırabilir. Bu sayede, bir dosyadan yüklenen SQL betiklerini çalıştırabilirsiniz.
Değiştirgeler
-
db -
SQLite Veritabanı özkaynağı. Yordamsal kullanımda sqlite_open() işlevi tarafından döndürülür. Nesne yönelimli kullanımda bu değiştirgeye gerek yoktur.
-
sorgu -
Çalıştırılacak sorgu.
Sorgu içindeki verinin düzgün olarak öncelenmiş olması gerekir.
-
hata_iletisi -
Bir hata oluştuğunda hata iletisi bu değiştirgeye konur. SQL sözdizimi hataları sqlite_last_error() işlevi ile alınamadığından bu değiştirge özellikle önemlidir.
Bilginize: Diğer veritabanı eklentileri (MySQL gibi) ile uyumluluk için iki ayrı sözdizimi desteklenmektedir. Genelde tercih edilen sözdizimi
dbdeğiştirgesinin ilk değiştirge olarak kullanıldığı ilk sözdizimidir.
Dönen Değerler
Başarı durumunda TRUE, başarısızlık durumunda FALSE döner. Sorgu bir sonuç döndürmeliyse bu işlevi değil
sqlite_query() işlevini kullanın.
SQLITE_ASSOC ve
SQLITE_BOTH sabitleri kullanılarak döndürülen sütun
isimlerinin harf büyüklükleri
sqlite.assoc_case php.ini yapılandırma yönergesinin değerine uygun
olarak döndürülür.
Sürüm Bilgisi
| Sürüm: | Açıklama |
|---|---|
| 5.1.0 |
hata_iletisi değiştirgesi eklendi.
|
Örnekler
Örnek 1 - Yordamsal kullanım örneği
<?php
$db = sqlite_open('mysqlitedb');
$query = sqlite_exec($db,
"UPDATE users SET email='jDoe@example.com' WHERE username='jDoe'",
$error);
if (!$query) {
exit("Sorguda hata: '$error'");
} else {
echo 'Değişen satır sayısı: ', sqlite_changes($db);
}
?>
Örnek 2 - Nesne yönelimli kullanım örneği
<?php
$db = new SQLiteDatabase('mysqlitedb');
$query = $db->queryExec(
"UPDATE users SET email='jDoe@example.com' WHERE username='jDoe'",
$error);
if (!$query) {
exit("Sorguda hata: '$error'");
} else {
echo 'Değişen satır sayısı: ', $db->changes();
}
?>
Ayrıca Bakınız
- sqlite_query() - Belirtilen veritabanında bir sorgu çalıştırıp bir sonuç tanıtıcısı döndürür
- sqlite_unbuffered_query() - Sonuçların alınıp bir tampona konmadığı bir sorgu çalıştırır
- sqlite_array_query() - Belirtilen veritabanı üzerinde bir sorgu çalıştırıp sonucu bir dizi içinde döndürür
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.
