【问题标题】:get and post table type using pqxx使用 pqxx 获取和发布表类型
【发布时间】:2016-09-22 05:05:08
【问题描述】:

朋友们,美好的一天。我正在使用pqxx,但我有一些问题。

1.我有两张桌子: table1 (table1_id integer) and table2 (table1_id integer, another_id integer) 具有一对多的关系。 如何轻松获取视图中的信息,例如:table1_id、向量 another_ids? 现在我在脚本中使用序列化(字符串连接到“%d %d %d ...”)并在我的 c++ 代码中反序列化。 我还需要在 table1 中插入值。以及如何在一笔交易中做到这一点?

2。我把存储过程称为

    t.exec("SELECT * FROM my_proc(some_argument)");

可能存在任何方式来做到这一点,就像在 c# 中一样?

非常感谢!

【问题讨论】:

    标签: c# c++ sql stored-procedures libpqxx


    【解决方案1】:

    所以,也许它可以帮助某人。

    第一种情况我发现并使用了两种方法: 1、在sql函数中分组concat,在c++中反序列化。如果 table2 只有 table1_id 和另一个整数,它会很快。 2. 我调用了两个函数:get_table1() 和 get_table2(),按 table1_id 排序。然后用两个指针创建 table1 数组:

    std::vector<Table1> table1Array;
    auto ap = wrk.prepared(GetTable1FuncName).exec();
    auto aps = wrk.prepared(GetTable2FuncName).exec();
    auto pos = aps.begin();
    for (auto row = ap.begin(); row != ap.end(); ++row) {
        std::vector<Table2> table2Array;
        while (pos != aps.end()
               && row["table1_id"].as(int()) == pos["table1_id"].as(int())) {
            table2Array.push_back(Table2(pos["first_id"].as(int()), 
                                         pos["second_string"].as(std::string())));
            ++pos;
        }
    
        Table1 tb1(row["table1_id"].as(int()), row["column2"].as(int()),
                   row["column3"].as(int()), row["column4"].as(int()),
                   table2Array);
    
        table1Array.push_back(tb1);
    }
    

    可能它不漂亮,但它正在工作。 插入我为一个元素编写的数据库。先插入Table1,多行后插入Table2。通话后pqxx::work.commit()

    第二种情况不,不存在。还要记住,函数总是返回 1 行!小心!

    【讨论】:

      猜你喜欢
      • 2020-11-08
      • 1970-01-01
      • 2014-08-09
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      • 2012-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多