0

I have string value like:

test.test.com
atest.com
btest.com
ctest.com

I want to insert them into Database table like: DB Example

I tried this:

@flask.route('/domain_settings', methods=['GET', 'POST'])
def domain_settings():
   ss = (s.replace(' ', ''))
   print(ss)
   # test.test.com
   # atest.com
   # btest.com
   # ctest.com
   cur = mysql.connection.cursor()
   cur.execute('INSERT INTO domain (domain_name) VALUES (%s)', [ss])
   mysql.connection.commit()
   cur.close()

But it insert them into one row like: db

How can i achieve that?

2
  • if that is a single string value and you are using a single insert statement, then isn't the inserted value correct ? Commented Jul 26, 2021 at 14:19
  • 1
    if you want to insert each line as a row, you should split into different elements, then either do a batch insert or loop overt the split list and insert them. Commented Jul 26, 2021 at 14:21

1 Answer 1

1

You can try split method and split the string on "\n" and then insert the values in a for loop.

@flask.route('/domain_settings', methods=['GET', 'POST'])
def domain_settings():
   s_list = s.split('\n') 
   cur = mysql.connection.cursor()
   for domain in s_list:
       cur.execute('INSERT INTO domain (domain_name) VALUES (%s)', [domain])
   mysql.connection.commit()
   cur.close()
Sign up to request clarification or add additional context in comments.

Comments

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.