14

我创建了一个 Python 模块,用于创建和填充多个 SQLite 表。现在,我想在程序中使用它,但我真的不知道如何正确调用它。我发现的所有教程本质上都是“内联”的,即他们以线性方式使用 SQLite,而不是如何在生产中实际使用它。

我想做的是有一个方法检查数据库是否已经创建。如果是这样,那么我可以使用它。如果不是,则会引发异常并且程序将创建数据库。(或使用 if/else 语句,以更好者为准)。

我创建了一个测试脚本来查看我的逻辑是否正确,但它不起作用。当我创建 try 语句时,它只是创建一个新数据库,而不是检查一个是否已经存在。下次我运行脚本时,我得到一个表已经存在的错误,即使我尝试捕获异常。(我之前没有使用过 try/except ,但认为这是学习的好时机)。

是否有任何关于在操作上使用 SQLite 的好教程或关于如何编码的任何建议?我浏览了 pysqlite 教程和我发现的其他教程,但他们没有解决这个问题。

4

8 回答 8

29

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.
于 2008-10-17T10:30:02.670 回答
13

AFAIK SQLITE 数据库只是一个文件。要检查数据库是否存在,请检查文件是否存在。

当你打开一个 SQLITE 数据库时,如果没有备份它的文件,它会自动创建一个。

如果您尝试将文件作为非数据库的 sqlite3 数据库打开,您将得到以下信息:

“sqlite3.DatabaseError:文件已加密或不是数据库”

所以检查文件是否存在,并确保在文件不是 sqlite3 数据库的情况下尝试捕获异常

于 2008-10-17T09:23:01.260 回答
7

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.

于 2008-10-17T09:44:53.760 回答
5

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)
...
于 2008-10-17T09:40:12.880 回答
4
  • 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
于 2008-10-17T09:26:07.307 回答
3

在我学习的任何语言中,总体上执行 SQL 都是可怕的。SQLalchemy 已被证明是最容易使用的,因为实际的查询和提交是如此干净且没有麻烦。

这是在您的应用程序中实际使用 sqlalchemy 的一些基本步骤,可以从文档中找到更好的详细信息。

  • 提供表定义并创建 ORM 映射
  • 加载数据库
  • 要求它根据定义创建表(如果存在则不会这样做)
  • 创建会话制造商(可选)
  • 创建会话

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

于 2008-10-17T09:24:13.350 回答
2

See this solution at SourceForge which covers your question in a tutorial manner, with instructive source code :

y_serial.py module :: warehouse Python objects with SQLite

"Serialization + persistance :: in a few lines of code, compress and annotate Python objects into SQLite; then later retrieve them chronologically by keywords without any SQL. Most useful "standard" module for a database to store schema-less data."

http://yserial.sourceforge.net

于 2009-09-13T05:07:20.723 回答
0

Yes, I was nuking out the problem. All I needed to do was check for the file and catch the IOError if it didn't exist.

Thanks for all the other answers. They may come in handy in the future.

于 2008-10-18T05:19:05.167 回答