3

I am trying to count the columns from a sqlite db using the sqlite command line tool. To test it I created a sample db like this:

c:\>sqlite.exe mydb.sqlite "create table tbl1(one varchar(10), two smallint);"

Now lets say i don't know that the table tbl1 has 2 columns, how can I find that using a query from the command line tool?

3 Answers 3

4

Run:

pragma table_info(yourTableName)

See:

http://www.sqlite.org/pragma.html#pragma_table_info

for more details.

Sign up to request clarification or add additional context in comments.

2 Comments

But is there a way to just display a number that represents the number of columns? I need this because the command line tool will be run from an installer so I don't want to have to parse or do any extra step because it wont be so easy from the installer, i just want the columns number.
To my knowledge there is no direct way to get the column count. I would suggest one of the following: 1. run the previous query and use the number of results returned as your count. or 2. Add your own meta-data table that already has the number of columns in it. Then you can query directly from that table.
0

Here is a way I found useful under Linux. Create a bash script file columns.sh and ensure it has execute permissions and copy - paste the following code.

columns() { for table in $(echo ".tables" | sqlite3 $1); do echo "$table $(echo "PRAGMA table_info($table);" | sqlite3 $1 | wc -l)"; done ;}

Type the following command, in terminal, on the first line to return results

 $  columns <database name>
    <table1>          <# of columns>
    <table2>          <# of columns>

Note: Ensure database is not corrupted or encrypted.

source: http://www.quora.com/SQLite/How-can-I-count-the-number-of-columns-in-a-table-from-the-shell-in-SQLite

UPDATE

Here is an interesting URL for Python Script Solution

http://pagehalffull.wordpress.com/2012/11/14/python-script-to-count-tables-columns-and-rows-in-sqlite-database/

Comments

0

Change your query to:

SELECT COUNT(*) FROM pragma_table_info(tablename);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.