我正在将查询(应用程序 pgAdmin)的每个结果复制到 Google 文档表中。
我想知道是否可以将 Postgres 设置为输出选项卡而不是分号 ;
来分隔列中的值 - 我想简单地从 Postgres 复制并粘贴到 Google 文档表。
如果不可能,有没有办法在 Google 文档表中编写宏?
我想再问一个问题 - 如何划分使用 select 命令获得的两个值(以及如何要求 Postgres SQL 存储值,例如 double)?
I am copying every result of Query (application pgAdmin) into Google Docs Sheet.
I am wonder if it is possible to set Postgres to output tabs instead of semicolon ;
to separate values in columns - I would like to simply copy and paste from Postgres to Google Docs sheet.
If it is not possible, is there any way to write a macro in Google Docs sheet?
I would like to ask one more question - how can I divide two values which I get by using select command (and how to demand from Postgres SQL to store values as for example double)?
发布评论
评论(9)
在 PGAdmin 4 中,您可以通过执行 sql 查询然后按 F8 来实现此目的,它将导出查询结果。
步骤一:
步骤 2:现在选择查询结果,然后单击下载 CSV 格式的查询结果,如下所示:
<图片src="https://i.sstatic.net/xRvyc.jpg" alt="在此处输入图像描述">
步骤 3:验证查询结果
您已完成。
In PGAdmin 4, you can achieve this by executing your sql query and then press F8 and it will export the query results.
Step 1:
Step 2: Now select the query result and click download query result in CSV format as shown below:
Step 3: Verify the query result
You are done.
由于 Apps 脚本中的 JDBC 尚不支持 Postgres(出于某种原因),您无法直接在 Google 表格中编写此脚本。您绝对可以编写这个 Google Sheets API 和您选择的语言(例如Python 和 Pandas 使这个任务变得相对简单)。
如果您不想推出自己的解决方案,请查看 SeekWell。它允许您连接到数据库并直接在表格中编写 SQL 查询。您可以创建一个运行“运行表”,它将同时运行多个查询,并安排这些查询运行,甚至无需打开该表。
免责声明:这是我做的。
Since JDBC within Apps Script doesn't yet support Postgres (for some reason), you can't script this directly in Google Sheets. You could definitely script this the Google Sheets API and the language of your choice (e.g. Python and Pandas make this a relatively simple task).
If you don't want to roll your own solution, check out SeekWell. It allows you to connect to databases and write SQL queries directly in Sheets. You can create a run a “Run Sheet” that will run multiple queries at once and schedule those queries to be run without you even opening the Sheet.
Disclaimer: I made this.
现在可以将 PgAdmin3 配置为使用 Tab 作为字段分隔符而不是 ; 进行复制/粘贴。
从 PgAdmin3 主窗口:
您可能需要重新启动 PgAdmin 才能生效。非常适合直接粘贴到 Google 电子表格中!
关于除法问题,如果您只是将两个数字相除,您可能并不总能得到您所期望的结果 - 整数不会自动除法并成为浮点结果 - 您必须对它们进行转换:
这将返回 0.50
而:
返回 0 (不是一般是你想要的)
It is now possible to configure PgAdmin3 to copy/paste with Tab as the field delimiter instead of ;
From the PgAdmin3 MAIN window:
You might have to restart PgAdmin for this to take effect. Works perfectly for pasting directly into Google spreadsheets!
And regarding your division question, if you just divide the two numbers, you might not always get what you expect - integers don't divide automatically and become a float result - you have to cast them:
This returns 0.50
Whereas:
Returns 0 (not generally what you want)
psql
可以在多种方式:逃逸是该方案的一个问题。
COPY
是你的朋友。psql
can do this in a number of ways:Escaping is an issue with that scheme.
COPY
is your friend.使用导出功能。
Use the export ability.
将
COPY
或\copy
的输出保存为.tsv
文件,并使用文件/导入加载该文件。或者,您可以以相同的方式使用 CSV 格式。Save output of
COPY
or\copy
as a.tsv
file and load that using File/Import. Alternatively, you can use the CSV format, in the same way.也许这样的东西可能有用:)
Maybe something like this could be usefull :)
不幸的是,宏不这样做。如果您想编写自己的脚本,则必须使用应用程序脚本,但应用程序脚本 JDBC 不支持 postgres,因此您必须构建自己的查询服务器。
您还可以使用 Castodia、Seekwell 和 Kpibees 等插件。您可以在此处找到它们
如果您想将数据库数据与工作表同步,然后更改该数据,最好的选择是添加一个单独的列并使用公式(例如:=A1/2)。如果您使用插件,您可能需要首先检查数据是如何更新的,这样它就不会覆盖所有列。自从我开发了 Castodia 插件以来,我只能代表它。当它更新工作表时,它仅更新包含数据的列。因此,如果您有“年龄、职业、收入”列,它只会覆盖这 3 列,因此您可以安全地用公式添加列。
列的数据类型存储在 Postgres 数据库中,因此如果您将数据存储为文本,它将以文本形式返回。不过,您可以将列设置为工作表中的特定类型。选择您的列,转到“格式”并选择“数字”。此后,当更新数据时,该列将保持所需的格式,除非更新工作表的脚本也更改列格式,因此请确保首先检查这一点。 Castodia 脚本不会更改列格式,因此该解决方案应至少与一个插件配合使用。
Macros don't do it unfortunately. If you want to write your own script, you'd have to use app script, but app script JDBCs don't support postgres, so you'd have to build your own querying sever.
You can also use addons like Castodia, Seekwell and Kpibees. You can find them here
If you want to sync DB data with sheet and then change that data, your best bet would be to add a separate column and use formula (ex: =A1/2). If you're using addon, you might want to check how the data is updated first, so it doesn't overwrite all columns. I can only speak for Castodia addon since I worked on it. When it updates your sheets, it only updates the columns that have data. So if you have columns "age, occupation, income", it will only overwrite those 3 columns, so you can safely add forth column with a formula.
Data type for a column is stored in your Postgres database, so if you store the data as a text, it will return it as a text. You can set your column to be a specific type in your sheet, however. Select your column, go to "Format" and select "Number". After that, when the data is updated, the column will remain in desired format, unless the script that updates the sheet changes column formats as well, so make sure to check for that first. Castodia script does not change column formats, so this solution should work with at least one addon.
我将使用普通 SQL 执行插入/插值,然后使用 QueryClips 之类的工具将数据输入 Google床单。
I would perform your insertions / interpolations using vanilla SQL and then use a tool like QueryClips to get your data into the google sheet.