Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Python-100-Days
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
huangkq
Python-100-Days
Commits
83e7f484
Commit
83e7f484
authored
Jun 28, 2018
by
jackfrued
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
'更新了PyMySQL实例代码'
parent
eade73fd
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
81 additions
and
24 deletions
+81
-24
main.py
Day36-40/code/contact/main.py
+49
-24
test02.py
Day36-40/code/contact/test02.py
+32
-0
No files found.
Day36-40/code/contact/main.py
View file @
83e7f484
...
@@ -15,11 +15,24 @@ SELECT_CONTACTERS = """
...
@@ -15,11 +15,24 @@ SELECT_CONTACTERS = """
select conid as id, conname as name, contel as tel, conemail as email
select conid as id, conname as name, contel as tel, conemail as email
from tb_contacter limit
%
s offset
%
s
from tb_contacter limit
%
s offset
%
s
"""
"""
SELECT_CONTACTERS_BY_NAME
=
"""
select conid as id, conname as name, contel as tel, conemail as email
from tb_contacter where conname like
%
s
"""
COUNT_CONTACTERS
=
"""
COUNT_CONTACTERS
=
"""
select count(conid) as total from tb_contacter
select count(conid) as total from tb_contacter
"""
"""
class
Contacter
(
object
):
def
__init__
(
self
,
id
,
name
,
tel
,
email
):
self
.
id
=
id
self
.
name
=
name
self
.
tel
=
tel
self
.
email
=
email
def
input_contacter_info
():
def
input_contacter_info
():
name
=
input
(
'姓名: '
)
name
=
input
(
'姓名: '
)
tel
=
input
(
'手机: '
)
tel
=
input
(
'手机: '
)
...
@@ -39,10 +52,10 @@ def add_new_contacter(con):
...
@@ -39,10 +52,10 @@ def add_new_contacter(con):
print
(
'添加联系人失败!'
)
print
(
'添加联系人失败!'
)
def
delete_contacter
(
con
,
contacter
_id
):
def
delete_contacter
(
con
,
contacter
):
try
:
try
:
with
con
.
cursor
()
as
cursor
:
with
con
.
cursor
()
as
cursor
:
if
cursor
.
execute
(
DELETE_CONTACTER
,
(
contacter
_
id
,
))
==
1
:
if
cursor
.
execute
(
DELETE_CONTACTER
,
(
contacter
.
id
,
))
==
1
:
print
(
'联系人已经删除!'
)
print
(
'联系人已经删除!'
)
except
pymysql
.
MySQLError
as
err
:
except
pymysql
.
MySQLError
as
err
:
print
(
err
)
print
(
err
)
...
@@ -51,14 +64,14 @@ def delete_contacter(con, contacter_id):
...
@@ -51,14 +64,14 @@ def delete_contacter(con, contacter_id):
def
edit_contacter_info
(
con
,
contacter
):
def
edit_contacter_info
(
con
,
contacter
):
name
,
tel
,
email
=
input_contacter_info
()
name
,
tel
,
email
=
input_contacter_info
()
contacter
[
'name'
]
=
name
or
contacter
[
'name'
]
contacter
.
name
=
name
or
contacter
.
name
contacter
[
'tel'
]
=
tel
or
contacter
[
'tel'
]
contacter
.
tel
=
tel
or
contacter
.
tel
contacter
[
'email'
]
=
email
or
contacter
[
'email'
]
contacter
.
email
=
email
or
contacter
.
email
try
:
try
:
with
con
.
cursor
()
as
cursor
:
with
con
.
cursor
()
as
cursor
:
if
cursor
.
execute
(
UPDATE_CONTACTER
,
if
cursor
.
execute
(
UPDATE_CONTACTER
,
(
contacter
[
'name'
],
contacter
[
'tel'
]
,
(
contacter
.
name
,
contacter
.
tel
,
contacter
[
'email'
],
contacter
[
'id'
]
))
==
1
:
contacter
.
email
,
contacter
.
id
))
==
1
:
print
(
'联系人信息已经更新!'
)
print
(
'联系人信息已经更新!'
)
except
pymysql
.
MySQLError
as
err
:
except
pymysql
.
MySQLError
as
err
:
print
(
err
)
print
(
err
)
...
@@ -66,16 +79,30 @@ def edit_contacter_info(con, contacter):
...
@@ -66,16 +79,30 @@ def edit_contacter_info(con, contacter):
def
show_contacter_detail
(
con
,
contacter
):
def
show_contacter_detail
(
con
,
contacter
):
print
(
'姓名:'
,
contacter
[
'name'
]
)
print
(
'姓名:'
,
contacter
.
name
)
print
(
'手机号:'
,
contacter
[
'tel'
]
)
print
(
'手机号:'
,
contacter
.
tel
)
print
(
'邮箱:'
,
contacter
[
'email'
]
)
print
(
'邮箱:'
,
contacter
.
email
)
choice
=
input
(
'是否编辑联系人信息?(yes|no)'
)
choice
=
input
(
'是否编辑联系人信息?(yes|no)'
)
if
choice
==
'yes'
:
if
choice
==
'yes'
:
edit_contacter_info
(
con
,
contacter
)
edit_contacter_info
(
con
,
contacter
)
else
:
else
:
choice
=
input
(
'是否删除联系人信息?(yes|no)'
)
choice
=
input
(
'是否删除联系人信息?(yes|no)'
)
if
choice
==
'yes'
:
if
choice
==
'yes'
:
delete_contacter
(
con
,
contacter
[
'id'
])
delete_contacter
(
con
,
contacter
)
def
show_search_result
(
con
,
cursor
):
contacters_list
=
[]
for
index
,
row
in
enumerate
(
cursor
.
fetchall
()):
contacter
=
Contacter
(
**
row
)
contacters_list
.
append
(
contacter
)
print
(
'[
%
d]:
%
s'
%
(
index
,
contacter
.
name
))
if
len
(
contacters_list
)
>
0
:
choice
=
input
(
'是否查看联系人详情?(yes|no)'
)
if
choice
.
lower
()
==
'yes'
:
index
=
int
(
input
(
'请输入编号: '
))
if
0
<=
index
<
cursor
.
rowcount
:
show_contacter_detail
(
con
,
contacters_list
[
index
])
def
find_all_contacters
(
con
):
def
find_all_contacters
(
con
):
...
@@ -87,15 +114,7 @@ def find_all_contacters(con):
...
@@ -87,15 +114,7 @@ def find_all_contacters(con):
while
True
:
while
True
:
cursor
.
execute
(
SELECT_CONTACTERS
,
cursor
.
execute
(
SELECT_CONTACTERS
,
(
size
,
(
page
-
1
)
*
size
))
(
size
,
(
page
-
1
)
*
size
))
contacters_list
=
[]
show_search_result
(
con
,
cursor
)
for
index
,
row
in
enumerate
(
cursor
.
fetchall
()):
contacters_list
.
append
(
row
)
print
(
'[
%
d]:
%
s'
%
(
index
,
row
[
'name'
]))
choice
=
input
(
'是否查看联系人详情?(yes|no)'
)
if
choice
.
lower
()
==
'yes'
:
index
=
int
(
input
(
'请输入编号: '
))
if
0
<=
index
<
cursor
.
rowcount
:
show_contacter_detail
(
con
,
contacters_list
[
index
])
if
page
*
size
<
total
:
if
page
*
size
<
total
:
choice
=
input
(
'继续查看下一页?(yes|no)'
)
choice
=
input
(
'继续查看下一页?(yes|no)'
)
if
choice
.
lower
()
==
'yes'
:
if
choice
.
lower
()
==
'yes'
:
...
@@ -103,15 +122,21 @@ def find_all_contacters(con):
...
@@ -103,15 +122,21 @@ def find_all_contacters(con):
else
:
else
:
break
break
else
:
else
:
print
(
'没有下一页记录
啦
!'
)
print
(
'没有下一页记录!'
)
break
break
except
pymysql
.
MySQLError
as
err
:
except
pymysql
.
MySQLError
as
err
:
print
(
err
)
print
(
err
)
def
find_contacters_by_name
(
con
):
def
find_contacters_by_name
(
con
):
pass
name
=
input
(
'联系人姓名: '
)
try
:
with
con
.
cursor
()
as
cursor
:
cursor
.
execute
(
SELECT_CONTACTERS_BY_NAME
,
(
'
%
'
+
name
+
'
%
'
,
))
show_search_result
(
con
,
cursor
)
except
pymysql
.
MySQLError
as
err
:
print
(
err
)
def
find_contacters
(
con
):
def
find_contacters
(
con
):
...
@@ -129,7 +154,7 @@ def find_contacters(con):
...
@@ -129,7 +154,7 @@ def find_contacters(con):
def
main
():
def
main
():
con
=
pymysql
.
connect
(
host
=
'
10.7.185.126
'
,
port
=
3306
,
con
=
pymysql
.
connect
(
host
=
'
localhost
'
,
port
=
3306
,
user
=
'root'
,
passwd
=
'123456'
,
user
=
'root'
,
passwd
=
'123456'
,
db
=
'contact'
,
charset
=
'utf8'
,
db
=
'contact'
,
charset
=
'utf8'
,
autocommit
=
True
,
autocommit
=
True
,
...
...
Day36-40/code/contact/test02.py
0 → 100644
View file @
83e7f484
class
Student
(
object
):
def
__init__
(
self
,
id
,
name
,
age
,
sex
):
self
.
id
=
id
self
.
name
=
name
self
.
age
=
age
self
.
sex
=
sex
def
study
(
self
,
course_name
):
print
(
f
'{self.name}正在学习{course_name}.'
)
def
watch_av
(
self
):
if
self
.
age
>=
18
:
print
(
f
'{self.name}正在观看岛国片.'
)
else
:
print
(
f
'{self.name}只能看《熊出没》.'
)
def
main
():
dict1
=
{
'id'
:
1001
,
'name'
:
'王大锤'
,
'age'
:
15
,
'sex'
:
True
}
stu
=
Student
(
**
dict1
)
stu
.
study
(
'Python程序设计'
)
stu
.
watch_av
()
if
__name__
==
'__main__'
:
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment