SQL按顺序插入值 - 可以一步或两步完成吗?

时间:2016-04-10 20:00:48

标签: sql sqlite

我试图按照定义的顺序插入值..无论如何要将6行合并为2行代码?提前谢谢。

students = [('TOM', 6120, 85),
           ('Jerry', 6110,86),
           ('Spike', 6120,55),
           ('Tyke',6110,73),
           ('Butch',6110,89),
           ('Toodle',6120,76)]
courses = [(6110,'Data Science I', 'LSB105'),
          (6120,'Data Science II', 'LSB109')]
grading = [('A', 90, 100),
          ('B', 80,90),
          ('C',70,80)]

import sqlite3
conn = sqlite3.connect('example3.db')
c = conn.cursor()

c.execute('CREATE TABLE students(name TEXT, courseid INTEGER, score INTEGER)') #create a table
c.executemany('INSERT INTO students VALUES(?,?,?)', students)

c.execute('CREATE TABLE courses(courseid INTEGER, name TEXT, classroom TEXT)') #create a table
c.executemany('INSERT INTO courses VALUES(?,?,?)', courses)

c.execute('CREATE TABLE gradingscheme(letter TEXT, lower REAL, upper REAL)') #create a table
c.executemany('INSERT INTO gradingscheme VALUES(?,?,?)', grading)

conn.commit()
conn.close()

1 个答案:

答案 0 :(得分:0)

你可以执行...

例如(来自doc):

con.executescript("""
  insert into recipe (name, ingredients) values ('broccoli stew', 'broccoli peppers cheese tomatoes');
  insert into recipe (name, ingredients) values ('pumpkin stew', 'pumpkin onions garlic celery');
  insert into recipe (name, ingredients) values ('broccoli pie', 'broccoli cheese onions flour');
  insert into recipe (name, ingredients) values ('pumpkin pie', 'pumpkin sugar flour butter');
  """)