reading external sql script in python

后端 未结 3 1914
天涯浪人
天涯浪人 2020-11-28 21:47

I am working on a learning how to execute SQL in python (I know SQL, not Python).

I have an external sql file. It creates and inserts data into three tables \'Zooke

3条回答
  •  一向
    一向 (楼主)
    2020-11-28 22:35

    A very simple way to read an external script into an sqlite database in python is using executescript():

    import sqlite3
    
    conn = sqlite3.connect('csc455_HW3.db')
    
    with open('ZooDatabase.sql', 'r') as sql_file:
        conn.executescript(sql_file.read())
    
    conn.close()
    

提交回复
热议问题