Python中的sqlite3

Python中的sqlite3,第1张

Python中的sqlite3

若要查看数据库是否存在,可以

sqlite3.connect
转到您认为包含数据库的文件,然后尝试在该文件上运行查询。如果它
不是 数据库,则会出现此错误:

>>> c.execute("SELECt * FROM tbl")Traceback (most recent call last):  File "<stdin>", line 1, in <module>sqlite3.DatabaseError: file is encrypted or is not a database

sqlite3.connect
__如果不存在,
创建数据库;正如@johnp在评论中指出的那样,
os.path.exists
它将告诉您该文件是否存在。

要检查现有表,请查询sqlite_master。例如:

>>> def foo(name):...     for row in c.execute("SELECt name FROM sqlite_master WHERe type='table'"):...  if row == (name,):...          return True...     return False... >>> foo("tz_data")True>>> foo("asdf")False


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5631647.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存