简体   繁体   English

For Loop或executemany - Python和SQLite3

[英]For Loop or executemany - Python and SQLite3

I have started to learn Python and SQL recently and have a question. 我最近开始学习Python和SQL并且有一个问题。

Using Python with SQLite3 I have written the following code: 使用Python和SQLite3我编写了以下代码:

# Use sqlite3 in the file
import sqlite3

# Create people.db if it doesn't exist or connect to it if it does exist
with sqlite3.connect("people.db") as connection:
    c = connection.cursor()

    # Create new table called people
    c.execute("""CREATE TABLE IF NOT EXISTS people(firstname TEXT, lastname TEXT, age INT, occupation TEXT)""")


    people_list = [
        ('Simon', 'Doe', 20, 'Python Master'),
        ('John', 'Doe', 50, 'Java Master'),
        ('Jane', 'Doe', 30, 'C++ Master'),
        ('Smelly', 'Doe', 2, 'Shower Master')
    ]

    # Insert dummy data into the table
    c.executemany("""INSERT INTO people(firstname, lastname, age, occupation) VALUES(?, ?, ?, ?)""", people_list)

I noticed that I can do the same thing using a for loop instead of executemany like so: 我注意到我可以使用for循环而不是executemany来执行相同的操作:

# Use sqlite3 in the file
import sqlite3

# Create people.db if it doesn't exist or connect to it if it does exist
with sqlite3.connect("people.db") as connection:
    c = connection.cursor()

# Create new table called people
c.execute("""CREATE TABLE IF NOT EXISTS people(firstname TEXT, lastname TEXT, age INT, occupation TEXT)""")


people_list = [
    ('Simon', 'Doe', 20, 'Python Master'),
    ('John', 'Doe', 50, 'Java Master'),
    ('Jane', 'Doe', 30, 'C++ Master'),
    ('Smelly', 'Doe', 2, 'Shower Master')
]

# Insert dummy data into the table
for person in people_list:
    c.execute("""INSERT INTO people(firstname, lastname, age, occupation) VALUES(?, ?, ?, ?)""", person)

I'm just wondering which one is more efficient and used more often? 我只是想知道哪一个更有效率并且更频繁地使用?

A batch insert with executemany will be more efficient, and the performance difference will typically be quite large as your number of records gets large. 使用executemany的批量插入将更有效,并且随着记录数量的增加,性能差异通常会非常大。 There is a significant overhead to executing an insert statement, which you incur over and over again if you insert one row at a time. 执行insert语句会产生很大的开销,如果一次插入一行,就会反复出现这种情况。

You should prefer a batch insert whenever it is straightforward to do so. 只要直接插入,您就应该更喜欢批量插入。

In some cases, it may be much simpler to code an algorithm that inserts one row at a time. 在某些情况下,编写一次插入一行的算法可能要简单得多。 For example, if you are receiving data from somewhere one item at a time, it may be simpler to just insert it as you get it, rather than building up a list and doing a single insert when you have saved up a lot of data. 例如,如果您一次从某个项目的某个位置接收数据,则可能更容易在您获取数据时插入数据,而不是在保存大量数据时构建列表并执行单个插入。 In such a case, you need to consider the tradeoffs between performance and simplicity of code. 在这种情况下,您需要考虑性能和代码简单性之间的权衡。 It's often better to start off with the simple approach (insert one row at a time) and then only optimize to something else if you find it's not performant. 通常最好从简单的方法开始(一次插入一行),然后如果发现它不具备性能,则只优化其他方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM