how to create database in sqlite3 using php?

how can i creat database in sqlite3 using  php
vishnu kalakotaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GeNeRaL971Commented:
Hi vishnu kalakota

You can  creat a  database in sqlite3 using  php with this:


public void SQLite3::open ( string $filename [, int $flags = SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE [, string $encryption_key ]] )
0
vishnu kalakotaAuthor Commented:
any examples please
0
GeNeRaL971Commented:
hum like this ??? :

script for SQLite 3 (PHP 5.4):

<?php
$dbname='base';
if(!class_exists('SQLite3'))
  die("SQLite 3 NOT supported.");

$base=new SQLite3($dbname, 0666);
echo "SQLite 3 supported.";
?>

This code creates a data base named base.
 If the extension(extension=php_sqlite3.dll) is not available, the variable base will cost false.
If it works, a file bases will appear in the directory of the script.
0
Ray PaseurCommented:
public void SQLite3::open ( string $filename [, int $flags = SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE [, string $encryption_key ]] )
That should be expected to cause a PHP parse error.

Please see my teaching example for SQLite3 here:  http://iconoun.com/demo/sqlite3_example.php

Note that the SQLite3 functions are not very good about exceptions, but they issue Warning messages when they fail, making them much better than the MySQL "silent failure" approach to errors.

<?php // demo/sqlite3_example.php
/**
 * Use SQLite3
 *
 * Why? http://www.sqlite.org/mostdeployed.html
 *
 * http://www.sqlite.org/about.html
 * http://php.net/manual/en/book.sqlite3.php
 * http://php.net/manual/en/sqlite3stmt.bindvalue.php#116523
 */
error_reporting(E_ALL);
echo '<pre>';


// THE FILE PATH AND NAME OF THE DATABASE
$db_name = 'SqLite.db';

// SET UP OUR DATABASE USING SQLITE3
try
{
    $sql3_db = new SQLite3($db_name);
}
// NOTE - THE CONSTRUCTOR MAY THROW AN EXCEPTION
catch(Exception $exc)
{
    var_dump($exc);
    trigger_error('SQLite3 Fail: ' . $exc->getMessage(), E_USER_ERROR);
}


// A QUERY TO CREATE A TABLE
$sql
=
"
CREATE TABLE stock
( symbol TEXT
, price  REAL
, xtime  TEXT
, key    INTEGER PRIMARY KEY AUTOINCREMENT
)
"
;

if (!$sql3_dbs = $sql3_db->query($sql))
{
    trigger_error('SQLite3 Failure', E_USER_ERROR);
}


// A QUERY TO LOAD THE TABLE
$sql
=
"
INSERT INTO stock
(  symbol,  price ) VALUES
( :symbol, :price )
"
;

if (!$sql3_stmt = $sql3_db->prepare($sql))
{
    trigger_error('SQLite3 Failure', E_USER_ERROR);
}


// SOME TEST DATA FOR OUR STOCK TABLE
$names = array
( array( 'symbol' => 'YHOO', 'price' =>   41.07 )
, array( 'symbol' => 'GOOG', 'price' => 1148.62 )
, array( 'symbol' => 'AAPL', 'price' =>  557.36 )
, array( 'symbol' => 'JCP',  'price' =>    6.92 )
)
;
// LOADING OUR ARRAYS OF TEST DATA INTO THE TABLE
foreach ($names as $name)
{
    $sql3_stmt->bindValue(':symbol', $name['symbol']);
    $sql3_stmt->bindValue(':price' , $name['price']);
    $sql3_stmt->execute();

    // GET THE SQLITE AUTOINCREMENT ID OF THE RECORD JUST INSERTED
    $rowid  = $sql3_db->lastInsertRowId();
    echo "SQLite3 INSERTED A ROW CONTAINING <b>" . $name['symbol'] . ' ' . $name['price']. "</b> WITH AUTO_INCREMENT ID = $rowid" . PHP_EOL;
}


// A QUERY TO RETRIEVE INFORMATION FROM THE TABLE
$sql
=
"
SELECT rowid, symbol, price FROM stock WHERE price > :value LIMIT 3
"
;
// PREPARES THE QUERY
$sql3_stmt = $sql3_db->prepare($sql);
$sql3_stmt->bindValue(':value', 500);
if (!$sql3_res = $sql3_stmt->execute())
{
    trigger_error('SQLite3 Failure', E_USER_ERROR);
}

// RETRIEVE THE RESULTS IN THE FORM OF ASSOCIATIVE ARRAY
while ($row = $sql3_res->fetchArray(SQLITE3_ASSOC))
{
    print_r($row);
}
// CLOSE THE RESULTS SET
$sql3_res->finalize();


// A QUERY TO REMOVE INFORMATION FROM THE TABLE
$sql
=
"
DELETE FROM stock WHERE price > :value
"
;
// PREPARE AND RUN THE QUERY
$sql3_stmt = $sql3_db->prepare($sql);
$sql3_stmt->bindValue(':value', 500);
$sql3_stmt->execute();

// SHOW THE NUMBER OF AFFECTED ROWS
echo "QUERY: $sql DELETED " . $sql3_db->changes() . " ROWS";
echo PHP_EOL;


// A QUERY TO SHOW THE INFORMATION LEFT IN THE TABLE
$sql
=
"
SELECT rowid, symbol, price FROM stock ORDER BY symbol
"
;
// PREPARES THE QUERY
$sql3_stmt = $sql3_db->prepare($sql);

if (!$sql3_res = $sql3_stmt->execute())
{
    trigger_error('SQLite3 Failure', E_USER_ERROR);
}


// RETRIEVE THE RESULTS IN THE FORM OF ASSOCIATIVE ARRAY
while ($row = $sql3_res->fetchArray(SQLITE3_ASSOC))
{
    print_r($row);
}
// CLOSE THE RESULTS SET
$sql3_res->finalize();


// A QUERY-PREP THAT WILL FAIL TO DELIBERATELY CAUSE A WARNING
$sql3_stmt = $sql3_db->prepare("SELECT oopsie FROM stock ORDER BY symbol");


// REMOVE THE DATABASE
unlink($db_name);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vishnu kalakotaAuthor Commented:
Very helpful
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.