Using SQLite in a Python program

后端 未结 8 739
逝去的感伤
逝去的感伤 2020-12-31 06:28

I have created a Python module that creates and populates several SQLite tables. Now, I want to use it in a program but I don\'t really know how to call it properly. All the

8条回答
  •  耶瑟儿~
    2020-12-31 07:10

    As @diciu pointed out, the database file will be created by sqlite3.connect. If you want to take a special action when the file is not there, you'll have to explicitly check for existance:

    import os
    import sqlite3
    if not os.path.exists(mydb_path):
        #create new DB, create table stocks
        con = sqlite3.connect(mydb_path)
        con.execute('''create table stocks
          (date text, trans text, symbol text, qty real, price real)''')
    else:
        #use existing DB
        con = sqlite3.connect(mydb_path)
    ...
    

提交回复
热议问题