0

I've written a small script to strip "`" out of multi-line SQL queries, and every time the loop comes to the end of a line, it breaks. below is the code, and what im inserting:

inp = input('Enter Query: ')

for s in inp:
    if s != '`':
        print(s, end = '')

input is:

CREATE TABLE IF NOT EXISTS `mydb`.`Employees` (
  `Employees_PK` INT NOT NULL,
  `Employee_L_NAME` VARCHAR(45) NOT NULL,
  `Employee_F_NAME` VARCHAR(45) NOT NULL,
  `Employees_PHONE` VARCHAR(45) NOT NULL,
  `Employee_EMAIL` VARCHAR(45) NOT NULL,
  `Employee_HIRE_DATE` DATE NOT NULL,
  `Employee_MANAGED_BY` INT NULL,
  PRIMARY KEY (`Employees_PK`))

So the problem is that the for loop breaks after one line, and only outputs this:

CREATE TABLE IF NOT EXISTS mydb.Employees (

Any way to get around this? its not essential to preserve the structure (indents, etc.), but it would be nice

AWein
  • 1
  • 1

1 Answers1

0

Because, input() takes only one line.

You can notice that just add a simple print() right after input()

inp = input('Enter Query: ')

print('input check:', inp)

for s in inp:
    if s != '`':
        print(s, end = '')


"""
<Output>
input check: CREATE TABLE IF NOT EXISTS `mydb`.`Employees` (
CREATE TABLE IF NOT EXISTS mydb.Employees (
"""

So, if you want to get input multiple lines, change like this:

print("Enter Query: ", end='')

# Get input until have line of texts.
while True:
    inp = input()

    if not inp:
        break

    for s in inp:
        if s != '`':
            print(s, end='')

Please, check this out: https://stackoverflow.com/a/30239138/7110084

occidere
  • 178
  • 1
  • 5
  • 16