English 中文(简体)
进口txt文档进入使用php的邮局时遇到的问题
原标题:Problems while importing a txt file into postgres using php

I am trying to import a txt/csv file into my postgres database from php using "copy" command. I cannot use COPY instead of copy as I need it to execute as a psql client. My code is:

$query =  \ . copy data1 FROM "data1.txt" WITH CSV HEADER DELIMITER AS "," QUOTE AS "^" ;

$result = pg_query($conn,$query);
if (!$result) {
  echo "cannot copy data
";
} else {
  echo "SUCCESS!";
}

当我掌握了这本存放机构档案时,我发现这一错误:

PHP Warning:  pg_query(): Query failed: ERROR:  syntax error at or near ""
LINE 1: copy data1 FROM "data1.txt" WITH ...
    ^ in script.php on line 30
最佳回答

You cannot run copy via pg_query(). It is not an SQL command. It is a meta-command of the psql client.

可以执行:

copy data1 FROM  data1.txt  WITH CSV HEADER DELIMITER AS  ,  QUOTE AS  ^ 

或者管理着壳体:

psql mydb -c "copy data1 FROM  data1.txt 
                WITH CSV HEADER DELIMITER AS  ,  QUOTE AS  ^ "

Note the quotes. Values need to be single-quoted in PostgreSQL: value .
Double-quotes are for identifiers - and are only actually needed for identifiers with upper case or illegal character or for reserved words: "My table".

问题回答

我遇到了同样的问题,但决定不使用原始<代码>psqlcopy的指令,而是决定使用<代码>。 页: 1 Repo.transaction/1, 正常执行COPY 获取以下数据:;https://experiment withcode.com/using-the-postgres-copy-command-in-ecto-migrations/”rel=“nofollow noreferer”>。 我没有测试这一业绩,但我表示,这将是这一答案的一个很好的补充。





相关问题
摘录数据

我如何将Excel板的数据输入我的Django应用? I m将PosgreSQL数据库作为数据库。

Postgres dump of only parts of tables for a dev snapshot

On production our database is a few hundred gigabytes in size. For development and testing, we need to create snapshots of this database that are functionally equivalent, but which are only 10 or 20 ...

How to join attributes in sql select statement?

I want to join few attributes in select statement as one for example select id, (name + + surname + + age) as info from users this doesn t work, how to do it? I m using postgreSQL.

What text encoding to use?

I need to setup my PostgreSQL DB s text encoding to handle non-American English characters that you d find showing up in languages such as German, Spanish, and French. What character encoding should ...

SQL LIKE condition to check for integer?

I am using a set of SQL LIKE conditions to go through the alphabet and list all items beginning with the appropriate letter, e.g. to get all books where the title starts with the letter "A": SELECT * ...

热门标签