Using SQLite in a Python program

后端 未结 8 700
逝去的感伤
逝去的感伤 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 06:53

    SQLite automatically creates the database file the first time you try to use it. The SQL statements for creating tables can use IF NOT EXISTS to make the commands only take effect if the table has not been created This way you don't need to check for the database's existence beforehand: SQLite can take care of that for you.

    The main thing I would still be worried about is that executing CREATE TABLE IF EXISTS for every web transaction (say) would be inefficient; you can avoid that by having the program keep an (in-memory) variable saying whether it has created the database today, so it runs the CREATE TABLE script once per run. This would still allow for you to delete the database and start over during debugging.

    0 讨论(0)
  • 2020-12-31 07:09
    • Sqlite doesn't throw an exception if you create a new database with the same name, it will just connect to it. Since sqlite is a file based database, I suggest you just check for the existence of the file.
    • About your second problem, to check if a table has been already created, just catch the exception. An exception "sqlite3.OperationalError: table TEST already exists" is thrown if the table already exist.
    import sqlite3
    import os
    database_name = "newdb.db"
    if not os.path.isfile(database_name):
        print "the database already exist"
    db_connection = sqlite3.connect(database_name)
    db_cursor = db_connection.cursor()
    try:
        db_cursor.execute('CREATE TABLE TEST (a INTEGER);')
    except sqlite3.OperationalError, msg:
        print msg
    
    0 讨论(0)
  • 2020-12-31 07:09

    Doing SQL in overall is horrible in any language I've picked up. SQLalchemy has shown to be easiest from them to use because actual query and committing with it is so clean and absent from troubles.

    Here's some basic steps on actually using sqlalchemy in your app, better details can be found from the documentation.

    • provide table definitions and create ORM-mappings
    • load database
    • ask it to create tables from the definitions (won't do so if they exist)
    • create session maker (optional)
    • create session

    After creating a session, you can commit and query from the database.

    0 讨论(0)
  • 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)
    ...
    
    0 讨论(0)
  • 2020-12-31 07:11

    Don't make this more complex than it needs to be. The big, independent databases have complex setup and configuration requirements. SQLite is just a file you access with SQL, it's much simpler.

    Do the following.

    1. Add a table to your database for "Components" or "Versions" or "Configuration" or "Release" or something administrative like that.

      CREATE TABLE REVISION( RELEASE_NUMBER CHAR(20) );

    2. In your application, connect to your database normally.

    3. Execute a simple query against the revision table. Here's what can happen.
      • The query fails to execute: your database doesn't exist, so execute a series of CREATE statements to build it.
      • The query succeeds but returns no rows or the release number is lower than expected: your database exists, but is out of date. You need to migrate from that release to the current release. Hopefully, you have a sequence of DROP, CREATE and ALTER statements to do this.
      • The query succeeds, and the release number is the expected value. Do nothing more, your database is configured correctly.
    0 讨论(0)
  • 2020-12-31 07:13

    AFAIK an SQLITE database is just a file. To check if the database exists, check for file existence.

    When you open a SQLITE database it will automatically create one if the file that backs it up is not in place.

    If you try and open a file as a sqlite3 database that is NOT a database, you will get this:

    "sqlite3.DatabaseError: file is encrypted or is not a database"

    so check to see if the file exists and also make sure to try and catch the exception in case the file is not a sqlite3 database

    0 讨论(0)
提交回复
热议问题