#include #include #include #include "epgsql.h" #include #include #include #include #include #include #include static int callback(void *NotUsed, int argc, char **argv, char **azColName){ int i; for(i=0; i Create it if it doesn't exist Return the Success/Failure status of the Open command and the pointer to the opened DB */ int rc; rc = sqlite3_open_v2(DBFile, &*db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, 0); if( rc != SQLITE_OK){ fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(*db)); sqlite3_close(*db); exit(1); } return 0; } int CloseDB(sqlite3 *db) { /* Close the database and return the result */ return sqlite3_close(db); } int InitDB(char * InitScript, sqlite3 *db) { /* Use the database initialisation script to initialise the DB Script should create tables if they don't already exist And delete any data that will be replaced */ int fp; char *zErrMsg = 0; int rc; char * SQLStatement; uint32_t SQLStatementSize; struct stat st; /* Get the size of the init file */ if (stat(InitScript, &st) == -1) { fprintf(stderr, "Error getting size of DB Creation/Initialisation File : %s\n", InitScript); sqlite3_close(db); exit(1); } SQLStatementSize = st.st_size; if ((fp = open(InitScript, O_RDONLY, 0)) == -1) { fprintf(stderr, "Failed to open DB Creation/Initialisation File : %s\n", InitScript); sqlite3_close(db); exit(1); } SQLStatement = (char *)mmap(NULL, SQLStatementSize, PROT_READ, MAP_SHARED, fp, 0); if (SQLStatement == MAP_FAILED) { fprintf(stderr, "Failed to MMAP the DB Creation File : %s\n", InitScript); fprintf(stderr, "SQLStatement : %s, Map_failed : %i\n", SQLStatement, MAP_FAILED); close(fp); sqlite3_close(db); exit(1); } rc = sqlite3_exec(db, SQLStatement, 0, 0, &zErrMsg); munmap((void *)SQLStatement, SQLStatementSize); if (fp > 0) close(fp); if( rc!=SQLITE_OK ){ fprintf(stderr, "SQL error executing DB Init script : %s\n", zErrMsg); fprintf(stderr, "RC = %i\n", rc); sqlite3_free(zErrMsg); sqlite3_close(db); exit(1); } return 0; } int ExecSQLStatement(char *SQL, sqlite3 *db) { int rc; char *zErrMsg = 0; rc = sqlite3_exec(db, SQL, 0, 0, &zErrMsg); if( rc!=SQLITE_OK ){ fprintf(stderr, "SQL error executing script : %s\n", zErrMsg); fprintf(stderr, "RC : %i\n", rc); fprintf(stderr, "SQL : %s\n", SQL); sqlite3_free(zErrMsg); return 1; } return 0; } int ExecSQLStatementRowCount(char *SQL, sqlite3 *db) { int rc; char *zErrMsg = 0; rc = sqlite3_exec(db, SQL, 0, 0, &zErrMsg); if( rc!=SQLITE_OK ){ fprintf(stderr, "SQL error executing script : %s\n", zErrMsg); fprintf(stderr, "RC : %i\n", rc); fprintf(stderr, "SQL : %s\n", SQL); sqlite3_free(zErrMsg); return -1; } return sqlite3_changes(db); } /*int GetFileTimestamp(sqlite3 *db, char *res)*/ void GetFileTimestamp(unsigned int * TS) { sqlite3_stmt *pStmt; char sql[256]={0}; int rc; int i; sqlite3 *db1; int KeepGoing=1; *TS = 0; rc = sqlite3_open_v2("/opt/epg/epg.db", &db1, SQLITE_OPEN_READWRITE, 0); if( rc ){ fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db1)); sqlite3_close(db1); exit(1); } sprintf(sql, "select lastfileprocessed from epgtimestamp limit 1;"); if (sqlite3_prepare_v2(db1, sql, strlen(sql), &pStmt, NULL) != SQLITE_OK) { fprintf(stderr, "Error Preparing Statement : %s\n", sqlite3_errmsg(db1)); exit(1); } while (KeepGoing==1) { rc = sqlite3_step(pStmt); switch (rc) { case SQLITE_ROW: /* know it will be one column and one row (due to the query) so stuff the result into the result string */ for (i=0; i<(sqlite3_column_count(pStmt)); i++) *TS = strtoul((char*)sqlite3_column_text(pStmt, i), NULL, 10); break; case SQLITE_DONE: KeepGoing=0; break; case SQLITE_ERROR: fprintf(stderr, "Error Executing Statement : %s\n", sqlite3_errmsg(db1)); KeepGoing=0; break; case SQLITE_BUSY: case SQLITE_LOCKED: fprintf(stderr, "In waiting loop ... %i\n", rc); break; default: fprintf(stderr, "Error Condition ... %i\n", rc); fprintf(stderr, "Error Executing Statement : %s\n", sqlite3_errmsg(db1)); KeepGoing=0; break; } } sqlite3_finalize(pStmt); sqlite3_close(db1); }