PostgreSQL encoding problem with greek characters - sql

I have made a database with utf8 encoding and some greek characters are not recognized in the values that I insert. I use this to create the database:
CREATE DATABASE greek WITH ENCODING 'UTF-8' LC_COLLATE='el_GR.utf8' LC_CTYPE='el_GR.utf8' TEMPLATE=template0;
I have this table:
CREATE TABLE IF NOT EXISTS products(
id SERIAL PRIMARY KEY,
name VARCHAR(50),
category VARCHAR(50),
subcategory VARCHAR(50),
photo VARCHAR(100),
UNIQUE (name)
);
And then with this insert I try to put in the datbase these values here is how it looks in the windows insert command command prompt:
INSERT INTO products VALUES
(0,'Κύκνος Τομάτες Αποφλ Ολoκλ 400γρ','ee0022e7b1b34eb2b834ea334cda52e7','a02951b1c083449b9e7fab2fabd67198'),
(1,'Elite Φρυγανιές Ολικής Άλεσης 180γρ','ee0022e7b1b34eb2b834ea334cda52e7','a483dd538ecd4ce0bdbba36e99ab5eb1'),
(2,'Trata Σαρδέλα Λαδιού 100γρ','ee0022e7b1b34eb2b834ea334cda52e7','df10062ca2a04789bd43d18217008b5f'),
(3,'Μεβγάλ Τυρί Ημισκλ Μακεδ 420γρ','ee0022e7b1b34eb2b834ea334cda52e7','4c73d0eccd1e4dde8bb882e436a64ebb');
when I do the select query to see the values
select * from products;
What I get is these unrecognized characters:
enter image description here
What should I try so that all the characters are recognized inside the database?
I tried changing the encoding of the database to WIN1252 but it did not work.

Related

integrating data into DB shows question marks as value

I have a dataset named "supplier_dim" in an excel sheet, and one of the columns "SUPPLIER NAME" has names in Russian supplier_name_data_input
So when I tried creating a table to integrate data into:
create table supplier_DIM
(
ID_supplier int primary key identity (1,1),
supplier_name nvarchar(50),
supplier_code varchar(50)
)
Then I inserted data into this table:
insert into supplier_DIM (supplier_name, supplier_code)
values ('Шпаркасе Лизинг ДОО', 'DC000325')
I get this result when I select all columns:
How can I fix the question mark value problem?
'Шпаркасе Лизинг ДОО' is a varchar literal, and will not preserve the characters in a database without a special collation. Instead use an nvarchar literal, eg
supplier_DIM (supplier_name,supplier_code) values (N'Шпаркасе Лизинг ДОО','DC000325')

How to make human readable autoincrement column in PostgreSQL?

I need to make the column for store serial number of orders in the online shop.
Currently, I have this one
CREATE TABLE public.orders
(
id SERIAL PRIMARY KEY NOT NULL,
title VARCHAR(100) NOT NULL
);
CREATE UNIQUE INDEX orders_id_uindex ON public.orders (id);
But I need to create the special alphanumeric format for storing this number
like this 5CC806CF751A2.
How can I create this format with Postgres capabilities?
You can create a view that simply converts the ID to a hex value:
create view readable_orders
as
select id,
to_hex(id) as readable_id,
title
from orders;

Generating sql file with uuids and referring those ids further postgres9.5

I am creating a sql file which has uuids as primary key. Here is how my create table definition looks like using pgcrypto extension
CREATE EXTENSION pgcrypto;
CREATE TABLE snw.contacts(
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT,
email TEXT
);
Now I add a record in this table using
INSERT INTO snw.contacts (name,email) VALUES('Dr Nic Williams','drnic');
postgres=# select * from snw.contacts;
id | name | email
--------------------------------------+-----------------+-------
7c627ee0-ac94-40ee-b39d-071299a55c13 | Dr Nic Williams | drnic
Now going ahead in the same file I want to insert a row in one of tables which looks like
CREATE TABLE snw.address(
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
street TEXT
contact UUID
);
where contact UUID refers to ID in snw.contacts table. How can I fetch the uuid which was generated in the first insert and use it in another insert in the snw.address table?Something like:
INSERT INTO snw.address(street,contact) values('ABC', (select id from snw.contacts where email='drnic'));
I can use where clause I am using this script for generating some test data and so I know what the email would be for fetching the id.
Use a data modifying CTE:
with new_contact as (
INSERT INTO snw.contacts (name,email)
VALUES('Dr Nic Williams','drnic')
returning id
)
INSERT INTO snw.address(street,contact)
select 'ABC', id
from new_contact;

Creating Databases in SQLite

We were asked to create a database in sqlite3 and then create a table in it. I used this command:
$sqlite3 me5.db
and tried to create a table with this statement:
CREATE TABLE me5.petID(pet id PRIMARY KEY int(3), pet name varchar(10), pet type varchar(10), pet age int(3));
but it says that:
ERROR: near "CREATE" : syntax error
What could I have possible done wrong? Thanks.
try this
CREATE TABLE petID(pet_id int(3) PRIMARY KEY, pet_name varchar(10), pet_type varchar(10), pet_age int(3));
you dont have to specify the database name because you're already using it after the command sqlite3 me5.db.
you have spaces in the names of the fields, which is not allowed. so i've put underscores instead of spaces.
use PRIMARY KEY after int(3)

unable to insert rows in a new table in sql, getting error as string or binary data would be truncated

Hi I have the below table created
create table person
(
sno int primary key identity(1,1) not null,
firstname nvarchar,
lastname nvarchar,
city nvarchar,
zip int
);
I have written the insert statement as
insert into person
(firstname,lastname,city,zip)
values
('firstname','lastname','city','123456')
but i am getting the following error when i try to insert values into the table
string or binary data would be truncated
The statement has been terminated
Please help to overcome my problem , i am not sure where i did i went wrong.
Please help,
Thanks In Advance.
in your script there are 2 issues:
You did not specify the length of the varchar columns.
Take a look here.
Do not define columns, variables and parameters using VARCHAR, and
NVARCHAR data types without specifying length attribute. This will not
produce a dynamic length string data, but will make SQL Server choose
default length of 1 (NOTE: In some scenarios it the length can be 30).
Replace it with:
create table person
(
sno int primary key identity(1,1) not null,
firstname nvarchar(50),
lastname nvarchar(50),
city nvarchar(50),
zip int
);
2..You are attempting to insert a varchar zip rather as int (which is the columns type).
Replace the insert with:
insert into person
(firstname,lastname,city,zip)
values
('firstname','lastname','city',123456)
Whenever you see this error
string or binary data would be truncated its a column length` issue.
Check the length of the column in the sql field definition
You are trying to insert data of more length than the field length
So either you limit the data to the length in database or increase the column length.
You are inserting a string for an int (zip), try this:
insert into person
(firstname,lastname,city,zip)
values
('firstname','lastname','city',123456)
EDIT:
you should also declare your nvarchar length as it only creates it as a nvarchar(1) if you don't , so change all of your nvarchar's to nvarchar(50) or whatever length you want for max
Per TSQL Technet Article on NVARCHAR:
nvarchar[(n)]: When n is not specified in a data definition or variable declaration statement, the default length is 1. When n is not specified with the CAST function, the default length is 30.