Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web SQL get column list from table

I try get column list from Web sql (Chrome local database). Оne of the decisions - get info from sqlite_master

SELECT name, sql FROM sqlite_master WHERE type="table" AND name = "'+name+'";

For example i get this result

CREATE TABLE table_name ( id INTEGER PRIMARY KEY AUTOINCREMENT, 
number INTEGER unique, description TEXT, password TEXT, url TEXT ) 

So help me write regex for get column names, or, maybe, show me another simple way get column names.

PS. i dont wont to do select * from table... for getting columns names. I think this is bad solution..

like image 322
MikeLP Avatar asked Mar 05 '13 10:03

MikeLP


People also ask

How do I get a list of all columns in a table?

We will be using sys. columns to get the column names in a table. It is a system table and used for maintaining column information.

How do I get a list of column names in SQL Server?

Column search A feature that can be used to search for column names in SQL Server is Object search. This feature allows users to find all SQL objects containing the specified phrase.

How do I SELECT only column names in SQL?

To get the column name of a table we use sp_help with the name of the object or table name. sp_columns returns all the column names of the object. The following query will return the table's column names: sp_columns @table_name = 'News'


1 Answers

To get the columns of a table, execute PRAGMA table_info(table_name):

PRAGMA table_info()

Return a single row for each column of the named table. The columns of the returned data set are:

  • cid: Column id (numbered from left to right, starting at 0)
  • name: Column name
  • type: Column declaration type.
  • notnull: True if 'NOT NULL' is part of column declaration
  • dflt_value: The default value for the column, if any.

Unfortunately, Chrome blocks all PRAGMAs, so this doesn't work in WebSQL.


In WebSQL, you can access only tables that were created by your app, so you should just remember which columns your tables have.


Alternatively, you can just try to read from the table:

SELECT * FROM table_name LIMIT 1

With the LIMIT clause, this will be very efficient because you read only some random record. (Except if you have some very big blob in that record.)

like image 102
CL. Avatar answered Sep 19 '22 10:09

CL.