S
S
skweartman2022-01-16 04:28:49
Python
skweartman, 2022-01-16 04:28:49

How to write all queries in one in sql?

cursor.execute("""CREATE TABLE IF NOT EXISTS information
                  (id INTEGER PRIMARY KEY AUTOINCREMENT,userid INTEGER,money INTEGER,weight INTEGER,sportmode INTEGER)
               """)
  cursor.execute("""CREATE TABLE IF NOT EXISTS task
                  (id INTEGER PRIMARY KEY AUTOINCREMENT,list TEXT, status INTEGER)
               """)
  cursor.execute("""CREATE TABLE IF NOT EXISTS what
                  (mycommands TEXT)
               """)
  cursor.execute("""CREATE TABLE IF NOT EXISTS accounts
                  (login TEXT, password TEXT, token TEXT)
               """)


Please tell me how to write these four requests in one, it hurts my eyes from such horror cursor.execute 4 times, aaaaaa

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Vodakov, 2022-01-16
@WaterSmith

Maybe so?

cursor.execute("""CREATE TABLE IF NOT EXISTS information
                  (id INTEGER PRIMARY KEY AUTOINCREMENT,userid INTEGER,money INTEGER,weight INTEGER,sportmode INTEGER);
                  CREATE TABLE IF NOT EXISTS task
                  (id INTEGER PRIMARY KEY AUTOINCREMENT,list TEXT, status INTEGER);
                  CREATE TABLE IF NOT EXISTS what
                  (mycommands TEXT);
                  CREATE TABLE IF NOT EXISTS accounts
                  (login TEXT, password TEXT, token TEXT)
               """)

S
Sergey c0re, 2022-01-26
@erge

SQLite3??
Cursor object methods SQLite3
cursor.executescript(sql_script)
UPDATE:
either like this for example:

sql_script = """
CREATE TABLE IF NOT EXISTS information
                  (id INTEGER PRIMARY KEY AUTOINCREMENT,userid INTEGER,money INTEGER,weight INTEGER,sportmode INTEGER);
CREATE TABLE IF NOT EXISTS task
                  (id INTEGER PRIMARY KEY AUTOINCREMENT,list TEXT, status INTEGER);
CREATE TABLE IF NOT EXISTS what
                  (mycommands TEXT);
CREATE TABLE IF NOT EXISTS accounts
                  (login TEXT, password TEXT, token TEXT)
"""

for sql in sql_script.split(";\n"):
  cursor.execute(sql)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question