在C中使用sqlite3调用两个使用不同数据库的函数

时间:2017-07-04 07:21:09

标签: c sqlite

我正在开发显示History fi chrome和Firefox(使用Sqlite3)的C Application。

  

我开发了两个函数:DisplaychromeHistory():它显示了   chrome和display firefoxhistory()的历史:它显示了   firefox的历史。   这两个函数运行良好的问题,但是当我在main中调用它们时,只有第一个函数才有效:如果我的程序感觉如下:

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

    return 0;
}

然后只有DisplayFirefoxHistory();但是如果我的代码好像是

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

    return 0;
}

仅显示chrome工作:程序不显示任何错误。我想清除缓存但它不会改变任何东西

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;
}

1 个答案:

答案 0 :(得分:0)

问题在于函数Callback和callbackFirefox我必须关闭File。当我添加

fclose (fp);

效果很好。