使用C从mysql结果存储数据的最佳方法

时间:2016-03-18 19:23:29

标签: mysql struct

我的C编程显然是生锈的: - (

我已经研究过http://zetcode.com/db/mysqlc/并且基本上对此感到满意。

但我不确定存储检索数据的最佳方式。

library(ggplot2)
library(dplyr)
library(scales)

set.seed(145)

# simulate the data
df_foo = data.frame(Age=sample(c(1:10),20,replace=TRUE),
                 Rank=sample(c("Extremely","Very","Slightly","Not At All"),
                             20,replace=TRUE),
                 Percent=(runif(10,0,.01)))

# get the ordering that you are interested in
age_order = df_foo %>% 
  filter(Rank %in% c("Extremely", "Very")) %>% 
  group_by(Age) %>% 
  summarize(SumRank = sum(Percent)) %>% 
  arrange(desc(SumRank)) %>% 
  `[[`("Age")

# in some cases ages do not appear in the order because the 
#   ordering logic does not span all categories
age_order = c(age_order, setdiff(unique(df_foo$Age), age_order))

# make age a factor sorted by the ordering above  
ggplot(df_foo, aes(x = factor(Age, levels = age_order), y = Percent, fill = Rank))+
  geom_bar(stat = "identity") +
  coord_flip() + 
  theme_bw() + 
  scale_y_continuous(labels = percent)

这很好,并且给我正确的结果。

熟悉perl哈希,我很想使用C结构。我(小)数据库中的每条记录都包含不同类型的列:char,float,int等。

所以我有:

  int num_fields = mysql_num_fields(result);
  MYSQL_ROW row;
  while ((row = mysql_fetch_row(result))) {
  for(int i = 0; i < num_fields; ) {
    printf("%s ", row[i] ? row[i] : "NULL"); 
  } 
  printf("\n"); 

LEN定义为常数。

我正在尝试这样的事情:

    struct Parameter {
      char  name[LEN_NAME];
      float max;
      float min;
      float sample_interval;
      int active;
      char units[LEN_UNITS];
      char start[LEN_START];
      char start_time[LEN_START_TIME];
      char end[LEN_END];
      char end_time[LEN_END_TIME];
      char type[LEN_TYPE];
    };  
    struct Parameter parameters[MAX_PARAMETERS];

这对于字符似乎工作正常,但对其他类型则不行。

挣扎。也许我是一棵树胶树!

0 个答案:

没有答案