我一直在使用psql Postgres终端使用以下内容将CSV文件导入表中
COPY tbname FROM
'/tmp/the_file.csv'
delimiter '|' csv;
Run Code Online (Sandbox Code Playgroud)
哪个工作正常,但我必须登录到psql终端才能运行它.
我想知道是否有人知道从Linux shell命令行执行类似命令的方法,类似于Postgres允许shell命令如下
/opt/postgresql/bin/pg_dump dbname > /tmp/dbname.sql
Run Code Online (Sandbox Code Playgroud)
这允许从Linux shell转储数据库而无需登录到psql终端.
我想知道是否可以使用IN子句进行查询,其中的选项是LIKE子句,例如我有我现有的SQL返回相同的结果,因为我打算它似乎是一个圆形的方法来做到这一点.
SELECT *
FROM pg_stat_activity
WHERE application_name NOT LIKE '%psql%'
AND (current_timestamp - state_change) > INTERVAL '30 minutes'
AND state IN (
SELECT state
FROM pg_stat_activity
WHERE state LIKE '%idle%'
OR state LIKE '%disabled%'
)
Run Code Online (Sandbox Code Playgroud)
有没有办法取代某些东西
SELECT *
FROM pg_stat_activity
WHERE application_name NOT LIKE '%psql%'
AND (current_timestamp - state_change) > INTERVAL '30 minutes'
AND state IN ('%idle%', '%disabled%')
Run Code Online (Sandbox Code Playgroud) 我偶然发现了不同人编写的几个python程序,我注意到每个函数都在它结束时返回
def function01():
# function
# events
# here
return
Run Code Online (Sandbox Code Playgroud)
我想知道在函数结束时包含返回是否是一个好的python编程实践,即使它没有返回任何东西(纯粹作为结束函数的一种方式)
或者它是否不需要,背景中不会发生任何不同的事情?
大家好
我试图通过将它们保存在服务器上的单独文件中来使我的一些 php 脚本可重用。
/public_html/scripts/phpfunctions.php
<?php echo 'This function has been called' ?>
Run Code Online (Sandbox Code Playgroud)
我有我的 HTML 表单
<form action="scripts/phpfunctions.php" method="post">
<input type="text" name="user_input">
<button type="submit">Submit</button>
</form>
Run Code Online (Sandbox Code Playgroud)
现在该函数是一个单独的文件,它将浏览器导航到该文件,而不是在我当前的文件中执行脚本。
通常我会用 AJAX 解决这个问题并调用 php 函数而不是 php 页面。
是否可以在按下提交按钮后从 php 脚本内部调用 php 函数(它通常在如下页面中执行的方式)
<!DOCTYPE html>
<head><title>No Title</title></head>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
echo 'function has been called'
}
?>
<body>
<form action="index.php" method="post">
<input type="text" name="user_input">
<button type="submit">Submit</button>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 在为该服务器创建 ssh2_tunnel 后,我正忙于尝试连接到 PostgreSQL 数据库(在不同的服务器上运行)。
ssh2_tunnel 似乎工作正常,但我的 pg_connect 不工作,我希望我错过了一些小东西
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
echo "<span>test script<br /></span>";
$connection = ssh2_connect('xxx.xx.xx.xx', 22);
if (ssh2_auth_pubkey_file($connection, 'usname', './ssh_key.pub', './ssh_key', 'psword')) {
echo "<span>Public Key Authentication Successful<br /></span>";
} else {
die('Public Key Authentication Failed');
}
if ($tunnel = ssh2_tunnel($connection, '127.0.0.1', 5432)) {
echo "<span>Tunnel Successful<br /></span>";
} else {
die('Tunnel Failed');
};
// this is where it is failing and I'm not sure why
$string = "host=127.0.0.1 port=5432 dbname=dbname user=dbuser password=dbpass";
$pgsqlcon = …
Run Code Online (Sandbox Code Playgroud) 这是一个非常简单的问题,我正在使用 psql 终端命令 COPY,如下所示
COPY tbname FROM '/tmp/file.csv'
delimiter '|' csv;
Run Code Online (Sandbox Code Playgroud)
但是,此 file.csv 包含数据,例如
random|stuff|32"
Run Code Online (Sandbox Code Playgroud)
也
random|other "stuff"|15
Run Code Online (Sandbox Code Playgroud)
我尝试使用双引号来转义 Postgres 网站建议的引号
random|stuff|32""
random|other ""stuff""|15
Run Code Online (Sandbox Code Playgroud)
这似乎完全删除了我不想要的引号。有没有办法让导入只将这些引号视为常规字符,以便它们像在 csv 文件中一样出现在数据库中?
我想知道在格式化整数时是否可以一起使用两种格式选项.
我知道我可以使用波纹管包含零位
varInt = 12
print(
"Integer : " +
"{:03d}".format(varInt)
)
Run Code Online (Sandbox Code Playgroud)
获得输出"整数:012"
我可以使用以下内容来包含小数位
varInt = 12
print(
"Integer : " +
"{:.3f}".format(varInt)
)
Run Code Online (Sandbox Code Playgroud)
获得输出"整数:12.000"
但是可以将它们一起使用以获得输出"Integer:012.000"
我正在尝试查看我的 PostgreSQL 数据库中特定表上发生的锁。
我看到有一个表叫 pg_locks
select * from pg_locks;
Run Code Online (Sandbox Code Playgroud)
这似乎给了我一堆列,但是否有可能找到关系,因为我看到其中一个列是关系 oid。
我必须将它链接到哪个表才能获得关系名称?
我想知道是否可以批量初始化从类型创建的变量为 null,而不需要单独初始化它们
create type tp_data as (
data01 text,
data02 integer,
data03 text
);
create function sp_function()
returns setof tp_data as
$$
declare
lr_data tp_data;
begin
for lr_data in
select data.data01, data.data02, data.data03
from data
loop
if lr_data.data01 = "cancelled" then
-- what is the correct way to do this?
lr_data.* := null;
-- without having to do this
lr_data.data01 := null;
lr_data.data02 := null;
lr_data.data03 := null;
end if;
return next lr_data;
end loop;
end
$$
language plpgsql;
Run Code Online (Sandbox Code Playgroud)
称呼: …
我对.NET Core(整体来说是ASP.NET)还是很陌生,我想知道在尝试在View中创建C#函数时是否做任何明显错误的事情
我的看法如下(Collections.cshtml)
<h2>Collections</h2>
<div id="content">
@{
// define variables
List<string> collections;
int counter;
// build list
collections = new List<string>();
collections.Add("Nothing 01");
collections.Add("Nothing 02");
// display list
counter = 0;
while(counter < collections.Count)
{
<p>@collections[counter]</p>
counter = counter + 1;
}
}
</div>
Run Code Online (Sandbox Code Playgroud)
一切正常,并且可以执行我想要的操作,但是如果我尝试将其组织为功能,甚至添加基本功能,它就会像波纹管一样折断
@{
// function for no reason
public void testFunc()
{
string nothing;
nothing = null;
return;
}
}
<h2>Collections</h2>
<div id="content">
@{
// define variables
List<string> collections;
int counter;
// build list
collections = new …
Run Code Online (Sandbox Code Playgroud)