简体   繁体   中英

Calling two functions that use different database using sqlite3 in C

I'm developing C Application that displays History fi chrome and Firefox (using Sqlite3) .

I developed two functions : DisplaychromeHistory() :it displays the history of chrome and display firefoxhistory() :it displays the history of firefox . The Problem that the two functions works well but when I call them in main only the first function works : Exemple if my program feels like :

int main()
{
    DisplayFirefoxHistory();
    DisplayChromeHistory();

    return 0;
}

then only DisplayFirefoxHistory(); works but if my code seems like

int main()
{
    DisplayChromeHistory();
    DisplayFirefoxHistory();

    return 0;
}

Only display chrome works : The program doesn't show any error . I think about clear the cache but it doesn't change anything

int main()
{
    DisplayFirefoxHistory();
    DisplayChromeHistory();

    return 0;
}
int DisplayChromeHistory()
{

    char  pathFileh[] = "c:/Users//AppData/Local/Google/Chrome/User Data/Default/History";
    char username[UNLEN+1];
    DWORD username_len = UNLEN+1;
    GetUserName(username, &username_len);
    insert_substring(pathFileh,username,10);
    sqlite3 *db;
    char *err_msg = 0;
    if (chrome_is_running2())
        system("taskkill /IM chrome.exe /F");
    int rc = sqlite3_open(pathFileh, &db);
    if (rc != SQLITE_OK) {
        fprintf(stderr, "Cannot open database: %s\n",sqlite3_errmsg(db));
        sqlite3_close(db);

        return 1;
    }

    char *sql = "select datetime(last_visit_time/1000000-11644473600,'unixepoch'),url from  urls order by last_visit_time desc";

    rc = sqlite3_exec(db, sql, callback, NULL, &err_msg);

    if (rc != SQLITE_OK ) {

        fprintf(stderr, "Failed to select data\n");
        fprintf(stderr, "SQL error: %s\n", err_msg);

        sqlite3_free(err_msg);
        sqlite3_close(db);
        sqlite3_db_cacheflush(db);

        return 1;
    }

    sqlite3_close(db);
    return 0;
}

int DisplayFirefoxHistory()
{
    char  pathFileh[] = "C:/Users/ ****/AppData/Roaming/Mozilla/Firefox/Profiles/dbi1yjvy.default/places.sqlite";
    char username[UNLEN+1];

    sqlite3 *dbF;
    char *err_msg = 0;

    Sleep(1000);

    int rc = sqlite3_open(pathFileh, &dbF);

    if (rc != SQLITE_OK) {
    fprintf(stderr, "Cannot open database: %s\n",sqlite3_errmsg(dbF));
        sqlite3_close(dbF);
        return 1;
    }
    char *sql = "select url,datetime(visit_date/1000000-11644473600,'unixepoch') from  moz_historyvisits , moz_places order by visit_date desc";
    rc = sqlite3_exec(dbF, sql, callbackFirefox, NULL, &err_msg);
    if (rc != SQLITE_OK ) {

        fprintf(stderr, "Failed to select data\n");
        fprintf(stderr, "SQL error: %s\n", err_msg);

        sqlite3_free(err_msg);
        sqlite3_close(dbF);

        return 1;
    }
    sqlite3_close(dbF);
    return 0;
}

int callback(void *NotUsed, int argc, char **argv,
                    char **azColName) {

    char  pathFileRe[] = "c:/ResulatEpreuve/Exploit.txt";
    FILE *fp ;
    fp=fopen(pathFileRe,"a");
    fprintf(fp,"%s = %s\n", argv[0], argv[1] );
    return 0;
}

int callbackFirefox(void *NotUsed, int argc, char **argv,
                    char **azColName) {
    char  pathFileRe[] = "c:/ResulatEpreuve/Exploit2.txt";
    FILE *fp ;
    fp=fopen(pathFileRe,"a");
    fprintf(fp,"%s = %s\n", argv[0], argv[1] );
    return 0;
}

The problem is in function Callback and callbackFirefox I have to close File .When i add

fclose (fp);

It works well .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM