New Start
New Start

Reputation: 1421

Reading in certain lines of a text file

Yes, I have already asked this question, but the problem is much more specific. Last time I wanted to ignore the first 2 and the last line of the file. But it struck me that anyone could put as many lines as they wanted before and after the stuff I want to read in, and in that case, the code I've implemented goes to pot.

example file

USE [Shelley's Other Database]  
CREATE TABLE db.exmpcustomers(  
 fname varchar(100) NULL,  
 lname varchar(100) NULL,  
 dateofbirth date NULL,  
 housenumber int NULL,  
 streetname varchar(100) NULL  
) ON [PRIMARY]

What I need to do is work out where my data starts (being fname - but remember it could be called anything, so I can't use a regex looking for 'f'), where it ends (the last NULL), then read in that. Or, you know, read in the file and select that data; whatever.
I thought about using regex looking for the brackets, but wouldn't it pick up the brackets in my datatypes as well?

Any ideas? Anything will help, I'm totally stuck.

EDIT

Oh, and, the original file actually has spaces/tabs before the data (indentation), but I'm not sure if that changes anything.

Upvotes: 0

Views: 283

Answers (2)

chiccodoro
chiccodoro

Reputation: 14716

the regex I provided in your other question would perfectly match your data. Your question is actually really a duplicate of the other one.

Upvotes: 0

Jeriko
Jeriko

Reputation: 6637

I would just regex greedy-match data between the brackets. As long as no ) follows ON, and no ( precedes CREATE, the parentheses in the content don't really matter.

If you don't know SQL scripts too well, is it a good idea exposing control to other people? Why not create a simple DSL that allows the same benefits? e.g.:

DATABASE databasenamegoeshere
TABLE tablenamegoeshere
FIELD name type default
FIELD name type default
FIELD name type default

And then translate that into corresponding SQL code? Seems a bit more secure.. Or does it have to be executable SQL?

Upvotes: 2

Related Questions