Skip to content

Commit

Permalink
create postgreSQL insertion script
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksonwalters committed Sep 10, 2024
1 parent 2a4fd50 commit fc7e43f
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions insert_into_psql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import psycopg2
import pickle

#Create PostgreSQL connection object to database
#sql_password = input("Enter pSQL password: ")
conn = psycopg2.connect(database = "jackson",
user = "jackson",
host= 'localhost',
password = "your-password-here",
port = 5432)
# Open a cursor to perform database operations
db_cursor = conn.cursor()

with open("vocab/dict.pkl", 'rb') as wordvector_dict:
wordvectors = pickle.load(wordvector_dict)

for word, embedding in wordvectors.items():
word_string = word.replace("'","''")
embedding_string = str([float(a) for a in embedding])
sql_insert_string = "INSERT INTO wordembeddings (word,embedding) VALUES ('{word}','{embedding}');".format(word=word_string,embedding=embedding_string)
db_cursor.execute(sql_insert_string)

# Make the changes to the database persistent
conn.commit()
# Close cursor and communication with the database
db_cursor.close()
conn.close()

0 comments on commit fc7e43f

Please sign in to comment.