开发者

SQLite colum names in empy table in C

I'm trying to capture the fields in a table but I need to get that data even if the table is empty.

sqlite3_table_colu开发者_运维问答mn_metadata isn't an option (it wasnt compiled into my version of SQLite3).

Is there is a trick to obtain that info?


PRAGMA table_info(table_name);

returns a list of column names

heres a link:

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


If you're running locally you can just download the source and compile whatever options you like. But if that's not an option, there are still several ways to get at the metadata.

You can query the sqlite_master table, but the data isn't in the friendliest form possible.

select sql 
from sqlite_master 
where type='table' and name='your-table-name';

That query will return the CREATE TABLE statement. Expect formatting surprises.

If you can issue "dot" commands from the c interface, you can turn on column headers, then query any table. Here's what it looks like from the sqlite prompt.

sqlite> .headers on
sqlite> select * from file_stats;
date|file_id|num_lines

There's no dead simple way to get the data type just by having the column names. The typeof() function doesn't return anything unless there's data in the table.

I was going to include the pragma table_info() function, too, but Trevor beat me to it. I don't know whether you can issue pragma statements from the c api.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜