[Tutor] How to test for the existence of a table in a sqlite3 db?
srinivas devaki
mr.eightnoteight at gmail.com
Sat Oct 14 03:20:40 EDT 2017
On Sat, Oct 14, 2017 at 12:41 PM, boB Stepp <robertvstepp at gmail.com> wrote:
> py3: c.executescript(sql)
> <sqlite3.Cursor object at 0x00000000026B4490>
> py3: tb_exists = "select name from sqlite_master where type='table'
> and name='BloodPressureReadings'"
> py3: tb_ck = c.execute(tb_exists).fetchone()
> py3: print(tb_ck)
> ('BloodPressureReadings',)
>
> So it is looking like I can use this technique to determine if I need
> to create the BloodPressureReadings table or not. Am I on track here
> or is there a better technique?
>
awesome trick. but most of the time applications had to exported out
of sqlite to either mysql or postgresql at which time it would be a
lot of refactoring as this trick uses sqlite internals to check if a
table exists.
fetchone would always return a tuple as we did count in the sql
instruction. so it would be easier to embed in an if statement.
Regards
Srinivas Devaki
Software Developer at Zomato, New Delhi
Phone: +91 9491 383 249
Telegram: @eightnoteight
More information about the Tutor
mailing list