Use Javascript RegEx to extract column names from SQLite Create Table SQL

Posted by NimbusSoftware on Stack Overflow See other posts from Stack Overflow or by NimbusSoftware
Published on 2010-05-07T01:52:29Z Indexed on 2010/05/07 1:58 UTC
Read the original article Hit count: 276

Filed under:
|
|

I'm trying to extract column names from a SQLite result set from sqlite_master's sql column. I get hosed up in the regular expressions in the match() and split() functions.

t1.executeSql('SELECT name, sql FROM sqlite_master WHERE type="table" and name!="__WebKitDatabaseInfoTable__";', [],
  function(t1, result) {
   for(i = 0;i < result.rows.length; i++){
     var tbl = result.rows.item(i).name;
     var dbSchema = result.rows.item(i).sql;
// errors out on next line
     var columns = dbSchema.match(/.*CREATE\s+TABLE\s+(\S+)\s+\((.*)\).*/)[2].split(/\s+[^,]+,?\s*/);
   }
  },
  function(){console.log('err1');}
);

I want to parse SQL statements like these...

CREATE TABLE sqlite_sequence(name,seq);
CREATE TABLE tblConfig (Key TEXT NOT NULL,Value TEXT NOT NULL);
CREATE TABLE tblIcon (IconID INTEGER NOT NULL PRIMARY KEY,png TEXT NOT NULL,img32 TEXT NOT NULL,img64 TEXT NOT NULL,Version TEXT NOT NULL)

into a strings like theses...

name,seq
Key,Value
IconID,png,img32,img64,Version

Any help with a RegEx would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about regex