Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use sqlite database with xampp using php

Tags:

php

sqlite

How to install sqlite or sqlite 3 database with xampp server and how to use sqlite or sqlite 3 database using php scripts or programs...

like image 424
uday Avatar asked Aug 05 '11 05:08

uday


People also ask

Can I use SQLite with phpMyAdmin?

phpLiteAdmin is for SQLite what phpMyAdmin is for MySQLphpLiteAdmin is a web-based SQLite database admin tool written in PHP with support for SQLite3 and SQLite2. Following in the spirit of the flat-file system used by SQLite, phpLiteAdmin consists of a single source file, phpliteadmin.

Does PHP support SQLite?

SQLite can be used with PHP scripts through the SQLite PHP functions. You can check the SQLite and PDO_SQLite library versions using the phpinfo function.

How do I connect to a SQLite database?

Select SQLite from the list. Give a Connection name for your own internal reference. For Database , click Choose a File and then select the database file on your local machine to which you want to connect. Hit Connect and you're all set!


2 Answers

To activate SQLite3 in Xampp (v3.2.2). Open xampp/php/php.ini, un-comment the line ;extension=sqlite3 (retrieve the ;), save the php.ini and restart your Xampp server.

like image 173
zeuf Avatar answered Sep 21 '22 00:09

zeuf


Creating the sqlite Database

You have two possibilities to achieve that:

  1. Writing your DDL-statements (CREATE TABLE...) in a .sql file and execute it using the sqlite command line (assuming your CREATE TABLE statements are in a file called tabledef.sql):

    cat tabledef.sql | sqlite3 yourdbname.db
    
  2. Use PHP to execute the DDL-statements on a connected database (see "Executing statements").

Connecting to a sqlite Database

You should definitely use PDO to do that:

$dbh = new PDO('sqlite:/path/to/your/database.db', '', '', array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
));

If the database does not exist, then it is created. But you need write-access to the directory that contains the database. If you permit write-access only to the database file, sqlite fails because it also needs to create a lock file in the same directory.

Executing statements

Now that you have a connection you can do your stuff with the database, for example execute some DDL-statements:

$dbh->exec("CREATE TABLE IF NOT EXISTS mytable (
    mypk INTEGER PRIMARY KEY AUTOINCREMENT,
    myvalue TEXT);");

Or if you need to dynamically generate SQL statements, use prepared statements:

$statement = $dbh->prepare("INSERT INTO mytable (myvalue) VALUES(?)");

$statement->execute(array("hello"));
$statement->execute(array("world"));

This is only a small overview, for further information you should check out the PDO manual and the sqlite Documentation.

like image 41
vstm Avatar answered Sep 20 '22 00:09

vstm