-
Notifications
You must be signed in to change notification settings - Fork 0
/
pypsq_examples.py
39 lines (39 loc) · 1.23 KB
/
pypsq_examples.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
Example of using class PyPSQL.
"""
# Import wrapper class
import pypsql
# Credentials for access to database
database = "hh"
datauser = "python"
datapass = "qqqqqQ!1"
# Create exemplar of class
psql = pypsql.PyPSQL(dbname=database, dbuser=datauser, dbpass=datapass)
# Print DSN connection string
print(psql.dsn_string())
# Connect with database
psql.connect()
# Change search path to your if you have several schemas in your database
psql.set_search_path()
# If connection with database esteblished
if psql.connected:
# Run query
a = psql.query(query="SELECT * FROM vacancies LIMIT 1;")
for r in a.fetchall():
print(r[0])
# Create table
psql.create_table(table_name="IDs", table_schema="ID NUMERIC")
psql.query(query="INSERT INTO IDs SELECT vac_id FROM vacancies;")
a = psql.query(query="SELECT count(*) FROM IDs;")
print(str(a.fetchone()[0]))
# Truncate table
psql.truncate(table_name="IDs")
# Rename table
psql.rename_table(old_table_name="IDs", new_table_name="IDs_new")
# Check exists of the table
a = psql.check_table_exists(table_name="IDs")
print(str(a))
# Drop table
psql.drop_table(table_name="IDs_new")
# Delete exemplar of class
del psql