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

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

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

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

  1. # include <stdlib.h>
  2. # include <stdio.h>
  3. # include <string.h>
  4. # include <mysql/mysql.h>
  5. const int MAXLEN = 180;
  6. // If any MySQL commands are unsuccessful, report error back and quit
  7. void finish_with_error(MYSQL *con)
  8. {
  9. fprintf(stderr, "%s\n", mysql_error(con));
  10. mysql_close(con);
  11. exit(1);
  12. }
  13. // Helper function, sends a query to SQL
  14. int send_query(MYSQL *con, char query[MAXLEN])
  15. {
  16. if (mysql_query(con, query)) {
  17. finish_with_error(con);
  18. }
  19. }
  20. int main(int argc, char**argv)
  21. {
  22. MYSQL *con = mysql_init(NULL);
  23. FILE * fPtrOut;
  24. fPtrOut = fopen("data.txt", "w");
  25. if (fPtrOut == NULL){
  26. printf("Unable to write to file.\nCheck permissions.\n" );
  27. }
  28. if (con == NULL){
  29. finish_with_error(con);
  30. }
  31. if (mysql_real_connect(con, "localhost", "USERNAME", "PASSWORD",
  32. "cTest", 0, NULL, 0) == NULL){
  33. finish_with_error(con);
  34. }
  35. /* In the main program, this section does not exist... This is just to provide dummy data for testing purposes.*/
  36. send_query(con, "CREATE DATABASE IF NOT EXISTS cTest");
  37. 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))");
  38. send_query(con, "TRUNCATE 2dataFrame");
  39. 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')");
  40. 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')");
  41. /* This line refines the actual data set... Of course with the sample size, this line is basically redundant */
  42. send_query(con, "SELECT * FROM cTest.2dataFrame WHERE inRange = 1 AND flag = 'GB' AND hops_taken <= 9");
  43. MYSQL_RES *result = mysql_store_result(con);
  44. if (result == NULL) {
  45. finish_with_error(con);
  46. }
  47. int num_fields = mysql_num_fields(result);
  48. int i, x = 0;
  49. MYSQL_ROW row;
  50. /* This function is the one I need help with
  51. * There's 7 fields in each line, and each 7 iterations, a newline is written to fPtrOut
  52. * At the minute, it writes them all to a text file. Ideally, I want a way to process it
  53. * before it writes to file- so that I can extrapolate the IP address from the 3rd field.
  54. * I am then going to use that in conjunction with code I've already written. I was debating
  55. * using strtok() but I'm trying to leave the original string unaltered, and be stringent on memory.
  56. */
  57. while ((row = mysql_fetch_row(result))) {
  58. for (i = 0; i < num_fields; i++){
  59. fprintf(fPtrOut, "%s\t", row[i] ? row[i] : "NULL");
  60. x++;
  61. if (x == 7){
  62. fprintf(fPtrOut, "\n");
  63. x = 0;
  64. }
  65. }
  66. }
  67. fclose(fPtrOut);
  68. mysql_free_result(result);
  69. mysql_close(con);
  70. return 0;
  71. }
eblbsuwk

eblbsuwk1#

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

相关问题