Making it Pythonic: create a sqlite3 database if it doesn't exist?

前端 未结 4 922
说谎
说谎 2021-02-19 07:09

I wrote a Python script which initializes an empty database if it doesn\'t exist.

import os

if not os.path.exists(\'Database\'):
    os.makedirs(\'Database\')
          


        
4条回答
  •  不要未来只要你来
    2021-02-19 07:46

    I think you can do it like this:

    import sqlite3
    conn = sqlite3.connect('Database/testDB.db')
    

    This should connect to your database and create it in case that it doesn't exist. I'm not sure this is the most pythonic way, but it does use the sqlite3 module instead of the sqlite3 command.

提交回复
热议问题