将文件从 Sqlite3 转储到 PostgreSQL:为什么导入时总是出现错误?

发布于 2024-12-29 10:20:15 字数 575 浏览 3 评论 0原文

我在 Sqlite3 db 中有很多表,现在我想将其导出到 PostgreSQL,但总是出现错误。 我使用了不同的技术从 sqlite 转储:

.模式csv

.header 上

.out ddd.sql

从 my_table 中选择 *

.模式插入

.out ddd.sql

从 my_table 中选择 *

当我尝试通过 phppgadmin 导入它时,我收到如下错误:

错误:关系“my_table”的列“1”不存在

第 1 行:INSERT INTO "public"."my_table" ("1", "1", "Vitas", "[电子邮件受保护]", "..

如何避免这个错误?

提前致谢!

I have many tables in Sqlite3 db and now I want to export it to PostgreSQL, but all the time I get errors.
I've used different techniques to dump from sqlite:

.mode csv

.header on

.out ddd.sql

select * from my_table

and

.mode insert

.out ddd.sql

select * from my_table

And when I try to import it through phppgadmin I get errors like this:

ERROR: column "1" of relation "my_table" does not exist

LINE 1: INSERT INTO "public"."my_table" ("1", "1", "Vitas", "[email protected]", "..

How to avoid this error?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云裳 2025-01-05 10:20:15

咆哮

你得到这个“列...不存在”错误 INSERT INTO "public"."my_table" ("1", ... - 因为引号周围“1”表示这是一个 标识符, 文字

不是 你修复了这个问题,查询仍然会给出错误,因为缺少 VAULES 关键字,正如 Jan 在其他答案中注意到的那样,

正确的形式是:

INSERT INTO "public"."my_table" VALUES ('1', ...

如果此 SQL 是由 sqlite 自动生成的,则对 sqlite 不利

。太棒了关于 SQL 语法的章节打印后只有大约 20 页。无论是谁生成了这个 INSERT,都是:阅读它 :-) 它会得到回报。

真正的解决方案

现在,重点是...要将表从 sqlite 传输到 postgres,您应该使用 COPY,因为它比 INSERT 快得多。

使用双方都能理解的 CSV 格式。

在 sqlite3 中:

create table tbl1(one varchar(20), two smallint);
insert into tbl1 values('hello',10);
insert into tbl1 values('with,comma', 20);
insert into tbl1 values('with "quotes"', 30);
insert into tbl1 values('with
enter', 40);
.mode csv
.header on
.out tbl1.csv
select * from tbl1;

在 PostgreSQL(psql 客户端)中:

create table tbl1(one varchar(20), two smallint);
\copy tbl1 from 'tbl1.csv' with csv header delimiter ','
select * from tbl1;

请参阅 http://wiki.postgresql.org/wiki/COPY< /a>.

Rant

You get this "column ... does not exist" error with INSERT INTO "public"."my_table" ("1", ... - because quotes around the "1" mean this is an identifier, not literal.

Even if you fix this, the query still will give error, because of missing VAULES keyword, as Jan noticed in other answer.

The correct form would be:

INSERT INTO "public"."my_table" VALUES ('1', ...

If this SQL was autogenerated by sqlite, bad for sqlite.

This great chapter about SQL syntax is only about 20 pages in print. My advice to whoever generated this INSERT, is: read it :-) it will pay off.

Real solution

Now, to the point... To transfer table from sqlite to postgres, you should use COPY because it's way faster than INSERT.

Use CSV format as it's understood on both sides.

In sqlite3:

create table tbl1(one varchar(20), two smallint);
insert into tbl1 values('hello',10);
insert into tbl1 values('with,comma', 20);
insert into tbl1 values('with "quotes"', 30);
insert into tbl1 values('with
enter', 40);
.mode csv
.header on
.out tbl1.csv
select * from tbl1;

In PostgreSQL (psql client):

create table tbl1(one varchar(20), two smallint);
\copy tbl1 from 'tbl1.csv' with csv header delimiter ','
select * from tbl1;

See http://wiki.postgresql.org/wiki/COPY.

云雾 2025-01-05 10:20:15

似乎缺少“VALUES”关键字:

INSERT INTO "public"."my_table" VALUES (...)

但是! - 您必须插入带有适当引号的值 - 单引号用于文本,不带引号用于数字。

Seems there is missing "VALUES" keyword:

INSERT INTO "public"."my_table" VALUES (...)

But! - You have to insert values with appropriate quotes - single quotes for text and without quotes for numbers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文