缺失的表格在 PostgreSQL 数据库中

huangapple go评论65阅读模式
英文:

Missing table in PostgreSQL database

问题

I was able to connect to the database, write the table, read it, but the dbGetQuery command gives an error Error: Failed to prepare query: ERROR: relation "table" does not exist LINE 1: SELECT DISTINCT ym.s.date FROM table. And the dbListTables command returns character(0) and dbExistsTable(conn = con, name ="table") [1] FALSE. That is, if the table is not in the database. Can you please tell me why this is happening and how to solve this problem?

数据库连接成功,成功写入表格并读取,但是dbGetQuery命令出现错误,报告Error: Failed to prepare query: ERROR: relation "table" does not exist LINE 1: SELECT DISTINCT ym.s.date FROM table. 而dbListTables命令返回character(0),dbExistsTable(conn = con, name ="table") 返回[1] FALSE,这表示表格不在数据库中。请告诉我这是为什么以及如何解决这个问题?

这个问题没有解决办法。

英文:

I was able to connect to the database, write the table, read it, but the dbGetQuery command gives an error Error: Failed to prepare query: ERROR: relation "table" does not exist LINE 1: SELECT DISTINCT ym.s.date FROM table. And the dbListTables command returns character(0) and dbExistsTable(conn = con, name ="table") [1] FALSE. That is, if the table is not in the database. Can you please tell me why this is happening and how to solve this problem?

there is no solution to the problem

答案1

得分: 1

General problem so I will give you some hint.

Try to understand your PSQL structure database using the following:

\l - Display database
\c - Connect to database
\dn - List schemas
\dt - List tables inside public schemas
\dt schema1. - List tables inside particular schemas. For eg: 'schema1'.

Most of the cases you are not using the correct schema
英文:

General problem so I will give you some hint.

Try to understand your PSQL structure database using the following:

\l - Display database
\c - Connect to database
\dn - List schemas
\dt - List tables inside public schemas
\dt schema1. - List tables inside particular schemas. For eg: 'schema1'.

Most of the cases you are not using the correct schema

</details>



# 答案2
**得分**: 0

以下是翻译好的部分:

```r
首先尝试每个:

dbGetQuery(con, 'select * from "abc"."ABC_01"')
dbGetQuery(con, 'select * from "ABC"."ABC_01"')

我的探索路径:

DBI::dbExecute(db, "create schema ABC")
dbWriteTable(db, Id(schema="ABC", table="ABC_01"), mtcars, create=TRUE, append=FALSE)
# 错误:nanodbc/nanodbc.cpp:1691: 3F000: 错误:模式 "ABC" 不存在;
# 在执行查询时出错 错误:模式 "ABC" 不存在;
# 在执行查询时出错
dbWriteTable(db, Id(schema="abc", table="ABC_01"), mtcars, create=TRUE, append=FALSE)
### (无错误)
dbGetQuery(db, "select distinct table_schema, table_name from information_schema.columns where table_name ilike '%abc%'")
#   table_schema table_name
# 1          abc     ABC_01

第一个提示:通常情况下,数据库在我们没有明确说明大小写时会忽略大小写。在这种情况下,虽然我“认为”我创建了模式“ABC”,但 PostgreSQL 会悄悄地将其变成小写。

接着:

dbGetQuery(db, "select * from ABC_01 limit 2")
# 错误:nanodbc/nanodbc.cpp:1752: 42P01: 错误:关系 "abc_01" 不存在;
# 在准备参数时出错 错误:关系 "abc_01" 不存在;
# 在准备参数时出错
# <SQL> 'select * from ABC_01 limit 2'
dbGetQuery(db, 'select * from ABC.ABC_01 limit 2')
# 错误:nanodbc/nanodbc.cpp:1752: 42P01: 错误:关系 "abc.abc_01" 不存在;
# 在准备参数时出错 错误:关系 "abc.abc_01" 不存在;
# 在准备参数时出错
# <SQL> 'select * from ABC.ABC_01 limit 2'
dbGetQuery(db, 'select * from abc.ABC_01 limit 2')
# 错误:nanodbc/nanodbc.cpp:1752: 42P01: 错误:关系 "abc.abc_01" 不存在;
# 在准备参数时出错 错误:关系 "abc.abc_01" 不存在;
# 在准备参数时出错
# <SQL> 'select * from abc.ABC_01 limit 2'

dbGetQuery(db, 'select * from "abc"."ABC_01" limit 2')
#       row_names mpg cyl disp  hp drat    wt  qsec vs am gear carb
# 1     Mazda RX4  21   6  160 110  3.9 2.620 16.46  0  1    4    4
# 2 Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

更进一步:

dbExecute(db, 'drop table "ABC"."ABC_01"')
dbExecute(db, 'drop schema "ABC"')
dbExecute(db, 'create schema "ABC"')
dbWriteTable(db, Id(schema="ABC", table="ABC_01"), mtcars, create=TRUE, append=FALSE)
dbGetQuery(db, "select * from ABC.ABC_01 limit 2")
# 错误:nanodbc/nanodbc.cpp:1752: 42P01: 错误:关系 "abc.abc_01" 不存在;
# 在准备参数时出错 错误:关系 "abc.abc_01" 不存在;
# 在准备参数时出错
# <SQL> 'select * from ABC.ABC_01 limit 2'
dbGetQuery(db, 'select * from "ABC"."ABC_01" limit 2')
#       row_names mpg cyl disp  hp drat    wt  qsec vs am gear carb
# 1     Mazda RX4  21   6  160 110  3.9 2.620 16.46  0  1    4    4

dbGetQuery(db, "select distinct table_schema, table_name from information_schema.columns where table_name ilike '%abc%'")
#   table_schema table_name
# 1          ABC     ABC_01
英文:

Up front, try each of these:

dbGetQuery(con, &#39;select * from &quot;abc&quot;.&quot;ABC_01&quot;&#39;)
dbGetQuery(con, &#39;select * from &quot;ABC&quot;.&quot;ABC_01&quot;&#39;)

My path of exploration:

DBI::dbExecute(db, &quot;create schema ABC&quot;)
dbWriteTable(db, Id(schema=&quot;ABC&quot;,table=&quot;ABC_01&quot;), mtcars, create=TRUE, append=FALSE)
# Error: nanodbc/nanodbc.cpp:1691: 3F000: ERROR: schema &quot;ABC&quot; does not exist;
# Error while executing the query   RROR: schema &quot;ABC&quot; does not exist;
# Error while executing the query 
dbWriteTable(db, Id(schema=&quot;abc&quot;,table=&quot;ABC_01&quot;), mtcars, create=TRUE, append=FALSE)
### (no error)
dbGetQuery(db, &quot;select distinct table_schema, table_name from information_schema.columns where table_name ilike &#39;%abc%&#39;&quot;)
#   table_schema table_name
# 1          abc     ABC_01

First hint: often databases disregard the case when we are not explicit about it. In this case, while I think I created schema ABC, postgres silently downcased it.

From there,

dbGetQuery(db, &quot;select * from ABC_01 limit 2&quot;)
# Error: nanodbc/nanodbc.cpp:1752: 42P01: ERROR: relation &quot;abc_01&quot; does not exist;
# Error while preparing parameters   RROR: relation &quot;abc_01&quot; does not exist;
# Error while preparing parameters 
# &lt;SQL&gt; &#39;select * from ABC_01 limit 2&#39;
dbGetQuery(db, &#39;select * from ABC.ABC_01 limit 2&#39;)
# Error: nanodbc/nanodbc.cpp:1752: 42P01: ERROR: relation &quot;abc.abc_01&quot; does not exist;
# Error while preparing parameters   RROR: relation &quot;abc.abc_01&quot; does not exist;
# Error while preparing parameters 
# &lt;SQL&gt; &#39;select * from ABC.ABC_01 limit 2&#39;
dbGetQuery(db, &#39;select * from abc.ABC_01 limit 2&#39;)
# Error: nanodbc/nanodbc.cpp:1752: 42P01: ERROR: relation &quot;abc.abc_01&quot; does not exist;
# Error while preparing parameters   RROR: relation &quot;abc.abc_01&quot; does not exist;
# Error while preparing parameters 
# &lt;SQL&gt; &#39;select * from abc.ABC_01 limit 2&#39;

dbGetQuery(db, &#39;select * from &quot;abc&quot;.&quot;ABC_01&quot; limit 2&#39;)
#       row_names mpg cyl disp  hp drat    wt  qsec vs am gear carb
# 1     Mazda RX4  21   6  160 110  3.9 2.620 16.46  0  1    4    4
# 2 Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

Further:

dbExecute(db, &#39;drop table &quot;ABC&quot;.&quot;ABC_01&quot;&#39;)
dbExecute(db, &#39;drop schema &quot;ABC&quot;&#39;)
dbExecute(db, &#39;create schema &quot;ABC&quot;&#39;)
dbWriteTable(db, Id(schema=&quot;ABC&quot;,table=&quot;ABC_01&quot;), mtcars, create=TRUE, append=FALSE)
dbGetQuery(db, &quot;select * from ABC.ABC_01 limit 2&quot;)
# Error: nanodbc/nanodbc.cpp:1752: 42P01: ERROR: relation &quot;abc.abc_01&quot; does not exist;
# Error while preparing parameters   RROR: relation &quot;abc.abc_01&quot; does not exist;
# Error while preparing parameters 
# &lt;SQL&gt; &#39;select * from ABC.ABC_01 limit 2&#39;
dbGetQuery(db, &#39;select * from &quot;ABC&quot;.&quot;ABC_01&quot; limit 2&#39;)
#       row_names mpg cyl disp  hp drat    wt  qsec vs am gear carb
# 1     Mazda RX4  21   6  160 110  3.9 2.620 16.46  0  1    4    4
# 2 Mazda RX4 Wag  21   6  160 110  3.9 2.875 17.02  0  1    4    4

dbGetQuery(db, &quot;select distinct table_schema, table_name from information_schema.columns where table_name ilike &#39;%abc%&#39;&quot;)
#   table_schema table_name
# 1          ABC     ABC_01

huangapple
  • 本文由 发表于 2023年4月17日 20:55:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035390.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定