How do i put a check for the database existed in SQLITE3 usin PHP

i have created test.db using php
how can i check if test.db exist when i run second time and display a message
vishnu kalakotaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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

Please see this 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


I want to say thank to the expert Ray Paseur also for this  answer
Ray PaseurCommented:
SQLite3 data bases are just files in the server file system.  I believe you can use file_exists() or similar to detect.
Ray PaseurCommented:
Please see http://iconoun.com/demo/temp_vishnu_k.php
<?php // demo/temp_vishnu_k.php
/**
 * http://www.experts-exchange.com/questions/28690764/How-do-i-put-a-check-for-the-database-existed-in-SQLITE3-usin-PHP.html
 */
error_reporting(E_ALL);
echo '<pre>';


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

// DOES THE FILE EXIST
if (!file_exists($db_name)) echo PHP_EOL . "THE FILE <b>$db_name</b> DOES NOT EXIST YET";


// 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);
}


// DOES THE FILE EXIST
if (file_exists($db_name)) echo PHP_EOL . "THE FILE <b>$db_name</b> EXISTS NOW, AFTER <i>SQLite3</i> INSTANTIATION";


// REMOVE THE DATABASE
unlink($db_name);


// DOES THE FILE EXIST
if (file_exists($db_name))
{
    echo PHP_EOL . "THE FILE <b>$db_name</b> EXISTS";
}
else
{
    echo PHP_EOL . "THE FILE <b>$db_name</b> NO LONGER EXISTS AFTER <i>unlink()</i>";
}

Open in new window

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:
Thank you very much.. working fine
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.