Is it possible to get a row with all column names of a table like this?
|id|foo|bar|age|street|address|
I don't like to use Pragma table_info(bla).
Is it possible to get a row with all column names of a table like this?
|id|foo|bar|age|street|address|
I don't like to use Pragma table_info(bla).
SELECT sql FROM sqlite_master
WHERE tbl_name = 'table_name' AND type = 'table'
Then parse this value with Reg Exp (it's easy) which could looks similar to this: [(.*?)]
Alternatively you can use:
PRAGMA table_info(table_name)
Then parse this value with Reg Exp (it's easy)... is really vague, a full example of a query with the regex to get the column defs would have gone a long way here."([^"]+)" (?!\() seems to work.If you are using the command line shell to SQLite then .headers on before you perform your query. You only need to do this once in a given session.
.mode column and .headers on for those who want to get the typical SELECT output format they're used to seeing with other SQL shells.Yes, you can achieve this by using the following commands:
sqlite> .headers on
sqlite> .mode column
The result of a select on your table will then look like:
id foo bar age street address
---------- ---------- ---------- ---------- ---------- ----------
1 val1 val2 val3 val4 val5
2 val6 val7 val8 val9 val10
This helps for HTML5 SQLite:
tx.executeSql('SELECT name, sql FROM sqlite_master WHERE type="table" AND name = "your_table_name";', [], function (tx, results) {
var columnParts = results.rows.item(0).sql.replace(/^[^\(]+\(([^\)]+)\)/g, '$1').split(','); ///// RegEx
var columnNames = [];
for(i in columnParts) {
if(typeof columnParts[i] === 'string')
columnNames.push(columnParts[i].split(" ")[0]);
}
console.log(columnNames);
///// Your code which uses the columnNames;
});
You can reuse the regex in your language to get the column names.
Shorter Alternative:
tx.executeSql('SELECT name, sql FROM sqlite_master WHERE type="table" AND name = "your_table_name";', [], function (tx, results) {
var columnNames = results.rows.item(0).sql.replace(/^[^\(]+\(([^\)]+)\)/g, '$1').replace(/ [^,]+/g, '').split(',');
console.log(columnNames);
///// Your code which uses the columnNames;
});
sql field of the SQLITE_MASTER table. since this field contains literal query text used to create the table, this is not always true. I'm also thinking it won't correctly handle field names that contain spaces.Use a recursive query. Given
create table t (a int, b int, c int);
Run:
with recursive
a (cid, name) as (select cid, name from pragma_table_info('t')),
b (cid, name) as (
select cid, '|' || name || '|' from a where cid = 0
union all
select a.cid, b.name || a.name || '|' from a join b on a.cid = b.cid + 1
)
select name
from b
order by cid desc
limit 1;
Alternatively, just use group_concat:
select '|' || group_concat(name, '|') || '|' from pragma_table_info('t')
Both yield:
|a|b|c|
The result set of a query in PHP offers a couple of functions allowing just that:
numCols()
columnName(int $column_number )
Example
$db = new SQLIte3('mysqlite.db');
$table = 'mytable';
$tableCol = getColName($db, $table);
for ($i=0; $i<count($tableCol); $i++){
echo "Column $i = ".$tableCol[$i]."\n";
}
function getColName($db, $table){
$qry = "SELECT * FROM $table LIMIT 1";
$result = $db->query($qry);
$nCols = $result->numCols();
for ($i = 0; $i < $ncols; $i++) {
$colName[$i] = $result->columnName($i);
}
return $colName;
}
Easiest way to get the column names of the most recently executed SELECT is to use the cursor's description property. A Python example:
print_me = "("
for description in cursor.description:
print_me += description[0] + ", "
print(print_me[0:-2] + ')')
# Example output: (inp, output, reason, cond_cnt, loop_likely)
Using @Tarkus's answer, here are the regexes I used in R:
getColNames <- function(conn, tableName) {
x <- dbGetQuery( conn, paste0("SELECT sql FROM sqlite_master WHERE tbl_name = '",tableName,"' AND type = 'table'") )[1,1]
x <- str_split(x,"\\n")[[1]][-1]
x <- sub("[()]","",x)
res <- gsub( '"',"",str_extract( x[1], '".+"' ) )
x <- x[-1]
x <- x[-length(x)]
res <- c( res, gsub( "\\t", "", str_extract( x, "\\t[0-9a-zA-Z_]+" ) ) )
res
}
Code is somewhat sloppy, but it appears to work.
sqlite_master for the name of the tables and pragma_table_info for the description of all the tables gives the complete structure of ALL the tables in a single query. SELECT sm.tname AS tablename, p.* FROM sqlite_master sm, pragma_table_info(sm.name) p
WHERE sm.type = 'table' -- just selecting the tables in sqlite_master
ORDER BY sm.name, p.cid; -- cid in pragma_table_info
WITH firstloop AS (SELECT sm.name AS tablename, p.*
FROM sqlite_master sm, pragma_table_info(sm.name) p
WHERE sm.type = 'table'
ORDER BY sm.name, p.cid)
SELECT tablename,
'|' || GROUP_CONCAT(name, '|') || '|' AS aggregatedlist FROM firstloop
GROUP BY tablename;
Hopes this helps.
If we assume you are on Unix you can use
sqlite3 -header your_database.sqlite "SELECT * FROM your_table LIMIT 1;" | head -n1
This prints the header and 1 row to stdout then we just select the first line.
The default output is list and the default separator is |, which gives the desired output. Both can be configured with command line arguments: sqlite3 --help
If you do require both leading and trailing |, then you can do
sqlite3 -header your_database.sqlite "SELECT * FROM your_table LIMIT 1;" | head -n1 | sed "s/^/|/;s/$/|/"
which does the two substitutions with sed.