· 5 years ago · Aug 13, 2020, 03:14 PM
1import psycopg2
2from config import Config
3
4
5"""
615.2-EX_XML_EDR_FOP
7
8<RECORD>
9<FIO>�������� ����� ����Ѳ����</FIO>
10<ADDRESS>96012, ��������� ���������� ����, ����� ��������, ̲�������� �����ܪ��, ������� 11, �������� 97</ADDRESS>
11<KVED>47.89 ��������� �������� � ������ � �� ������ ������ ��������</KVED>
12<STAN>���������</STAN>
13</RECORD>
14
15command for postgresql e v mene v evernote !!!
16
17CREATE TABLE IF NOT EXISTS comments (
18 "Id" BIGSERIAL PRIMARY KEY,
19 "PostId" VARCHAR(255) NOT NULL,
20 "Score" VARCHAR(255) NOT NULL,
21 "Text" TEXT NULL,
22 "CreationDate" DATE NOT NULL,
23 "UserId" VARCHAR(255) NULL
24);
25"""
26
27
28# try:
29# connect_str = "dbname='fop' user='ivan' host='localhost' " + \
30# "password=''"
31# # use our connection values to establish a connection
32# conn = psycopg2.connect(connect_str)
33# # create a psycopg2 cursor that can execute queries
34# cursor = conn.cursor()
35# # create a new table with a single column called "name"
36# cursor.execute("""CREATE TABLE fop (id char(40));""")
37# # run a SELECT statement - no data in there, but we can try it
38# cursor.execute("""SELECT * from fop""")
39# rows = cursor.fetchall()
40# print(rows)
41# except Exception as e:
42# print("Uh oh, can't connect. Invalid dbname, user or password?")
43# print(e)
44
45def create_tables():
46 """ create tables in the PostgreSQL database"""
47 commands = ("""
48 CREATE TABLE fop (
49 id BIGSERIAL PRIMARY KEY,
50 FIO VARCHAR(255) NOT NULL,
51 ADDRESS VARCHAR(500),
52 KVED VARCHAR(255) NOT NULL,
53 STAN VARCHAR(255) NOT NULL )""")
54
55 conn = None
56 # try:
57 # read the connection parameters
58 # params = Config()
59 # connect to the PostgreSQL server
60 # conn = psycopg2.connect(**params)
61
62 connect_str = "dbname='fop' user='ivan' host='localhost' " + \
63 "password='ivanlove911911'"
64 # use our connection values to establish a connection
65 conn = psycopg2.connect(connect_str)
66
67 # conn = psycopg2.connect("")
68 cur = conn.cursor()
69 # create table one by one
70 # for command in commands:
71 # cur.execute(command)
72 cur.execute("""
73 CREATE TABLE fop (
74 id BIGSERIAL PRIMARY KEY,
75 FIO VARCHAR(255) NOT NULL,
76 ADDRESS VARCHAR(500),
77 KVED VARCHAR(255) NOT NULL,
78 STAN VARCHAR(255) NOT NULL )""")
79
80 # close communication with the PostgreSQL database server
81 cur.close()
82 # commit the changes
83 conn.commit()
84 # except (Exception, psycopg2.DatabaseError) as error:
85 # print(error)
86 # finally:
87 # if conn is not None:
88 # conn.close()
89
90
91if __name__ == '__main__':
92 create_tables()
93
94# Let’s run the program and use the \dt command to display table list in the suppliers database.
95#
96#
97# suppliers=# \dt
98# List of relations
99# Schema | Name | Type | Owner
100# --------+---------------+-------+----------
101# public | part_drawings | table | postgres
102# public | parts | table | postgres
103# public | vendor_parts | table | postgres
104# public | vendors | table | postgres
105# (4 rows)