Probleme mit simpler Abfrage

renesb01

Neuer Benutzer
Beiträge
3
Hallo,

ich möchte gern über ein Terminalfenster eine Datenbankabfrage machen.

Die Abfrage sieht so aus:
psql -h 127.0.0.1 -U XXXXX -d data -p XXXXX -t -A -F";" -c "SELECT t2.f_3 AS "Firma [contact:company]", t1.f_5 AS "Name [contact:familyname]", t1.f_6 AS "Vorname [contact:firstname]", t1.f_26 AS "Rufnummer [telephone:phone]", t1.f_28 AS "Mobil [telephone:mobile]", t1.f_29 AS "Fax [telephone:fax]" FROM public.l_256 AS t1, public.l_256 AS t2 WHERE t1.f_11 = t2.docid" > kontakte.csv

Die Abfrage als solches funktioniert. Das Problem was ich habe ist, dass bei der Frage, so wie sie gepostet habe, die Spaltenköpfe nicht übergeben werden. Macht ja auch Sinn, weil ich den String beende und wieder beginne.
Gebe ich die Abfrage direkt bei der Datenbank ein, funktioniert alles bestens. Nun habe ich schon verschiedene Sachen versucht. Ich habe versucht anstelle von "Firma [contact:company]" --> 'Firma [contact:company]'. Dann hatte ich es mit \"Firma [contact:company]\" aber auch ohne Erfolg.

Mein Ziel ist es, in einem String einen String zu haben.

Vielleicht geht es auch komplett anders. Ich möchte gern einen Befehl haben, den ich in die Konsole eingebe und mir der Rechner gleich das Abfrageergebnis in eine Datei schreibt.

Ich danke euch sehr!
 
Werbung:
Das Problem was ich habe ist, dass bei der Frage, so wie sie gepostet habe, die Spaltenköpfe nicht übergeben werden.

Dafür sorgt Dein Parameter -t:

Code:
psql --help 2>&1  | grep "\-t,"
  -t, --tuples-only  print rows only

in psql:

Code:
test=# create table renesb01(id int, val int);
CREATE TABLE
Time: 212,131 ms
test=*# insert into renesb01 values (1,100);
INSERT 0 1
Time: 0,296 ms
test=*# commit;
COMMIT
Time: 2,556 ms
test=# select * from renesb01 ;
 id | val
----+-----
  1 | 100
(1 row)

in der Bash:

Code:
psql test  -A -F";" -c "select * from renesb01"
id;val
1;100
(1 row)

Zum nachlesen:

Code:
Output format options:
  -A, --no-align  unaligned table output mode
  -F, --field-separator=STRING
  field separator for unaligned output (default: "|")
  -H, --html  HTML table output mode
  -P, --pset=VAR[=ARG]  set printing option VAR to ARG (see \pset command)
  -R, --record-separator=STRING
  record separator for unaligned output (default: newline)
  -t, --tuples-only  print rows only
  -T, --table-attr=TEXT  set HTML table tag attributes (e.g., width, border)
  -x, --expanded  turn on expanded table output
  -z, --field-separator-zero
  set field separator for unaligned output to zero byte
  -0, --record-separator-zero
  set record separator for unaligned output to zero byte
 
Verstehe ich das richtig? Es liegt an dem Parameter -t ?

Ich habe das -t jetzt weggemacht.

Mein Befehl sieht nun so aus:
psql -h 127.0.0.1 -U XXX -d XXX -p 36000 -A -F";" -c "SELECT t2.f_3 AS 'Firma [contact:company]', t1.f_5 AS 'Name [contact:familyname]', t1.f_6 AS 'Vorname [contact:firstname]', t1.f_26 AS 'Rufnummer [telephone:phone]', t1.f_28 AS 'Mobil [telephone:mobile]', t1.f_29 AS 'Fax [telephone:fax]' FROM public.l_256 AS t1, public.l_256 AS t2 WHERE t1.f_11 = t2.docid" > kontakte.csv



Als Fehlermeldung bekomme ich:
ERROR: syntax error at or near "'Firma [contact:company]'"
LINE 1: SELECT t2.f_3 AS 'Firma [contact:company]', t1.f_5 AS 'Name ...
 
Werbung:
Ah,
natürlich mein Fehler. Die Syntax muss ja so lauten:

psql -h 127.0.0.1 -U XXX -d XXX -p XXX -A -F";" -c "SELECT t2.f_3 AS \"Firma [contact:company]\", t1.f_5 AS \"Name [contact:familyname]\", t1.f_6 AS \"Vorname [contact:firstname]\", t1.f_26 AS \"Rufnummer [telephone:phone]\", t1.f_28 AS \"Mobil [telephone:mobile]\", t1.f_29 AS \"Fax [telephone:fax]\" FROM public.l_256 AS t1, public.l_256 AS t2 WHERE t1.f_11 = t2.docid" > kontakte.csv

So geht's!! Vielen Dank!
 
Zurück
Oben