This is rather not a post, but a small cheat sheet required to work with PostgreSQL operations. Impossible to keep all the information in your head, so you need to do for yourself handbooks to be able to access them at any time.
PostgreSQL installation
I work on Linux Manjaro and the installation of PostgreSQL was reduced to what I opened the package Manager, found PostgreSQL in the search engine and installed. In other distributions, you can install in a similar way.
Working with PostgreSQL
Initial settings
Server startup.
To view the status.
The superuser console.
psql — the PostgreSQL interactive terminal.
Connect to the database server.
Connection information.
Get information in PostgreSQL
List of commands for information.
Users
Creating user with some rights and password.
Remove user.
User privilege
Give the user the privilege on the database.
Revocation of privileges.
Why do we need a scheme and why create it?
The database schema includes descriptions of the content, structure, and integrity constraints used to create and maintain the database.
Create a schema.
Delete the scheme.
Databases
Create a database.
Create a database based on another database.
Or create a database for a specific user.
Remote database.
Tables
A table consists of columns that are defined by type and name, and rows that contain data. At least one of the columns must have a primary key.
A primary key is a field or set of fields that uniquely identify a record. The primary key should be minimal sufficient: it should not contain fields whose removal from the primary key will not affect its uniqueness.
Create a table.
Remove table.
Get a table.
Get a table with order by a certain column.
Get a specific line of table.
Inserting and deleting data and tables
Inserting data to the table.
Delete rows containing name equal to ‘words’ from the table.
Delete all data from the table.
Add an additional column named new_column with the type text and default value ‘default text’
For work with PostgreSQL from Python I found the library psycopg2.
First of all, you need to import the library.
Then you need to connect to the database. This makes the connect method where you must specify the required parameters:
dbname
user
password
host
At the end of the program you need to close the connection.
To execute SQL commands, you need to create a so-called cursor, which also needs to be closed after executing the necessary commands.
To execute SQL commands, you need to call the execute method, which takes the necessary commands into arguments. To get results or data, use methods:
fetchall
fetchone
fetchmany(n)
For example, get the whole table.
The data output is in the format of an array, which can be parsed by accessing the index and get the individual elements.
For example, get value value of line ‘vol’.
Add data to the table.
After making changes, you must commit them by calling the method conn.commit(), otherwise, the data will not be saved.
PostgreSQL and C++
As their friends?
ODB C++
As far as I know, the application of ODB - this is a correct way to work with the database through C++. He allows you to use the database as an object. On the official website there is all information on installation and work with ODB. Unfortunately, at the moment I have not managed to install it properly on my Manjaro Linux, so maybe I will write about it later.
libpqxx
libpqxx - an open source library for working with PostgreSQL that provides methods for executing SQL queries.
It is installed simply:
To work enough to connect the library <pqxx/pqxx>.
Well, give an example to display the entire table.
The flags used for compilation.
The output will be something like this.
It is worth noting that if the database is not changed, it is better to use pqxx::nontransaction.
If you need to insert data into the table, use pqxx:: work.
All work must be done in the try block to catch the exception and quickly fix the problem.
The following example shows how to insert additional rows into a table.
This should be enough for the job. All the queries necessary for working with the database should be formed in std::string SQL.
QtSql
Qt provides its own library for working with databases, which is called QtSql.
To work with it, first of all, you need to add a line to the pro file:
And connect the QtSql library.
The essence of the work is about the same as in the previous examples - send SQL query, get the execution status and response.
For queries with data changes, you can simply make a SQL query with the required data.
It is also possible to first prepare the request form using the prepare method and, if necessary, set the values for the data by the bindValue method.