How can I export a PostgreSQL table to HTML? - postgresql

How can I save a PostgreSQL table to HTML?

I'll take a stab at assuming what you mean. In psql:
dbname=# \H
dbname=# \d tablename

Here is an example of an XML "forest":
SELECT xmlforest (
"FirstName" as "FName", "LastName" as "LName",
’string’ as "str", "Title", "Region" )
FROM "Demo"."demo"."Employees";
With some data in the employees table, this might result in:
<FName>Nancy</FName>
<LName>Davolio</LName>
<str>string</str>
<Title>Sales Representative</Title>
<Region>WA</Region>
...
<FName>Anne</FName>
<LName>Dodsworth</LName>
<str>string</str>
<Title>Sales Representative</Title>
http://wiki.postgresql.org/wiki/XML_Support

Related

How can I separate the string using space in 1 column? example from "JohnDoe" into "John Doe"

How can I separate the string using space in 1 column? example from "JohnDoe" into "John Doe"
Table Name is employee
Column name is name
You can try this way, see
SELECT regexp_replace('JohnDoe', '([A-Z])', ' \1', 'g');
In your case,
SELECT regexp_replace(name, '([A-Z])', ' \1', 'g') from employee;

Error when copying a csv file to a table using SQL Shell (psql)

I have a file test.csv with two lines:
"smth", "1", "2", "3"
"smth more", "4", "5", "6"
I'm trying to copy it to a table with sql shell:
CREATE TABLE test_table(col1 TEXT, col2 TEXT, col3 TEXT, col4 TEXT);
COPY test_table FROM 'E:\\PostgreSQL13\\scripts\\test.csv';
And get the following error:
wrong syntax for type integer: """"smth"", ""1"", ""2"", ""3""""
What could be the problem here?
Thank you!
Tell copy you are inputing a csv, so it will use the default comma separator and recognize the double quotes. (text format is the default)
COPY test_table FROM 'E:\\PostgreSQL13\\scripts\\test.csv' (FORMAT 'csv')

Concat values to string array postgres

I want to reach following structure:
["687ccca","da075bd","4194d"]
and try to achieve it like this:
UPDATE table1
SET "ids"= CONCAT ('[', result, ']')
FROM (SELECT string_agg( id::character varying, ', ')
FROM table2 where "name" like '%W11%'
or "name" like '%12%'
or "name" like '%13%'
or "name" like '%5%'
or "name" like '%W9%'
or "name" like '%74%'
) AS result
WHERE "ids"='all';
however I get this:
[("df6bd58d, 26e094b, 637c1, 4a8cf387ff43c5, 9b0bf9f")]
How do I remove ( and ) and add " after each id?
I believe you want to get an JSON array:
demo:db<>fiddle
SELECT
json_agg(your_texts)
FROM
your_table
If you really want text you can cast this result with ::text into a text afterwards:
json_agg(your_texts)::text

Change Json text to json array

Currently in my table data is like this
Field name : author
Field Data : In json form
When we run select query
SELECT bs.author FROM books bs; it returns data like this
"[{\"author_id\": 1, \"author_name\": \"It happend once again\", \"author_slug\": \"fiction-books\"}]"
But I need selected data should be like this
[
{
"author_id": 1,
"author_name": "It happend once again",
"author_slug": "fiction-books"
}
]
Database : PostgreSql
Note : Please avoid PHP code or iteration by PHP code
The answer depends on the version of PostgreSQL you are using and ALSO what client you are using but PostgreSQL has lots of builtin json processing functions.
https://www.postgresql.org/docs/10/functions-json.html
Your goal is also not clearly defined...If all you want to do is pretty print the json, this is included.
# select jsonb_pretty('[{"author_id": 1,"author_name":"It happend once again","author_slug":"fiction-books"}]') as json;
json
-------------------------------------------------
[ +
{ +
"author_id": 1, +
"author_name": "It happend once again",+
"author_slug": "fiction-books" +
} +
]
If instead you're looking for how to populate a postgres record set from json, this is also included:
# select * from json_to_recordset('[{"author_id": 1,"author_name":"It happend once again","author_slug":"fiction-books"}]')
as x(author_id text, author_name text, author_slug text);
author_id | author_name | author_slug
-----------+-----------------------+---------------
1 | It happend once again | fiction-books

String getting converted to number when inserting it in database through Perl's DBI $sth->execute() function

I'm using Perl's DBI and SQLite database (I have DBD::SQLite installed). I have the following code:
my $dbh = DBI->connect("dbi:SQLite:dbname=$db", "", "", { RaiseError => 1, AutoCommit => 1 });
...
my $q = "INSERT OR IGNORE INTO books (identica, book_title) VALUES (?, ?)";
my $sth = $dbh->prepare($q);
$sth->execute($book_info->{identica}, $book_info->{book_title});
The problem I have is when $book_info->{identica} begins with 0's they get dropped and I get a number inserted in the database.
For example, identica of 00123 will get converted to 123.
I know SQLite doesn't have types, so how do I make DBI to insert the identica as string rather than number?
I tried quoting it as "$book_info->{identica}" when passing to $sth->execute but that didn't help.
EDIT
Even if I insert value directly in query it doesn't work:
my $i = $book_info->{identica};
my $q = "INSERT OR IGNORE INTO books (identica, book_title) VALUES ('$i', ?)";
my $sth = $dbh->prepare($q);
$sth->execute($book_info->{book_title});
This still coverts 00123 to 123, and 0000000009 to 9...
EDIT
Holy sh*t, I did this on the command line, and I got this:
sqlite> INSERT INTO books (identica, book_title) VALUES ('0439023521', 'a');
sqlite> select * from books where id=28;
28|439023521|a|
It was dropped by SQLite!
Here is how the schema looks:
CREATE TABLE books (
id INTEGER PRIMARY KEY AUTOINCREMENT,
identica STRING NOT NULL,
);
CREATE UNIQUE INDEX IDX_identica on books(identica);
CREATE INDEX IDX_book_title on books(book_title);
Any ideas what is going on?
SOLUTION
It's sqlite problem, see answer by in the comments by Jim. The STRING has to be TEXT in sqlite. Otherwise it treats it as number!
Changing schema to the following solved it:
CREATE TABLE books (
id INTEGER PRIMARY KEY AUTOINCREMENT,
identica TEXT NOT NULL,
);
Use bind params
my $sth = $dbh->prepare($q);
$sth->bind_param(1, 00123, { TYPE => SQL_VARCHAR });
$sth->bind_param(2, $book_info->{book_title});
$sth->execute();
UPDATE:
Read about type affinity in SQLite. Because your column type is STRING (technically unsupported), it defaults to INTEGER affinity. You need to create your column as TEXT instead.
According to the docs, if the column type (affinity) is TEXT it should store it as a string; otherwise it will be a number.