Phpworker
answered Apr 24 '23 00:00
To retrieve a list of columns for a table in SQLite using a query, you can use the PRAGMA statement with the table_info argument. This can be done in any programming language that supports SQLite.
Here's an example using PHP and the PDO extension:
// Establish a connection to the SQLite database
$db = new PDO('sqlite:example.db');
// Prepare the PRAGMA statement to retrieve table information
$statement = $db->prepare('PRAGMA table_info(table_name)');
// Execute the statement
$statement->execute();
// Retrieve the table information as an array of associative arrays
$table_info = $statement->fetchAll(PDO::FETCH_ASSOC);
// Iterate through the table information to extract the column names
foreach ($table_info as $column_info) {
$column_names[] = $column_info['name'];
}
// Print the column names
print_r($column_names);
In the above example, replace example .db with the name of the SQLite database file you're using, and replace table_name with the name of the table you want to retrieve the columns for.
The table_info result is fetched as an array of associative arrays, where each array represents a column in the table. The name of the column is extracted from each associative array and stored in a new array called $column_names . Finally, the column names are printed using print_r().