Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
By overriding all the query methods of the SQLiteDatabase class we ensure that any failed query throws an exception. This done, we needn't worry about error trapping whenever we perform a query. The remaining methods of our derived class are utility methods aimed at helping verify data posted from a form. These methods give us an opportunity to explore some of the ways to retrieve metadata from an SQLite database. Find those methods in Listing 15-8.
|
Code View:
Scroll
/
Show All
/**
Get all table names in database
*/
public function getTableNames(){
if (!isset($this->tablenames)){
$this->setTablenames();
}
return $this->tablenames;
}
/////////////////////////////////////////////////////////////
/**
Retrieve field names/types for specified table
*/
public function getFields($tablename){
if (!isset($this->tablenames)){
$this->setTablenames();
}
if (!in_array($tablename, $this->tablenames)){
throw new SQLiteException("Table $tablename not in database.");
}
$fieldnames = array();
$sql = "PRAGMA table_info('$tablename')";
$result = $this->unbufferedQuery($sql);
//no error - bad pragma ignored
//get name and data type as defined upon creation
foreach ($result as $row){
$fieldnames[$row['name']] = $row['type'];
}
return $fieldnames;
}
//////////////////////////////////////////////////////////////
//private methods
/**
private method - initializes table names array
*/
private function ❶setTableNames(){
$sql = "SELECT name ".
"FROM sqlite_master ".
"WHERE type = 'table' ".
"OR type = 'view'";
$result = $this->unbufferedQuery($sql);
foreach ($result as $row){
$this->tablenames[] = $row['name'];
}
}
|