ALTER TABLE Sqlite:如何在更改表之前检查列是否存在?

发布于 2024-08-23 16:23:40 字数 197 浏览 16 评论 0原文

我需要在 python 中执行一个在 sqlite3 中添加新列的 SQL 查询。

问题是有时它已经存在。因此,在执行查询之前,我需要检查该列是否已存在。

如果是,那么我将不会执行查询。

sqlite 有没有办法做到这一点?或者我必须通过 python 代码中的 try-catch 块来实现它吗?

提前非常感谢!

I need to execute in python a SQL query that adds a new column, in sqlite3.

The problem is that sometimes it already exists. So previous to executing the query I need to check if the column already exists.

If it does, then I won't execute the query.

Is there a way in sqlite to do that? Or do I have to make it through a try-catch block in python code?

Thanks a lot in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

漫雪独思 2024-08-30 16:23:40

您可以通过以下语句获取表的列列表:

PRAGMA table_info('table_name');

有关 pragma 命令的更多详细信息,请访问 sqlite 网站

You can get a list of columns for a table via the following statement:

PRAGMA table_info('table_name');

More details on the pragma commands are availabel at the sqlite web site

泪意 2024-08-30 16:23:40

在我看来,这

conn = sqlite3.connect(':memory:')
c = conn.cursor()
try:
    c.execute('ALTER TABLE mytable ADD COLUMN newcolumn;')
except:
    pass # handle the error
c.close()

是比构造特殊情况查询更好的选择。

您可以将上述代码包装在 AddColumn(cursor, table, column) 函数中,以便您可以重用它,
另外它会让代码更具可读性。

IMO this

conn = sqlite3.connect(':memory:')
c = conn.cursor()
try:
    c.execute('ALTER TABLE mytable ADD COLUMN newcolumn;')
except:
    pass # handle the error
c.close()

is a better choice than constructing special case queries.

You can wrap the above code in a AddColumn(cursor, table, column) function so you can reuse it,
plus it'll make the code more readable.

谁的新欢旧爱 2024-08-30 16:23:40

无论出于何种原因,您想要一种明确的方法来检查列是否已存在,您可以在下面找到完整的 Python 配方。由您决定将代码包装在函数中或改进它

import sqlite3

sqlite_db = 'my_sqlite_db.sqlite'
col_to_test = 'my_column'
table_to_test = 'my_table_name'

con = sqlite3.connect(sqlite_db)
check_sqlite_col_exist_query = """SELECT count(*) > 0
FROM pragma_table_info('{}')
WHERE name=?;""".format

with con:
    q = con.execute(check_sqlite_col_exist_query(table_to_test), (col_to_test, ))
    col_exist = q.fetchone()
    col_exist = col_exist[0] > 0
    if not col_exist:
        print('"{}" column does not exist in table "{}"!'.format(col_to_test, table_to_test))
        # Do stuff here like adding your column or something else
    else:
        print('"{}" column already exist in table "{}"!'.format(col_to_test, table_to_test))

For any reason you want an explicitly way to check if a column is already present, you can find a full Python recipe below. Up to you to wrap the code in a function or improve it

import sqlite3

sqlite_db = 'my_sqlite_db.sqlite'
col_to_test = 'my_column'
table_to_test = 'my_table_name'

con = sqlite3.connect(sqlite_db)
check_sqlite_col_exist_query = """SELECT count(*) > 0
FROM pragma_table_info('{}')
WHERE name=?;""".format

with con:
    q = con.execute(check_sqlite_col_exist_query(table_to_test), (col_to_test, ))
    col_exist = q.fetchone()
    col_exist = col_exist[0] > 0
    if not col_exist:
        print('"{}" column does not exist in table "{}"!'.format(col_to_test, table_to_test))
        # Do stuff here like adding your column or something else
    else:
        print('"{}" column already exist in table "{}"!'.format(col_to_test, table_to_test))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文