c-预处理mysql数据,同时有效地维护文件写入的原始副本

7y4bm7vi  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(223)

**编辑->我用gcc init.c编译了这个 mysql_config --libs 所以,程序在服务器启动时运行。我已经用python和sql做了大量的数据操作,运行它的数据库已经生成了。

程序基本上在vpn列表上运行select语句。有些是内部的,有些不是。select语句获取gb中已知的内部节点(需要大量python来解决这个问题)。在第42-47行中,我编写了一些代码来创建我构建的表的副本和两行数据。我已经将sql插入封装在一个helper函数中,因此如果您想添加更多的测试数据,只需复制行并替换文本即可。
不管怎样。。问题在于:
目前,程序只是将指定的数据吐出到文本文件中。这很好,但我也希望能够在将数据写入文件之前对其进行预处理,而不影响文件写入。
我正在尝试隔离第三个字段(ip地址),以便将其交给各种分析工具。如有任何建议,我们将不胜感激:)


# include <stdlib.h>

# include <stdio.h>

# include <string.h>

# include <mysql/mysql.h>

const int MAXLEN = 180;

// If any MySQL commands are unsuccessful, report error back and quit
void finish_with_error(MYSQL *con)
{
    fprintf(stderr, "%s\n", mysql_error(con));
    mysql_close(con);
    exit(1);
}

// Helper function, sends a query to SQL
int send_query(MYSQL *con, char query[MAXLEN])
{
    if (mysql_query(con, query)) {
        finish_with_error(con);
    }
}

int main(int argc, char**argv)
{
    MYSQL *con = mysql_init(NULL);

    FILE * fPtrOut;
    fPtrOut   = fopen("data.txt", "w");

    if (fPtrOut == NULL){
        printf("Unable to write to file.\nCheck permissions.\n" );
    }
    if (con == NULL){
        finish_with_error(con);
    }
    if (mysql_real_connect(con, "localhost", "USERNAME", "PASSWORD", 
     "cTest", 0, NULL, 0) == NULL){
        finish_with_error(con);
    }

    /* In the main program, this section does not exist... This is just to provide dummy data for testing purposes.*/
    send_query(con, "CREATE DATABASE IF NOT EXISTS cTest");
    send_query(con, "CREATE TABLE IF NOT EXISTS 2dataFrame (id INT, domain VARCHAR(32), ip VARCHAR(15), flag VARCHAR(2), internal_node TINYINT, inRange INT(1), hops_taken INT(3))");
    send_query(con, "TRUNCATE 2dataFrame");
    send_query(con, "INSERT INTO 2dataFrame (id, domain, ip, flag, internal_node, inRange, hops_taken) VALUES ('590772', 'num01.example.com', '192.168.0.1', 'GB', '1', '1', '8')");
    send_query(con, "INSERT INTO 2dataFrame (id, domain, ip, flag, internal_node, inRange, hops_taken) VALUES ('946700', 'num02.example.com', '192.168.0.2', 'GB', '1', '1', '6')");

    /* This line refines the actual data set... Of course with the sample size, this line is basically redundant */
    send_query(con, "SELECT * FROM cTest.2dataFrame WHERE inRange = 1 AND flag = 'GB' AND hops_taken <= 9");

    MYSQL_RES *result = mysql_store_result(con);
    if (result == NULL) {
        finish_with_error(con);
    }

    int num_fields = mysql_num_fields(result);
    int i, x = 0;
    MYSQL_ROW row;

    /* This function is the one I need help with
     * There's 7 fields in each line, and each 7 iterations, a newline is written to fPtrOut
     * At the minute, it writes them all to a text file. Ideally, I want a way to process it
     * before it writes to file- so that I can extrapolate the IP address from the 3rd field.
     * I am then going to use that in conjunction with code I've already written. I was debating
     * using strtok() but I'm trying to leave the original string unaltered, and be stringent on memory. 
     */
    while ((row = mysql_fetch_row(result))) {
        for (i = 0; i < num_fields; i++){
            fprintf(fPtrOut, "%s\t", row[i] ? row[i] : "NULL");
            x++;
            if (x == 7){
                fprintf(fPtrOut, "\n");
                x = 0;
            }
        }
    }

    fclose(fPtrOut);
    mysql_free_result(result);
    mysql_close(con);
    return 0;
}
eblbsuwk

eblbsuwk1#

没有关系。。。原来解决方案一直都在盯着我看。我从来没有真正需要隔离第三排,它已经为我做了:)

相关问题