0% found this document useful (0 votes)
37 views

Python SQL

Uploaded by

dbqatest001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Python SQL

Uploaded by

dbqatest001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Python sql.

ipynb - Colab 13/06/24, 3:59 PM

def generate_sql(table_name, col_names):

columnNames = ', '.join(col for col in col_names)

return f"CREATE TABLE {table_name} ({columnNames})"

# generate_sql('Customer', ['Id', 'FirstName', 'LastName', 'Age'])

generate_sql('Product', ['Id', 'ProductName'])

# 'CREATE TABLE Product (Id, ProductName)'

'CREATE TABLE Product (Id, ProductName)'

col_names = ['Id', 'FirstName', 'LastName', 'Age']

# for col in col_names:


# print(col)

columnNames = (', '.join(col for col in col_names))

print(columnNames)

Id, FirstName, LastName, Age

# Implement a function called generate_sql() that takes three arguments:


# [IN]: generate_sql('Customer', ['Id', 'FirstName'], ['INTEGER PRIMARY KEY', 'TEXT
# table_name - table name (str)
# col_names - column names (list, tuple)
# constraints - constraints for columns (list), empty string means no column constr
# The generate_sql() function generates SQL code that creates a table named table_n

# Example:

# [IN]: generate_sql('Customer', ['Id', 'FirstName'], ['INTEGER PRIMARY KEY', 'TEXT


# [OUT]: 'CREATE TABLE Customer (Id INTEGER PRIMARY KEY, FirstName TEXT NOT NULL)'

https://colab.research.google.com/drive/1zL3BO_-hVFO5LOT_kDCVE6l_NCZTSfdi Page 1 of 4
Python sql.ipynb - Colab 13/06/24, 3:59 PM

# 'CREATE TABLE Customer (Id INTEGER PRIMARY KEY, FirstName TEXT NOT NULL)'
def generate_sql(table_name, col_names, constraints):
col_constraints = (', '.join(col_names[i] + (' ' + constraints[i] if constraints[
return f"CREATE TABLE {table_name} ({col_constraints})"

# generate_sql('Customer', ['Id', 'FirstName'], ['INTEGER PRIMARY KEY', 'TEXT NOT N

# 'CREATE TABLE Customer (Id INTEGER PRIMARY KEY, FirstName TEXT NOT NULL)'

generate_sql('Customer', ['Id', 'FirstName'], ['', 'TEXT NOT NULL'])

'CREATE TABLE Customer (Id, FirstName TEXT NOT NULL)'

a = ['Id', 'FirstName']

b = ['', 'TEXT NOT NULL']

for i in range(len(a)):
print(a[i] + ' ' + b[i])

Id
FirstName TEXT NOT NULL

def generate_sql(table_name, col_names, constraints):


cols = [
" ".join((col, constraint)).strip()
for col, constraint in zip(col_names, constraints)
]
return f'CREATE TABLE {table_name} (' + ', '.join(cols) + ')'

generate_sql('Customer', ['Id', 'FirstName'], ['', 'TEXT NOT NULL'])

'CREATE TABLE Customer (Id, FirstName TEXT NOT NULL)'

https://colab.research.google.com/drive/1zL3BO_-hVFO5LOT_kDCVE6l_NCZTSfdi Page 2 of 4
Python sql.ipynb - Colab 13/06/24, 3:59 PM

def generate_sql(table_name, col_names, constraints):


cols = [" ".join((col, constraint)).strip()
for col, constraint in zip(col_names, constraints)]
return f'CREATE TABLE {table_name} (\n\t' + ',\n\t'.join(cols) + '\n)'

# generate_sql('Customer', ['Id', 'FirstName'], ['INTEGER PRIMARY KEY', 'TEXT NOT N


print(generate_sql('Customer', ['Id', 'FirstName'], ['INTEGER PRIMARY KEY', 'TEXT N

# generate_sql('Customer', ['Id', 'FirstName'], ['INTEGER PRIMARY KEY', 'TEXT NOT N


# 'CREATE TABLE Customer (\n\tId INTEGER PRIMARY KEY,\n\tFirstName TEXT NOT NULL\n)

CREATE TABLE Customer (


Id INTEGER PRIMARY KEY,
FirstName TEXT NOT NULL
)

def hello(a):
return f'Hello {a}, (' + 'Jess' + ')' + 'Have a great day'

hello('world')

'Hello world, (Jess)Have a great day'

import csv

with open('Customer.csv', newline='') as csvFile:

csvReaderF = csv.DictReader(csvFile)
cols = []
for row in csvReaderF:
cols.append(row['Country'])
print(sorted(set(cols)))

import csv

with open('Customer.csv', 'r', encoding='utf-8') as csvFile:


readerF = csv.reader(csvFile, delimiter=',')
columns = next(readerF)
rows = tuple(readerF)

https://colab.research.google.com/drive/1zL3BO_-hVFO5LOT_kDCVE6l_NCZTSfdi Page 3 of 4
Python sql.ipynb - Colab 13/06/24, 3:59 PM

https://colab.research.google.com/drive/1zL3BO_-hVFO5LOT_kDCVE6l_NCZTSfdi Page 4 of 4

You might also like