Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL command to extract schema of a SQLite table?

Tags:

sql

sqlite

I can't decide if this is a dumb question or not but I'll take the risk. :)

I'm a fairly simple SQL user - I understand CREATE/DROP tables and SELECT, INSERT, DELETE in their basic forms but not much more. What I'd like to know is if there's a way with SQL to request the schema of a table?

I have a PC app which uses SQLite and I'm writing a mobile client for it. I don't need all of the tables from the PC DB and not all columns from the tables. In SQLite Spy, I can right-click a table and select 'Show CREATE statement' which generates the SQL needed to recreate the table.

What I'd like to do (using code on the PC) to get the CREATE statement (or at least the schema) of the SQLite tables I need and then edit the strings to include only the columns I need. These could then be passed to my mobile app so it could create the 'subset' DB on the device. Is it possible?

EDIT: For anyone who comes after me looking for a similar answer. Thanks to dan04 pointing me in the right direction. The results of using...

SELECT * FROM sqlite_master

... include a column called 'sql' which includes the CREATE statement. Just what I'm looking for. :)

like image 395
Squonk Avatar asked Jan 18 '11 23:01

Squonk


People also ask

How do I find the schema of a table in SQLite?

If you are running the sqlite3 command-line access program you can type ". tables" to get a list of all tables. Or you can type ". schema" to see the complete database schema including all tables and indices.

Does SQLite have schema?

Every SQLite database contains a single "schema table" that stores the schema for that database. The schema for a database is a description of all of the other tables, indexes, triggers, and views that are contained within the database.


1 Answers

It's stored in the sqlite_master table.

like image 104
dan04 Avatar answered Sep 21 '22 00:09

dan04