PHP uses PDO to connect Sqlite3 configuration example, pdosqlite3_php tutorial

Source: Internet
Author: User
Tags dsn getting started with php sqlite format 3

PHP uses PDO to connect the Sqlite3 configuration example, Pdosqlite3


This example describes how PHP uses PDO to connect sqlite3. Share to everyone for your reference, as follows:

Just started using Php+sqlite, always think that they use sqlite3, actually not, PHP php5 >=5.3.0 from the time to start the default support Sqlite3

Refer to Official Document http://www.php.net/manual/zh/sqlite3.open.php

The default method interface:

public void Sqlite3::open (string $filename [, int $flags = Sqlite3_open_readwrite | Sqlite3_open_create [, String $encryption _key]])

Using PHP to operate the database found that PHP is only supported by default to Sqlite2, the latest version of the Sqlite3 is not supported. If you want to support Sqlite3 you will use PDO. To use PDO, you need to load the Php_pdo.dll and Php_pdo_sqlite.dll two modules into the php.ini. As follows:

Extension=php_pdo.dllextension=php_pdo_sqlite.dll

If you do not use PDO, even if you open the above parameters, in fact, still use sqlite2, do not believe you access to see the generated database at the beginning of the file is not a hint:

* * This file contains an SQLite 2.1 database * *

The following error is reported when the PHP environment does not open the configuration supported above:

Fatal error:call to undefined function sqlite_open ()

sqlite3 Example:

<?php//$dsn = ' sqlite:sql.db '; try{//$dbh = new PDO ($DSN, $user, $password); Establish connection//$DBH = new PDO (' sqlite:yourdatabase.db '); $dbh = new PDO (' sqlite:itlife365.com '); Echo ' Create db OK ';//Build Table $DBH-&G T;exec ("CREATE TABLE itlife365 (ID integer,name varchar (255))"), echo ' CREATE table itlife365 OK
'; $dbh->exec ("INSERT into itlife365 values (1, ' itlife365.com ')"); Echo ' Insert Data OK
'; $dbh->begintransaction (); $sth = $dbh->prepare (' SELECT * from itlife365 '); $sth->execute ();//Get result $result = $ Sth->fetchall ();p rint_r ($result); $dsn =null;} catch (Pdoexception $e) {echo ' Connection failed: '. $e->getmessage (); $dsn = null;}? ><?php $DBH = null;//or using unset ($DBH);?>

Verify: View database:

In the file header display:

SQLite format 3***

For more information, refer to the official website: http://cn.php.net/manual/zh/ref.pdo-sqlite.php

For more information about PHP related content readers can view the topic: "PHP based on PDO Operation Database Skills Summary", "Php+oracle Database Programming Skills Summary", "PHP+MONGODB Database operation Skills Daquan", "PHP object-oriented Programming introduction Tutorial", PHP String Usage Summary, "Getting Started with Php+mysql database operations" and "PHP Common Database Operations Skills Summary"

I hope this article is helpful to you in PHP programming.

http://www.bkjia.com/PHPjc/1133063.html www.bkjia.com true http://www.bkjia.com/PHPjc/1133063.html techarticle PHP uses the PDO connection Sqlite3 configuration example, Pdosqlite3 This example describes how PHP uses the PDO connection Sqlite3 configuration method. Share to everyone for your reference, specifically as follows: Just started to make ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.