Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count number of columns in a table in SQLITE?

Tags:

android

sqlite

How can I count the number of columns in a table in a sqlite database in Android?

like image 981
Tester Avatar asked Nov 29 '10 06:11

Tester


3 Answers

A query returns a Cursor which has methods like getColumnCount().

like image 78
Kennet Avatar answered Nov 10 '22 00:11

Kennet


You can use pragma table_info(foo_table) and count the number of rows returned

like image 44
dhaag23 Avatar answered Nov 10 '22 00:11

dhaag23


Here is the code and it runs perfectly without any error.

public Cursor getAllTitles(){
   return db.query(DATABASE_TABLE, new String[] {
        KEY_ROWID,KEY_ISBN,KEY_TITLE,KEY_PUBLISHER},
        null,null,null,null,null);
}

Now create the following object in onCreate() method to call above getAllTitles() method.

Cursor c = db.getAllTitles();
c.getColumnCount(); //this line will give the number of columns in table.
like image 29
user609239 Avatar answered Nov 10 '22 01:11

user609239