Skip to content

Commit 59bc806

Browse files
authored
Update phonebook_sqlite3.py
1 parent f9c2d3b commit 59bc806

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

phonebook_sqlite3.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,58 @@
11
import sqlite3
2-
2+
3+
table_name = "phonebook"
34
conn = sqlite3.connect("phonebook.db")
45
cur = conn.cursor()
56

67
def create_table():
78
global conn
89
try:
9-
conn.execute('create table phonebook (no integer primary key autoincrement, name text, hpno text)')
10-
conn.commit()
11-
print("DB(테이블) 생성 성공")
10+
11+
cur.execute("select * from sqlite_master where type='table' and tbl_name='phonebook'")
12+
rows = cur.fetchall()
13+
14+
if rows:
15+
print(f"DB(테이블) {table_name}이 이미 존재합니다. ")
16+
else:
17+
conn.execute('create table phonebook(no integer primary key autoincrement, name text, hpno text)')
18+
conn.commit()
19+
print("DB(테이블) 생성 성공")
1220
except:
1321
print("DB(테이블) 생성 실패")
22+
1423
print_menu()
1524
return
1625

26+
1727
def print_menu():
1828
print()
1929
print("="*60)
20-
print("1. 조회, 2. 입력, 3. 수정, 4. 삭제, 5. 종료, 6. DB(테이블) 생성")
30+
print("1. 조회, 2. 입력, 3. 수정, 4. 삭제, 5. 종료, 6. DB생성")
2131
print("="*60)
2232

33+
2334
def phonebook_search():
2435
global conn, cur
2536
print("1. 조회")
2637
a = input("조회할 이름 또는 전화번호를 입력하세요: ").strip()
2738

28-
cur.execute("select * from phonebook where (name like '%"+a+"%' or hpno like '%"+a+"%') ")
39+
cur.execute("select * from phonebook where (name like '%"+a+"%' or hpno like '%"+a+"%')")
2940
rows = cur.fetchall()
41+
3042
idx = 0
3143
for row in rows:
3244
print("[{}] {} {}".format(row[0], row[1], row[2]))
3345
idx += 1
34-
46+
3547
if idx == 0:
3648
print("조회된 자료가 없습니다.")
3749

3850
print_menu()
3951
return
4052

53+
4154
def phonebook_insert():
42-
global phonebook
55+
global conn, cur
4356
print("2. 입력")
4457
a = input("입력할 이름을 입력하세요: ").strip()
4558
b = input("입력할 전화번호를 입력하세요: ").strip()
@@ -49,17 +62,18 @@ def phonebook_insert():
4962
return
5063

5164
try:
52-
cur.execute('insert into phonebook (name, hpno) values(?, ?)', (a, b))
65+
cur.execute('insert into phonebook (name, hpno) values (?, ?)', (a, b))
5366
conn.commit()
67+
5468
print("{} {} 입력 성공".format(a, b))
5569
except:
5670
print("{} {} 입력 실패".format(a, b))
57-
71+
5872
print_menu()
5973
return
6074

6175
def phonebook_update():
62-
global phonebook
76+
global conn, cur
6377
print("3. 수정")
6478
a = input("이름을 입력하세요: ").strip()
6579
b = input("변경할 전화번호를 입력하세요: ").strip()
@@ -79,7 +93,7 @@ def phonebook_update():
7993
return
8094

8195
def phonebook_delete():
82-
global phonebook
96+
global conn, cur
8397
print("4. 삭제")
8498
a = input("삭제할 전화번호를 입력하세요: ").strip()
8599
if a == "":
@@ -96,29 +110,21 @@ def phonebook_delete():
96110

97111
print_menu()
98112
return
99-
100-
101-
"""
102-
[
103-
['홍길동','010-1234-5678'],
104-
['김철수','010-1234-5678'],
105-
['김대한','010-1111-2222']
106-
]
107-
"""
113+
108114

109115
print("파이썬 전화번호부 Ver 1.0")
110116
print_menu()
111117

112118
while True:
113-
m = input("기능을 선택하세요.(1~5 입력) ").strip()
119+
m = input("기능을 선택하세요.(1~6 입력) ").strip()
114120
if m == "": continue
115121

116122
if m == "1":
117123
phonebook_search()
118124

119125
elif m == "2":
120126
phonebook_insert()
121-
127+
122128
elif m == "3":
123129
phonebook_update()
124130

@@ -129,4 +135,7 @@ def phonebook_delete():
129135
break
130136

131137
elif m == "6":
132-
create_table()
138+
create_table()
139+
140+
cur.close()
141+
conn.close()

0 commit comments

Comments
 (0)