9th day of python challenges 139-142

This commit is contained in:
abd.shallal 2019-08-06 16:41:37 +03:00
parent 81d4fd37bf
commit 8008cd68d9
9 changed files with 716 additions and 128 deletions

View File

@ -76,6 +76,10 @@
<option value="E501" />
<option value="W29" />
<option value="E501" />
<option value="W29" />
<option value="E501" />
<option value="W29" />
<option value="E501" />
</list>
</option>
</inspection_tool>

View File

@ -2,10 +2,15 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="7106b959-52c5-4d26-9f5a-8703a78c8c29" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/challenges139-145/challenge-139.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/commands" afterDir="false" />
<change afterPath="$PROJECT_DIR$/challenges139-145/challenge-140.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/challenges139-145/challenge-141.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/challenges139-145/challenge-142.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/challenges139-145/sqlite-python-tutorial.py" afterDir="false" />
<change afterPath="$PROJECT_DIR$/useful-link" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/inspectionProfiles/Project_Default.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/inspectionProfiles/Project_Default.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/challenges139-145/challenge-139.py" beforeDir="false" afterPath="$PROJECT_DIR$/challenges139-145/challenge-139.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/commands" beforeDir="false" afterPath="$PROJECT_DIR$/commands" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
@ -14,21 +19,51 @@
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileEditorManager">
<leaf>
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/commands">
<entry file="file://$PROJECT_DIR$/challenges139-145/challenge-139.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="30">
<caret line="2" selection-start-line="2" selection-end-line="2" />
<state relative-caret-position="195">
<caret line="13" selection-start-line="13" selection-end-line="22" selection-end-column="15" />
<folding>
<element signature="e#0#14#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/challenges139-145/challenge-140.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="525">
<caret line="44" selection-start-line="38" selection-end-line="44" />
<folding>
<element signature="e#0#14#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/challenges139-145/challenge-141.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="285">
<caret line="31" column="54" selection-start-line="31" selection-start-column="40" selection-end-line="31" selection-end-column="54" />
<folding>
<element signature="e#0#14#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/challenges139-145/challenge-139.py">
<entry file="file://$PROJECT_DIR$/challenges139-145/challenge-142.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="180">
<caret line="12" selection-start-line="12" selection-end-line="12" />
<state relative-caret-position="202">
<caret line="29" lean-forward="true" selection-start-line="29" selection-end-line="29" />
<folding>
<element signature="e#0#14#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
@ -45,6 +80,7 @@
<component name="FindInProjectRecents">
<findStrings>
<find>upper</find>
<find>create_project</find>
</findStrings>
<dirStrings>
<dir>$PROJECT_DIR$</dir>
@ -57,16 +93,23 @@
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/challenges124-132/challenge-124.py" />
<option value="$PROJECT_DIR$/commands" />
<option value="$PROJECT_DIR$/challenges139-145/test.py" />
<option value="$PROJECT_DIR$/challenges139-145/sqlite-python-tutorial.py" />
<option value="$PROJECT_DIR$/challenges139-145/challenge-139.py" />
<option value="$PROJECT_DIR$/challenges139-145/challenge-149.py" />
<option value="$PROJECT_DIR$/commands" />
<option value="$PROJECT_DIR$/useful-link" />
<option value="$PROJECT_DIR$/challenges139-145/challenge-141.py" />
<option value="$PROJECT_DIR$/challenges139-145/challenge-140.py" />
<option value="$PROJECT_DIR$/challenges139-145/challenge-142.py" />
</list>
</option>
</component>
<component name="ProjectFrameBounds" extendedState="6">
<option name="x" value="473" />
<option name="y" value="4" />
<option name="width" value="958" />
<option name="height" value="1083" />
<option name="x" value="443" />
<option name="y" value="-28" />
<option name="width" value="956" />
<option name="height" value="1082" />
</component>
<component name="ProjectId" id="1O5wfQ7DfSfe4iwhiOQ9ZxAmGlm" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
@ -82,6 +125,11 @@
<item name="paython-by-learn" type="b2602c69:ProjectViewProjectNode" />
<item name="paython-by-learn" type="462c0819:PsiDirectoryNode" />
</path>
<path>
<item name="paython-by-learn" type="b2602c69:ProjectViewProjectNode" />
<item name="paython-by-learn" type="462c0819:PsiDirectoryNode" />
<item name="challenges139-145" type="462c0819:PsiDirectoryNode" />
</path>
</expand>
<select />
</subPane>
@ -125,7 +173,7 @@
</list>
</option>
</component>
<component name="RunManager" selected="Python.challenge-139">
<component name="RunManager" selected="Python.challenge-142">
<configuration default="true" type="PythonConfigurationType" factoryName="Python">
<module name="paython-by-learn" />
<option name="INTERPRETER_OPTIONS" value="" />
@ -148,91 +196,6 @@
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="challenge-121" type="PythonConfigurationType" factoryName="Python" temporary="true">
<module name="paython-by-learn" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/challenges118-123" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/challenges118-123/challenge-121.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="true" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="challenge-122" type="PythonConfigurationType" factoryName="Python" temporary="true">
<module name="paython-by-learn" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/challenges118-123" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/challenges118-123/challenge-122.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="true" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="challenge-123" type="PythonConfigurationType" factoryName="Python" temporary="true">
<module name="paython-by-learn" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/challenges118-123" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/challenges118-123/challenge-123.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="true" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="challenge-124" type="PythonConfigurationType" factoryName="Python" temporary="true">
<module name="paython-by-learn" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/challenges124-132" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/challenges124-132/challenge-124.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="true" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="challenge-139" type="PythonConfigurationType" factoryName="Python" temporary="true">
<module name="paython-by-learn" />
<option name="INTERPRETER_OPTIONS" value="" />
@ -252,13 +215,89 @@
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="challenge-140" type="PythonConfigurationType" factoryName="Python" temporary="true">
<module name="paython-by-learn" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/challenges139-145" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/challenges139-145/challenge-140.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="true" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="challenge-141" type="PythonConfigurationType" factoryName="Python" temporary="true">
<module name="paython-by-learn" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/challenges139-145" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/challenges139-145/challenge-141.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="true" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="challenge-142" type="PythonConfigurationType" factoryName="Python" temporary="true">
<module name="paython-by-learn" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/challenges139-145" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/challenges139-145/challenge-142.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="true" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="sqlite-python-tutorial" type="PythonConfigurationType" factoryName="Python" temporary="true">
<module name="paython-by-learn" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/challenges139-145" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/challenges139-145/sqlite-python-tutorial.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="true" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<recent_temporary>
<list>
<item itemvalue="Python.challenge-142" />
<item itemvalue="Python.challenge-141" />
<item itemvalue="Python.challenge-140" />
<item itemvalue="Python.challenge-139" />
<item itemvalue="Python.challenge-124" />
<item itemvalue="Python.challenge-123" />
<item itemvalue="Python.challenge-122" />
<item itemvalue="Python.challenge-121" />
<item itemvalue="Python.sqlite-python-tutorial" />
</list>
</recent_temporary>
</component>
@ -283,15 +322,9 @@
</component>
<component name="ToolWindowManager">
<frame x="-1" y="-4" width="1922" height="1085" extended-state="6" />
<editor active="true" />
<layout>
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.24986821" />
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.16552451" />
<window_info id="Structure" order="1" weight="0.25" />
<window_info anchor="bottom" id="Educational.CheckDetails" />
<window_info anchor="bottom" id="Version Control" />
<window_info anchor="bottom" id="Terminal" weight="0.3294979" />
<window_info anchor="bottom" id="Event Log" side_tool="true" />
<window_info anchor="bottom" id="Python Console" visible="true" weight="0.3294979" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" />
<window_info anchor="bottom" id="Run" order="2" />
@ -299,6 +332,11 @@
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="bottom" id="Version Control" order="7" />
<window_info anchor="bottom" id="Educational.CheckDetails" order="8" />
<window_info anchor="bottom" id="Terminal" order="9" weight="0.3294979" />
<window_info anchor="bottom" id="Event Log" order="10" side_tool="true" />
<window_info active="true" anchor="bottom" id="Python Console" order="11" visible="true" weight="0.31066945" />
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
<window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
@ -354,26 +392,104 @@
<MESSAGE value="8th day of python challenges 111" />
<MESSAGE value="8th day of python challenges 111-117" />
<MESSAGE value="9th day of python challenges 118-123" />
<option name="LAST_COMMIT_MESSAGE" value="9th day of python challenges 118-123" />
<MESSAGE value="9th day of python challenges 124-145" />
<option name="LAST_COMMIT_MESSAGE" value="9th day of python challenges 124-145" />
</component>
<component name="com.intellij.coverage.CoverageDataManagerImpl">
<SUITE FILE_PATH="coverage/paython_by_learn$challenge_039.coverage" NAME="challenge-039 Coverage Results" MODIFIED="1563282890417" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/challenges35-44" />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/challenges124-132/challenge-124.py">
<entry file="file://$PROJECT_DIR$/challenges124-132/challenge-124.py" />
<entry file="file://$PROJECT_DIR$/challenges139-145/python-by-example" />
<entry file="file://$PROJECT_DIR$/challenges118-123/challenge-123.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="184">
<caret line="44" column="12" selection-start-line="44" selection-start-column="12" selection-end-line="59" selection-end-column="57" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/challenges118-123/challenge-118.py">
<provider selected="true" editor-type-id="text-editor" />
</entry>
<entry file="file://$PROJECT_DIR$/useful-link">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="30">
<caret line="2" column="35" selection-start-line="2" selection-start-column="35" selection-end-line="2" selection-end-column="35" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/commands">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="30">
<caret line="2" selection-start-line="2" selection-end-line="2" />
<state relative-caret-position="45">
<caret line="3" selection-start-line="3" selection-end-line="3" selection-end-column="35" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/challenges88-95/challenge-091.py">
<provider selected="true" editor-type-id="text-editor" />
</entry>
<entry file="file://$PROJECT_DIR$/challenges88-95/challenge-092.py">
<provider selected="true" editor-type-id="text-editor">
<state>
<folding>
<element signature="e#0#19#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/challenges88-95/challenge-093.py">
<provider selected="true" editor-type-id="text-editor" />
</entry>
<entry file="file://$PROJECT_DIR$/challenges88-95/challenge-094.py">
<provider selected="true" editor-type-id="text-editor" />
</entry>
<entry file="file://$PROJECT_DIR$/challenges139-145/sqlite-python-tutorial.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="180">
<caret line="36" column="4" selection-start-line="36" selection-start-column="4" selection-end-line="41" selection-end-column="16" />
<folding>
<element signature="e#0#14#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/challenges139-145/challenge-139.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="180">
<caret line="12" selection-start-line="12" selection-end-line="12" />
<state relative-caret-position="195">
<caret line="13" selection-start-line="13" selection-end-line="22" selection-end-column="15" />
<folding>
<element signature="e#0#14#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/challenges139-145/challenge-140.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="525">
<caret line="44" selection-start-line="38" selection-end-line="44" />
<folding>
<element signature="e#0#14#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/challenges139-145/challenge-141.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="285">
<caret line="31" column="54" selection-start-line="31" selection-start-column="40" selection-end-line="31" selection-end-column="54" />
<folding>
<element signature="e#0#14#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/challenges139-145/challenge-142.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="202">
<caret line="29" lean-forward="true" selection-start-line="29" selection-end-line="29" />
<folding>
<element signature="e#0#14#0" expanded="true" />
</folding>
</state>
</provider>
</entry>

View File

@ -1,12 +1,54 @@
import sqlite3
from sqlite3 import Error
with sqlite3.connect('python-by-example') as db:
cursor = db.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS employees(
id integer PRIMARY KEY,
name text NOT NULL,
dept text NOT NULL,
salary integer);""")
cursor.execute("""INSERT INTO employees(id,name,dept,salary)
VALUES("1","Bob","Sales","25000")""")
db.commit()
def create_connection(db):
try:
conn = sqlite3.connect(db)
return conn
except Error as e:
print(e)
return None
def create_phone_book_table(conn):
sql_create_projects_table = """ CREATE TABLE IF NOT EXISTS PhoneBook (
id integer PRIMARY KEY,
first_name text NOT NULL,
surname text,
phone integer
); """
cur = conn.cursor()
cur.execute(sql_create_projects_table)
cur.close()
def create_phone_book_record(conn, phone_book):
sql = ''' INSERT INTO PhoneBook(first_name,surname,phone)
VALUES(?,?,?) '''
cur = conn.cursor()
cur.execute(sql, phone_book)
cur.close()
return cur.lastrowid
def main():
conn = create_connection('python-by-example.db')
with conn:
create_phone_book_table(conn)
phone_book_record_1 = ('Simon', 'Howels', '01223349752')
phone_book_record_2 = ('Karen', 'Philips', '01954295773')
phone_book_record_3 = ('Darren', 'Smith', '01583749012')
phone_book_record_4 = ('Anne', 'Jones', '01323567322')
phone_book_record_5 = ('Mark', 'Smith', '01223855534')
create_phone_book_record(conn, phone_book_record_1)
create_phone_book_record(conn, phone_book_record_2)
create_phone_book_record(conn, phone_book_record_3)
create_phone_book_record(conn, phone_book_record_4)
create_phone_book_record(conn, phone_book_record_5)
main()

View File

@ -0,0 +1,84 @@
import sqlite3
from sqlite3 import Error
def create_connection(db):
try:
conn = sqlite3.connect(db)
return conn
except Error as e:
print(e)
return None
def show_all(conn):
cur = conn.cursor()
cur.execute("SELECT * FROM PhoneBook")
rows = cur.fetchall()
for row in rows:
print(row)
def view_phone_book(conn, phone_id):
cur = conn.cursor()
cur.execute(str("SELECT * FROM PhoneBook WHERE id = " + str(phone_id)))
print(cur.fetchone())
cur.close()
def add_phone_book(conn, phone_book):
sql = ''' INSERT INTO PhoneBook(first_name,surname,phone)
VALUES(?,?,?) '''
cur = conn.cursor()
cur.execute(sql, phone_book)
print(cur.fetchone())
cur.close()
def surname_in_phone_book(conn, surname):
sql = "SELECT * FROM PhoneBook WHERE surname like '%" + surname + "%'"
cur = conn.cursor()
cur.execute(sql)
rows = cur.fetchall()
for row in rows:
print(row)
def delete_phone_book(conn, phone_id):
cur = conn.cursor()
cur.execute(str("DELETE FROM tasks WHERE id = " + str(phone_id)))
cur.close()
def main():
conn = create_connection('python-by-example.db')
with conn:
print('\n\nMain Menu\n\n\n')
print('1) View phone book ')
print('2) Add to phone book')
print('3) Search for surname')
print('4) Delete person from phone book')
print('5) Quit')
operation_ask = int(input('Enter your selection : '))
if operation_ask == 1:
ask_id = int(input('Enter id to show : '))
view_phone_book(conn, ask_id)
elif operation_ask == 2:
ask_name = str(input('Enter name : '))
ask_surname = str(input('Enter surname : '))
ask_phone = str(input('Enter phone : '))
phone_book = (ask_name, ask_surname, ask_phone)
add_phone_book(conn, phone_book)
elif operation_ask == 3:
ask_surname = str(input('Enter surname to show : '))
surname_in_phone_book(conn, ask_surname)
elif operation_ask == 4:
ask_id = int(input('Enter id to delete : '))
delete_phone_book(conn, ask_id)
else:
print('Invalid input, please try again.')
main()

View File

@ -0,0 +1,92 @@
import sqlite3
from sqlite3 import Error
def create_connection(db):
try:
conn = sqlite3.connect(db)
return conn
except Error as e:
print(e)
return None
def create_author_table(conn):
try:
sql_create_projects_table = """ CREATE TABLE IF NOT EXISTS authors (
name text PRIMARY KEY,
birth_place text
); """
cur = conn.cursor()
cur.execute(sql_create_projects_table)
cur.close()
except Error as e:
print(e)
def create_books_table(conn):
sql_create_projects_table = """ CREATE TABLE IF NOT EXISTS books (
id integer PRIMARY KEY,
title text NOT NULL,
author text,
published_date text
); """
cur = conn.cursor()
cur.execute(sql_create_projects_table)
cur.close()
def insert_to_authors(conn, authors):
cur = conn.cursor()
for author in authors:
sql = 'INSERT INTO authors (name, birth_place) VALUES (?, ?)'
cur.execute(sql, author)
conn.commit()
cur.close()
def insert_to_books(conn, books):
cur = conn.cursor()
for book in books:
sql = 'INSERT INTO books (title, author, published_date) VALUES (?, ?, ?)'
cur.execute(sql, book)
print(cur.fetchone())
cur.close()
def main():
conn = create_connection('BookInfo.db')
with conn:
create_author_table(conn)
create_books_table(conn)
authors = [
['Agatha Christie', 'Torquay'],
['Cecelia Ahern', 'Dublin'],
['J.K, Rowling', 'Bristol'],
['Oscar Wilde', 'Dublin']
]
insert_to_authors(conn, authors)
books = [
['De Profundis', 'Oscar Wilde', '1905'],
['Harry Potter and the chamber of secrets', 'J.K. Rowling', '1998'],
['Harry Potter and the prisoner or Azkaban', 'J.K. Rowling', '1999'],
['Lyrebird', 'Cecelia Ahren', '2017'],
['Murder At the Orient Express', 'Agatha Christie', '1934'],
['Perfect', 'Cecelia Ahren', '2017'],
['The marble collector', 'Cecelia Ahren', '2016'],
['The murder on the links', 'Agatha Christie', '1923'],
['The picture of Dorian Gray', 'Oscar Wilde', '1890'],
['The secret adversary', 'Agatha Christie', '1921'],
['The seven dials mystery', 'Agatha Christie', '1929'],
['The year i met you', 'Cecelia Ahren', '2014']
]
insert_to_books(conn, books)
main()

View File

@ -0,0 +1,50 @@
import sqlite3
from sqlite3 import Error
def create_connection(db):
try:
conn = sqlite3.connect(db)
return conn
except Error as e:
print(e)
return None
def show_all_authors(conn):
cur = conn.cursor()
cur.execute("SELECT * FROM authors")
rows = cur.fetchall()
for row in rows:
print(row)
def search_place_birth(conn, place):
cur = conn.cursor()
cur.execute(str("select books.title, books.published_date, authors.name from authors join books on books.author = authors.name where authors.birth_place = '" + place + "'"))
rows = cur.fetchall()
for row in rows:
print(row)
cur.close()
def main():
conn = create_connection('BookInfo.db')
with conn:
print('\n\nMain Menu\n\n\n')
print('1) List authors ')
print('2) Search place of birth')
print('3) Quit')
operation_ask = int(input('Enter your selection : '))
if operation_ask == 1:
show_all_authors(conn)
elif operation_ask == 2:
ask_place = str(input('Enter place of birth : '))
search_place_birth(conn, ask_place)
else:
print('Invalid input, please try again.')
main()

View File

@ -0,0 +1,192 @@
import sqlite3
from sqlite3 import Error
""""""""""""""""""" CREATING SQLITE DATABASE """""""""""""""""""""
def create_connection(db_file):
""" create a database connection to the SQLite database
specified by db_file
:param db_file: database file
:return: Connection object or None
"""
try:
conn = sqlite3.connect(db_file)
return conn
except Error as e:
print(e)
# finally:
# conn.close()
return None
# if __name__ == '__main__':
# create_connection('python-by-example.db')
"""""""""""""""" CREATE TABLE """""""""""""""
def create_table(conn, create_table_sql):
""" create a table from the create_table_sql statement
:param conn: Connection object
:param create_table_sql: a CREATE TABLE statement
:return:
"""
try:
c = conn.cursor()
c.execute(create_table_sql)
c.close()
except Error as e:
print(e)
def create_project(conn, project):
"""
Create a new project into the projects table
:param conn:
:param project:
:return: project id
"""
sql = ''' INSERT INTO projects(name,begin_date,end_date)
VALUES(?,?,?) '''
cur = conn.cursor()
cur.execute(sql, project)
cur.close()
return cur.lastrowid
def create_task(conn, task):
"""
Create a new task
:param conn:
:param task:
:return:
"""
sql = ''' INSERT INTO tasks(name,priority,status_id,project_id,begin_date,end_date)
VALUES(?,?,?,?,?,?) '''
cur = conn.cursor()
cur.execute(sql, task)
cur.close()
return cur.lastrowid
def select_all_tasks(conn):
"""
Query all rows in the tasks table
:param conn: the Connection object
:return:
"""
cur = conn.cursor()
cur.execute("SELECT * FROM tasks")
rows = cur.fetchall()
for row in rows:
print(row)
def select_task_by_priority(conn, priority):
"""
Query tasks by priority
:param conn: the Connection object
:param priority:
:return:
"""
cur = conn.cursor()
cur.execute("SELECT * FROM tasks WHERE priority=?", (priority,))
rows = cur.fetchall()
for row in rows:
print(row)
def update_task(conn, task):
"""
update priority, begin_date, and end date of a task
:param conn:
:param task:
:return: project id
"""
sql = ''' UPDATE tasks
SET priority = ? ,
begin_date = ? ,
end_date = ?
WHERE id = ?'''
cur = conn.cursor()
cur.execute(sql, task)
cur.close()
def delete_task(conn, task):
"""
delete a task
:param conn:
:param task:
:return: null
"""
cur = conn.cursor()
cur.execute(str("DELETE FROM tasks WHERE id = " + str(task)))
cur.close()
def main():
database = 'python-by-example.db'
# sql_create_projects_table = """ CREATE TABLE IF NOT EXISTS projects (
# id integer PRIMARY KEY,
# name text NOT NULL,
# begin_date text,
# end_date text
# ); """
#
# sql_create_tasks_table = """CREATE TABLE IF NOT EXISTS tasks (
# id integer PRIMARY KEY,
# name text NOT NULL,
# priority integer,
# status_id integer NOT NULL,
# project_id integer NOT NULL,
# begin_date text NOT NULL,
# end_date text NOT NULL,
# FOREIGN KEY (project_id) REFERENCES projects (id)
# );"""
#
# # create a database connection
conn = create_connection(database)
# if conn is not None:
# # create projects table
# create_table(conn, sql_create_projects_table)
# # create tasks table
# create_table(conn, sql_create_tasks_table)
# else:
# print("Error! cannot create the database connection.")
with conn:
# # create a new project
# project = ('Cool App with SQLite & Python', '2015-01-01', '2015-01-30');
# project_id = create_project(conn, project)
#
# # tasks
# task_1 = ('Analyze the requirements of the app', 1, 1, project_id, '2015-01-01', '2015-01-02')
task_2 = ('Confirm with user about the top requirements', 1, 1, 1, '2015-01-03', '2015-01-05')
#
# # create tasks
# create_task(conn, task_1)
create_task(conn, task_2)
# update_task(conn, (2, '2015-01-04', '2015-01-06',2))
# delete_task(conn, 2)
# print("1. Query task by priority:")
# select_task_by_priority(conn, 1)
#
# print("2. Query all tasks")
# select_all_tasks(conn)
if __name__ == '__main__':
main()

View File

@ -1,3 +1,7 @@
sudo apt-get install python3-tk
sudo apt install sqlite
------ challenges139-145 ----------
$sudo apt install sqlite

4
useful-link Normal file
View File

@ -0,0 +1,4 @@
for CSV challenges
------ challenges139-145 ----------
- http://www.sqlitetutorial.net/sqlite-python/