/
asimakov
/
GigaChat
Обзор
Документация
Войти
/
asimakov
/
GigaChat
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
dev
pgvector init.py
58 строк
2 KB
Anton Simakov
init commit
25 янв 2025, 23:24
25 янв 2025, 23:24
e5d7235
Код
Авторство
О чём код?
import psycopg2 from psycopg2 import Error, Binary connection = None cursor = None try: connection = psycopg2.connect(user='postgres', password='123', host='127.0.0.1', # 127.0.0.1 - local port='5432') connection.autocommit = True cursor = connection.cursor() drop_database_query = 'DROP DATABASE IF EXISTS documents' create_database_query = 'CREATE DATABASE documents' cursor.execute(drop_database_query) cursor.execute(create_database_query) print('database <documents> create successful ') except (Exception, Error) as error: print("ERROR with PostgreSQL", error) # Создание и наполнение таблицы doc connection = None cursor = None try: connection = psycopg2.connect(user='postgres', password='123', host='127.0.0.1', # 127.0.0.1 - local port='5432', database='documents') cursor = connection.cursor() create_table_query = ''' CREATE TABLE doc (id SERIAL PRIMARY KEY NOT NULL, name VARCHAR(255) UNIQUE, type VARCHAR(64) NOT NULL, file_binary BYTEA NOT NULL); ''' cursor.execute(create_table_query) connection.commit() doc_paths = [ ('C:\\Users\\smg3e\\PythonProjects\\GigaChat\\Info.txt', 'text') ] for filename,type in doc_paths: with open(filename, 'rb') as file: data = file.read() cursor.execute('''INSERT INTO doc (name, type, file_binary) VALUES (%s, %s, %s)''', (filename, type, Binary(data))) connection.commit() print('Table <doc> created and filled successful') except (Exception, Error) as error: print("ERROR with PostgreSQL", error) finally: if connection: cursor.close() connection.close() print("DB connection closed")