| 15 | // ---------------------------------------------------------------------------------------- |
| 16 | |
| 17 | bool table_exists ( |
| 18 | database& db, |
| 19 | const std::string& tablename |
| 20 | ) |
| 21 | { |
| 22 | // Sometimes you want to just run a query that returns one thing. In this case, we |
| 23 | // want to see how many tables are in our database with the given tablename. The only |
| 24 | // possible outcomes are 1 or 0 and we can do this by looking in the special |
| 25 | // sqlite_master table that records such database metadata. For these kinds of "one |
| 26 | // result" queries we can use the query_int() method which executes a SQL statement |
| 27 | // against a database and returns the result as an int. |
| 28 | return query_int(db, "select count(*) from sqlite_master where name = '"+tablename+"'")==1; |
| 29 | } |
| 30 | |
| 31 | // ---------------------------------------------------------------------------------------- |
| 32 | |