且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果存在另一个表,则在SQLite中创建临时表

更新时间:2023-11-23 08:42:22

SQLite几乎没有控制逻辑.作为嵌入式数据库,它被设计为与真实"编程语言一起使用.

SQLite has almost no control logic; as an embedded database, it is designed to be used together with a 'real' programming language.

您可以尝试复制数据,然后忽略错误:

You could just try to copy the data, and ignore the errors:

try:
    c.execute("CREATE TEMP TABLE tempEmployees AS SELECT * FROM Employees")
except:
    pass

但是,这还将消除其他任何错误.

However, this would also suppress any other errors.

一个更好的主意是显式检查表是否存在:

A better idea is to check explicitly whether the table exists:

c.execute("SELECT 1 FROM sqlite_master WHERE type='table' AND name='Employees'")
if c.fetchone():
    c.execute("CREATE TEMP TABLE tempEmployees AS SELECT * FROM Employees")