xxxxxxxxxx
import csv
import psycopg2
# Connect to the PostgreSQL database
conn = psycopg2.connect(
host="your_host",
database="your_database",
user="your_user",
password="your_password"
)
# Create a cursor object to perform database operations
cursor = conn.cursor()
# Specify the path to the CSV file
csv_file_path = "path/to/your/file.csv"
# Open the CSV file and read its contents
with open(csv_file_path, 'r') as file:
# Create a CSV reader object
reader = csv.reader(file)
# Skip the header row if needed
next(reader)
# Iterate over each row in the CSV file
for row in reader:
# Extract the data from each column in the current row
column1 = row[0]
column2 = row[1]
column3 = row[2]
# Use the cursor to execute an SQL INSERT query
cursor.execute(
"INSERT INTO your_table (column1, column2, column3) VALUES (%s, %s, %s)",
(column1, column2, column3)
)
# Commit the changes to the database
conn.commit()
# Close the cursor and the database connection
cursor.close()
conn.close()
xxxxxxxxxx
COPY table_name FROM 'C:\Users\Public\Documents\ert.txt' DELIMITER ',' CSV
-- make sure to put the file in public folder in windows
-- Or temp folder in linux/mac
-- because it might ask for some permissions
xxxxxxxxxx
-- To stdout
COPY (select * from table) TO STDOUT WITH CSV HEADER;
-- To file
COPY (select * from table) TO '/table.csv' WITH CSV HEADER;
xxxxxxxxxx
copy table from '/home/usama/Documents/columns_rearrange.csv' csv header ;
xxxxxxxxxx
COPY mytable FROM '/path/to/csv/file' WITH CSV HEADER; -- must be superuser
xxxxxxxxxx
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
@ValueSource(strings = {"555 555 55 55", "5555555555", "+15555555555"})
void testProcessValidPhones(String phone) {
assertTrue(phoneValidationService.validatePhone(phone));
}