我正在使用 sqlite3 在 Python 中开发一个小型数据库应用程序(当前目标为 2.5 和 2.6)。
如果能够提供一系列可以设置数据库并验证它是否与当前架构匹配的函数,将会很有帮助。在重新发明轮子之前,我想我应该四处寻找可以提供类似功能的库。我希望有类似于 RoR 迁移的东西。 xml2ddl似乎并不意味着作为一个库(尽管它可以这样使用),更重要的是不支持 sqlite3。由于最近缺乏对 xml2ddl 的关注,我还担心有一天需要迁移到 Python 3。
人们还使用其他工具来处理这个问题吗?
最佳答案
您可以通过以下方式找到 sqlite3 表的架构:
import sqlite3
db = sqlite3.connect(':memory:')
c = db.cursor()
c.execute('create table foo (bar integer, baz timestamp)')
c.execute("select sql from sqlite_master where type = 'table' and name = 'foo'")
r=c.fetchone()
print(r)
# (u'CREATE TABLE foo (bar integer, baz timestamp)',)
关于python - 是否有标准方法可以在 python 应用程序之外存储数据库模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2815847/