Create the SQLite tables if needed (if createdb is true), and return * the SQLite database handle. Return NULL on error. */
| 601 | /* Create the SQLite tables if needed (if createdb is true), and return |
| 602 | * the SQLite database handle. Return NULL on error. */ |
| 603 | sqlite3 *dbInit(char *createdb_query) { |
| 604 | sqlite3 *db; |
| 605 | int rt = sqlite3_open(Bot.dbfile, &db); |
| 606 | if (rt != SQLITE_OK) { |
| 607 | fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db)); |
| 608 | sqlite3_close(db); |
| 609 | return NULL; |
| 610 | } |
| 611 | |
| 612 | if (createdb_query) { |
| 613 | char *errmsg; |
| 614 | int rc = sqlite3_exec(db, createdb_query, 0, 0, &errmsg); |
| 615 | if (rc != SQLITE_OK) { |
| 616 | fprintf(stderr, "SQL error [%d]: %s\n", rc, errmsg); |
| 617 | sqlite3_free(errmsg); |
| 618 | sqlite3_close(db); |
| 619 | return NULL; |
| 620 | } |
| 621 | } |
| 622 | return db; |
| 623 | } |
| 624 | |
| 625 | /* Should be called every time a thread exits, so that if the thread has |
| 626 | * an SQLite thread-local handle, it gets closed. */ |
no outgoing calls
no test coverage detected