PHP SQLite delete table

SQLite

We will create the database and connection object.
$my_conn = new PDO('sqlite:my_student.sqlite3');
We will use $my_conn to execute our query.

By using drop query to delete table.

Deleting table

Deleting table
$my_conn = new PDO('sqlite:my_student.sqlite3');

// Set errormode to exceptions
$my_conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

 $sql=$my_conn->prepare("DROP TABLE  student");

if($sql->execute()){
echo " <br><br>Table deleted. ";
}else{
print_r($sql->errorInfo()); 
}
$my_conn = null;
Output is here
Table deleted.
We will check the table if exists or not by using sqlite_master before deleting it. The first line is the query for checking sqlite_master with type=table and name=table_name by using WHERE condition.

If the table is available then we will delete it.
$q="SELECT name FROM sqlite_master WHERE type='table' AND name='student'";
if($result=$my_conn->query($q)){
$row = $result->fetch(PDO::FETCH_OBJ);
if($row->name=='student'){
if(strlen($todo)==3 and $todo=='yes'){	
 $sql=$my_conn->prepare("DROP TABLE  student");

if($sql->execute()){
echo " <br><br>Table deleted. <br> ";
}else{
print_r($sql->errorInfo()); 
}
$my_conn = null;
   
} // end of checking todo
}else{ // end of if else if student table exits 
echo " <br><br>student table is not available<br> "; 
}	
}
Download sample script for SQLite with instructions on how to use.

Check PHP pdo support for SQLite using phpinfo()

PHP SQLite PDO Functions


plus2net.com







Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer