PHP code example of phenixphp / sqlite

1. Go to this page and download the library: Download phenixphp/sqlite library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phenixphp / sqlite example snippets




use Phenix\Sqlite\SqliteConfig;

$config = new SqliteConfig(
    database: __DIR__ . '/database.db',
);

$config = new SqliteConfig(
    database: ':memory:',
);



declare(strict_types=1);

use Phenix\Sqlite\SqliteConfig;

use function Phenix\Sqlite\connect;

$config = new SqliteConfig(
    database: __DIR__ . '/database.db',
);

$connection = connect($config);

// ... use connection ...

$connection->close();

$connection->execute(
    'CREATE TABLE IF NOT EXISTS users (
        id   INTEGER PRIMARY KEY AUTOINCREMENT,
        name TEXT NOT NULL,
        email TEXT NOT NULL
    )'
);

$connection->execute(
    "INSERT INTO users (name, email) VALUES ('John Doe', '[email protected]')"
);

$result = $connection->query('SELECT * FROM users');

foreach ($result as $row) {
    echo $row['id'] . ': ' . $row['name'] . ' — ' . $row['email'] . PHP_EOL;
}

$connection->execute(
    "UPDATE users SET name = 'Jane Doe' WHERE id = 1"
);

$connection->execute(
    "DELETE FROM users WHERE id = 1"
);

$statement = $connection->prepare(
    'INSERT INTO users (name, email) VALUES (?, ?)'
);

$statement->execute(['Alice', '[email protected]']);
$statement->execute(['Bob',   '[email protected]']);

$statement = $connection->prepare(
    'SELECT * FROM users WHERE name = ? AND email = ?'
);

$result = $statement->execute(['Alice', '[email protected]']);

foreach ($result as $row) {
    echo $row['name'] . PHP_EOL;
}

$statement->close();

$transaction = $connection->beginTransaction();

try {
    $transaction->execute(
        "INSERT INTO users (name, email) VALUES ('Charlie', '[email protected]')"
    );
    $transaction->execute(
        "INSERT INTO users (name, email) VALUES ('Diana', '[email protected]')"
    );

    $transaction->commit();
} catch (\Throwable $e) {
    $transaction->rollback();

    throw $e;
}

$transaction = $connection->beginTransaction();

try {
    $statement = $transaction->prepare(
        'INSERT INTO users (name, email) VALUES (?, ?)'
    );

    $statement->execute(['Eve',   '[email protected]']);
    $statement->execute(['Frank', '[email protected]']);

    $transaction->commit();
} catch (\Throwable $e) {
    $transaction->rollback();

    throw $e;
}