I'm using the libpq library in C to accessing my PostgreSQL database. So, when I do res = PQexec(conn, "SELECT point FROM test_point3d"); I don't know how to convert the PGresult I got to my custom data type.
我正在使用C中的libpq库来访问我的PostgreSQL数据库。所以,当我做res = PQexec(conn,“SELECT point FROM test_point3d”);我不知道如何将PGresult转换为我的自定义数据类型。
I know I can use the PQgetValue function, but again I don't know how to convert the returning string to my custom data type.
我知道我可以使用PQgetValue函数,但我不知道如何将返回的字符串转换为我的自定义数据类型。
1 个解决方案
#1
4
The best way to think about this is that data types interact with applications over a textual interfaces. Libpq returns a string from just about anything. The programmer has a responsibility to parse the string and create a data type from it. I know the author has probably abandoned the question but I am working on something similar and it is worth documenting a few important tricks here that are helpful in some cases.
考虑这一点的最佳方式是数据类型通过文本界面与应用程序交互。 Libpq从几乎任何东西返回一个字符串。程序员有责任解析字符串并从中创建数据类型。我知道作者可能已经放弃了这个问题,但我正在研究类似的东西,值得记录一些在某些情况下有用的重要技巧。
Obviously if this is a C language type, with its own in and out representation, then you will have to parse the string the way you would normally.
显然,如果这是一种C语言类型,具有自己的输入和输出表示,那么你将必须按照通常的方式解析字符串。
However for arrays and tuples, the notation is basically
但是对于数组和元组,符号基本上是
[open_type_identifier][csv_string][close_type_identifier]
For example a tuple may be represented as:
例如,元组可以表示为:
(35,65,1111111,f,f,2011-10-06,"2011-10-07 13:11:24.324195",186,chris,f,,,,f)
This makes it easy to parse. You can generally use existing csv processers once you trip off the first and last character. Moreover, consider:
这使得解析变得容易。一旦你离开第一个和最后一个角色,你通常可以使用现有的csv处理器。此外,考虑:
select row('test', 'testing, inc', array['test', 'testing, inc']);
row
-------------------------------------------------
(test,"testing, inc","{test,""testing, inc""}")
(1 row)
As this shows you have standard CSV escaping inside nested attributes, so you can, in fact, determine that the third attribute is an array, and then (having undoubled the quotes), parse it as an array. In this way nested data structures can be processed in a manner roughly similar to what you might expect with a format like JSON. The trick though is that it is nested CSV.
由于这表明您在嵌套属性中有标准的CSV转义,因此您实际上可以确定第三个属性是一个数组,然后(加上引号),将其解析为数组。通过这种方式,嵌套数据结构的处理方式大致类似于JSON之类的格式。但诀窍在于它是嵌套的CSV。
#1
4
The best way to think about this is that data types interact with applications over a textual interfaces. Libpq returns a string from just about anything. The programmer has a responsibility to parse the string and create a data type from it. I know the author has probably abandoned the question but I am working on something similar and it is worth documenting a few important tricks here that are helpful in some cases.
考虑这一点的最佳方式是数据类型通过文本界面与应用程序交互。 Libpq从几乎任何东西返回一个字符串。程序员有责任解析字符串并从中创建数据类型。我知道作者可能已经放弃了这个问题,但我正在研究类似的东西,值得记录一些在某些情况下有用的重要技巧。
Obviously if this is a C language type, with its own in and out representation, then you will have to parse the string the way you would normally.
显然,如果这是一种C语言类型,具有自己的输入和输出表示,那么你将必须按照通常的方式解析字符串。
However for arrays and tuples, the notation is basically
但是对于数组和元组,符号基本上是
[open_type_identifier][csv_string][close_type_identifier]
For example a tuple may be represented as:
例如,元组可以表示为:
(35,65,1111111,f,f,2011-10-06,"2011-10-07 13:11:24.324195",186,chris,f,,,,f)
This makes it easy to parse. You can generally use existing csv processers once you trip off the first and last character. Moreover, consider:
这使得解析变得容易。一旦你离开第一个和最后一个角色,你通常可以使用现有的csv处理器。此外,考虑:
select row('test', 'testing, inc', array['test', 'testing, inc']);
row
-------------------------------------------------
(test,"testing, inc","{test,""testing, inc""}")
(1 row)
As this shows you have standard CSV escaping inside nested attributes, so you can, in fact, determine that the third attribute is an array, and then (having undoubled the quotes), parse it as an array. In this way nested data structures can be processed in a manner roughly similar to what you might expect with a format like JSON. The trick though is that it is nested CSV.
由于这表明您在嵌套属性中有标准的CSV转义,因此您实际上可以确定第三个属性是一个数组,然后(加上引号),将其解析为数组。通过这种方式,嵌套数据结构的处理方式大致类似于JSON之类的格式。但诀窍在于它是嵌套的CSV。