Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) > Code Samples & Tips
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Code Samples & Tips Share your recent discoveries and ideas with other users.

How to retrieve the column names from the table?

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-30-2009, 02:49 PM
Newbie
 
Join Date: Mar 2008
Posts: 5
Default How to retrieve the column names from the table?

Hi

I searched the forum but could not find the answer:
how to get the column names from the SQLite table.
The only information I found in the web was that the structure of the table is returned with PRAGMA TABLE_INFO() command. But it seems that PRAGMA returns different object than the standard SELECT.
Anyone knows how to do that?
Reply With Quote
  #2 (permalink)  
Old 06-30-2009, 04:06 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 5,953
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

Others know better than I but if you query the master table

"select * from sqlite_master"

It should return the SQL statement used to create each table in the database from which you can see the column names.
__________________
Sorry, but I don't answer questions by PM or email.
Please post your queries in the forum.
Reply With Quote
  #3 (permalink)  
Old 06-30-2009, 10:03 PM
Basic4ppc Veteran
 
Join Date: Dec 2008
Posts: 203
Awards Showcase
Beta Tester 
Total Awards: 1
Default

You have to run a query like this:

select
sql
from
sqlite_master
where
name = 'table1'

and then parse out the fields from the obtained create table sql.


RBS
Reply With Quote
  #4 (permalink)  
Old 06-30-2009, 11:23 PM
Basic4ppc Veteran
 
Join Date: Dec 2008
Posts: 203
Awards Showcase
Beta Tester 
Total Awards: 1
Default

Actually, it is simpler to use a pragma to get the table fields:

PRAGMA table_info ('Table1')

As it will save you parsing out the fields from the create sql.
It will give you the table meta-date like this:

cid name type notnull dflt_value pk
------------------------------------------------
0 FIELD1 INTEGER 0 1
1 FIELD2 TEXT 0 0
2 FIELD3 TEXT 0 0
3 FIELD4 REAL 0 0


RBS
Reply With Quote
  #5 (permalink)  
Old 06-30-2009, 11:24 PM
Basic4ppc Veteran
 
Join Date: Dec 2008
Posts: 203
Awards Showcase
Beta Tester 
Total Awards: 1
Default

Quote:
table meta-date
To avoid any confusion, this should be:
table meta-data


RBS
Reply With Quote
  #6 (permalink)  
Old 06-30-2009, 11:54 PM
Basic4ppc Veteran
 
Join Date: Dec 2008
Posts: 203
Awards Showcase
Beta Tester 
Total Awards: 1
Default

B4P code would be something like this, where Hashtable1 is the new
hashtable control:

Sub GetTableFieldsAndDataTypes(strTable)

cmd.CommandText = "PRAGMA table_info ('" & strTable & "')"
Reader.Value = cmd.ExecuteReader
Hashtable1.Clear

Do While Reader.ReadNextRow = True
'field name to Key and field data type to Value
'----------------------------------------------
Hashtable1.Add(Reader.GetValue(1), Reader.GetValue(2))
Loop

Reader.Close

End Sub


RBS
Reply With Quote
  #7 (permalink)  
Old 07-01-2009, 12:10 PM
Newbie
 
Join Date: Mar 2008
Posts: 5
Default

Thanks for the code!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Table Column word wrap jjdiaz Questions (Windows Mobile) 3 01-15-2009 08:21 PM
Retrieve table filter from the filtered table? LineCutter Questions (Windows Mobile) 1 09-08-2008 07:20 AM
Modifying the table column header mozaharul Questions (Windows Mobile) 2 05-12-2008 08:38 AM
Column names in a table pmu5757 Questions (Windows Mobile) 2 05-07-2008 03:50 PM
Display images in table column chow Questions (Windows Mobile) 1 04-23-2008 12:50 PM


All times are GMT. The time now is 03:42 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0