无法使用Psycopg2从Amazon Redshift读取列类型

时间:2018-07-20 20:40:10

标签: python-3.x types amazon-redshift psycopg2

我正在尝试使用psycopg2在redshift中访问表中的列类型。

我这样做是通过在<div class="wrapper"> <div class="blade-container"> <div class="blade-header"> <div class="header-title"> <div class="header-title-wrapper"> Lorem Ipsum </div> </div> <div class="header-actions"> <span class="action-button"> <b>&#x0229f;</b> </span> <span class="action-button" (click)="onMaximize()"> <b>&#x0229E;</b> </span> <span class="action-button danger" id="ngxBladeCloseBtn"> <b>&times;</b> </span> </div> </div> <div class="blade-body"> <div class="blade-body-wrapper"> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> Why do we use it? It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). <h3> Where does it come from? </h3> <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p> <p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. </p> <h3> Where can I get some? </h3> <p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.</p> </div> </div> <div class="blade-footer"> <div class="blade-footer-wrapper"> <h3> Footer </h3> "On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains." </div> </div> </div> </div>上运行一个简单的查询,如下所示:

pg_table_def

这将返回回溯:

SELECT * FROM pg_table_def;

因此psycopg2似乎不支持存储架构的列的类型(以及有关进一步查询的其他类似信息)。

有人遇到过此问题或类似问题并且知道解决方法吗?我的主要目标是能够返回表中的列类型。出于我正在做的目的,我不能使用另一个postgresql适配器。

使用:

python- 3.6.2

psycopg2- 2.7.4

pandas- 0.17.1

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作,并将结果返回给呼叫服务。

cur.execute("select * from pg_table_def where tablename='sales'")

results = cur.fetchall()
for row in results:
    print ("ColumnNanme=>"+row[2] +",DataType=>"+row[3]+",encoding=>"+row[4])  

不确定异常,如果所有权限都很好,那么应该可以正常工作,打印如下内容。

ColumnNanme=>salesid,DataType=>integer,encoding=>lzo
ColumnNanme=>commission,DataType=>numeric(8,2),encoding=>lzo
ColumnNanme=>saledate,DataType=>date,encoding=>lzo
ColumnNanme=>description,DataType=>character varying(255),encoding=>lzo