python - 是否有标准方法可以在 python 应用程序之外存储数据库模式

标签 python sqlite

我正在使用 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/

相关文章:

android - 无法将列动态添加到 sqlite 表中

sqlite - sqlite 支持索引吗?

python - 在 Python 中更新字典值

python - python中的轮廓变形

python - 属性错误 : 'numpy.float64' object has no attribute 'log10'

python - 为什么当我尝试对这个 numpy 数组求和时 Python 会崩溃?

php - 无法让 Python 3 将 POST 数据传输到 PHP

android - 打开存储在 SQLite 中的文件作为 blob

ios - 从不兼容类型 'double*' 分配给 'double'

java - 运行时异常: NullPointerException with SQLite Database