Glib实例学习(7)关系/元组

本文介绍了一个类似数据库的关系数据结构GRelation及其操作方法。通过示例展示了如何创建、插入、统计、选择、删除及销毁关系数据。适用于需要轻量级数据管理的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:概述
   关系:类似数据库,不过目前只限两个字段
   元组:也类似数据库,不过只是关系返回的每条记录

2:结构


typedef struct {
      guint len;
    } GTuples;
3:原型



GRelation* g_relation_new (gint fields);
    void g_relation_index (GRelation *relation,
                                                             gint field,
                                                             GHashFunc hash_func,
                                                             GEqualFunc key_equal_func);
    void g_relation_insert (GRelation *relation,
                                                             ...);
    gboolean g_relation_exists (GRelation *relation,
                                                             ...);
    gint g_relation_count (GRelation *relation,
                                                             gconstpointer key,
                                                             gint field);
    GTuples* g_relation_select (GRelation *relation,
                                                             gconstpointer key,
                                                             gint field);
    gint g_relation_delete (GRelation *relation,
                                                             gconstpointer key,
                                                             gint field);
    void g_relation_destroy (GRelation *relation);

    void g_relation_print (GRelation *relation);

                        GTuples;
    void g_tuples_destroy (GTuples *tuples);
    gpointer g_tuples_index (GTuples *tuples,
                                                             gint index_,
                                                             gint field);
4:实例



#include <stdio.h>
    #include <glib.h>
    #include <glib/gprintf.h>

    struct map {
        int key;
        char *value;
    } m[10] = {
        {0,"zero"},
        {1,"one"},
        {2,"two"},
        {3,"three"},
        {4,"four"},
        {5,"five"},
        {6,"six"},
        {7,"seven"},
        {8,"eight"},
        {9,"nine"}
    };

    typedef struct map map;

    #define NUMS    (sizeof(m)/sizeof(m[0]))

    static void
    test_relation(void)
    {
    // GRelation* g_relation_new(gint fields);
        GRelation *relation = g_relation_new(2);

    // void g_relation_index(GRelation *relation, gint field, GHashFunc hash_func, GEqualFunc key_equal_func);
        g_relation_index(relation, 0, g_int_hash, g_int_equal);
        g_relation_index(relation, 1, g_str_hash, g_str_equal);

    // void g_relation_insert(GRelatioin *relation, ...);
        gint i;
        for (i = 0; i < NUMS; i++)
            g_relation_insert(relation, &m[i].key, m[i].value);

    // gint g_relation_count(GRelation *relation, gconstpointer key, gint fields);
        g_printf("The '%d' should be exist '%d' times now.\t\tResult: %d.\n",
                m[1].key, 1, g_relation_count(relation, &m[1].key, 0));

    // gboolean g_relation_exists(GRelation *relation, ...);
        gboolean b = g_relation_exists(relation, &m[1].key, m[1].value);
        g_printf("The key: '%d' and value: '%s' should be %sfound now.\n",
                m[1].key, m[1].value, b ? "" : "not ");

    // gint g_relation_delete(GRelation *relation, gconstpointer key, gint key);
        g_printf("The key: '%d' has been found '%d' times and deleted now.\n",
                m[1].key, g_relation_delete(relation, &m[1].key, 0));
        
    // GTuples* g_relation_select(GRelation *relation, gconstpointer key, gint field);
    // gpointer g_tuples_index(GTuples *tuples, gint index_, gint field);
    // void g_tuples_destroy(GTuples *tuples);
        g_printf("The all records now:\n");
        for (i = 0; i < NUMS; i++) {
            GTuples *tuples = g_relation_select(relation, m[i].value, 1);
            gint j;
            for (j = 0; j < tuples->len; j++)
                g_printf("Key: %d\t\tValue: %s\n",
                        *(gint *)g_tuples_index(tuples, j, 0),
                        (gchar *)g_tuples_index(tuples, j, 1));
            g_tuples_destroy(tuples);
        }
        g_printf("\n");
    // void g_relation_print(GRelation *relation);
    //    g_relation_print(relation);

    // void g_relation_destroy(GRelation *relation);
        g_relation_destroy(relation);
    }

    int
    main(void)
    {
        printf("BEGIN:\n************************************************************\n");
        test_relation();
        printf("\n************************************************************\nDONE\n");
        
        return 0;
    }
5:结果



[xied1@soho use]$ gcc `pkg-config --cflags --libs glib-2.0` -Wall -o relation relation.c
    [xied1@soho use]$ ./relation
    BEGIN:
    ************************************************************
    The '1' should be exist '1' times now. Result: 1.
    The key: '1' and value: 'one' should be found now.
    The key: '1' has been found '1' times and deleted now.
    The all records now:
    Key: 0 Value: zero
    Key: 2 Value: two
    Key: 3 Value: three
    Key: 4 Value: four
    Key: 5 Value: five
    Key: 6 Value: six
    Key: 7 Value: seven
    Key: 8 Value: eight
    Key: 9 Value: nine


    ************************************************************
    DONE



6:小结
  • 创建: g_relation_new()
  • 插入: g_relation_insert()
  • 统计: g_relation_count()
  • 选择:  g_relation_select()
  • 删除: g_relation_delete()
  • 销毁: g_relation_destroy()


转载于:https://my.oschina.net/iamhere/blog/493153

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值