1

I have a requirement to print tablename and primary key using the below program. But it is not working. Could you please help in solving this. I have used the regex pattern to find out table name and PRIMARY KEY data in the database Schema.

class ParseCreateFile:

    begin = re.compile(r"CREATE \s+ TABLE \s+'((?:[^']|'')*)'.+(PRIMARY \s+ KEY\s? \([^)]*\))", re.IGNORECASE | re.VERBOSE | re.DOTALL)

    def __init__(self,fileName):
        self.fileName = fileName
        self.readFromFile()

    def readFromFile(self):
        try:
            file = open(self.fileName,'r')
            fileData = file.read()
            file.close()
            self.parse(fileData)
        except IOError:
            print 'Unable to read file %s' % self.fileName

    def parse(self,rawData):
        self.list = []
        while rawData:
          mo = self.begin.search(rawData)
          tablename, fil = mo.groups()
          print tablename, fil
          if mo is None:
            break
          rawData = rawData[mo.end():]

    if __name__ == '__main__':
        print "only the main classes"
        file = ParseCreateFile('file2.sql')

file2.sql

CREATE TABLE 'x'(
  'X' integer
  PRIMARY KEY(dn)
  'Y' short);
CREATE TABLE 'y'(
  'X1' integer
  PRIMARY KEY(dn1, dn2)
  'Y1' short);

Expected Out:
x: PRIMARY KEY(dn)
y: PRIMARY KEY(dn1, dn2)
2

1 Answer 1

1

I edited the regex, and used the findall.

class ParseCreateFile:

    begin = re.compile(r"CREATE TABLE[ ']*([^ ']+)[ ']*[(][^/;]+(PRIMARY KEY.*)[^/;]+;", re.IGNORECASE)

    def __init__(self,fileName):
        self.fileName = fileName
        self.readFromFile()

    def readFromFile(self):
        try:
            file = open(self.fileName,'r')
            fileData = file.read()
            file.close()
            self.parse(fileData)
        except IOError:
            print 'Unable to read file %s' % self.fileName

    def parse(self,rawData):
        self.list = []
        found = self.begin.findall(rawData)
        for i in found:
            print i[0], i[1]


if __name__ == '__main__':
    print "only the main classes"
    file = ParseCreateFile('file2.sql')
Sign up to request clarification or add additional context in comments.

6 Comments

With the above program, the output is shown as "only the main classes". It is not working. The desired output is not shown.
Removed re.VERBOSE | re.DOTALL
Thanks for your answer
Hi Akram, if i have tables like this CREATE TABLE 'x'( 'X' integer PRIMARY KEY(dn)); CREATE TABLE 'y'( 'X1' integer PRIMARY KEY(dn1, dn2)); The above logic doesn't work
Hopefully this will work for everything you had till now, you need to fix the regex. re.compile(r"CREATE TABLE[ ']*([^ ']+)[ ']*[(][^/;]+(PRIMARY KEY.*?\))[^/;]+;", re.IGNORECASE)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.