Skip to content

Commit 2dc83ab

Browse files
committed
add demo
0 parents  commit 2dc83ab

File tree

8 files changed

+265
-0
lines changed

8 files changed

+265
-0
lines changed

.gitignore

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Python template
3+
# Byte-compiled / optimized / DLL files
4+
__pycache__/
5+
*.py[cod]
6+
*$py.class
7+
8+
# C extensions
9+
*.so
10+
11+
# Distribution / packaging
12+
.Python
13+
build/
14+
develop-eggs/
15+
dist/
16+
downloads/
17+
eggs/
18+
.eggs/
19+
lib/
20+
lib64/
21+
parts/
22+
sdist/
23+
var/
24+
wheels/
25+
pip-wheel-metadata/
26+
share/python-wheels/
27+
*.egg-info/
28+
.installed.cfg
29+
*.egg
30+
MANIFEST
31+
32+
# PyInstaller
33+
# Usually these files are written by a python script from a template
34+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
35+
*.manifest
36+
*.spec
37+
38+
# Installer logs
39+
pip-log.txt
40+
pip-delete-this-directory.txt
41+
42+
# Unit test / coverage reports
43+
htmlcov/
44+
.tox/
45+
.nox/
46+
.coverage
47+
.coverage.*
48+
.cache
49+
nosetests.xml
50+
coverage.xml
51+
*.cover
52+
*.py,cover
53+
.hypothesis/
54+
.pytest_cache/
55+
cover/
56+
57+
# Translations
58+
*.mo
59+
*.pot
60+
61+
# Django stuff:
62+
*.log
63+
local_settings.py
64+
db.sqlite3
65+
db.sqlite3-journal
66+
67+
# Flask stuff:
68+
instance/
69+
.webassets-cache
70+
71+
# Scrapy stuff:
72+
.scrapy
73+
74+
# Sphinx documentation
75+
docs/_build/
76+
77+
# PyBuilder
78+
.pybuilder/
79+
target/
80+
81+
# Jupyter Notebook
82+
.ipynb_checkpoints
83+
84+
# IPython
85+
profile_default/
86+
ipython_config.py
87+
88+
# pyenv
89+
# For a library or package, you might want to ignore these files since the code is
90+
# intended to run in multiple environments; otherwise, check them in:
91+
# .python-version
92+
93+
# pipenv
94+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
95+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
96+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
97+
# install all needed dependencies.
98+
#Pipfile.lock
99+
100+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
101+
__pypackages__/
102+
103+
# Celery stuff
104+
celerybeat-schedule
105+
celerybeat.pid
106+
107+
# SageMath parsed files
108+
*.sage.py
109+
110+
# Environments
111+
.env
112+
.venv
113+
env/
114+
venv/
115+
ENV/
116+
env.bak/
117+
venv.bak/
118+
119+
# Spyder project settings
120+
.spyderproject
121+
.spyproject
122+
123+
# Rope project settings
124+
.ropeproject
125+
126+
# mkdocs documentation
127+
/site
128+
129+
# mypy
130+
.mypy_cache/
131+
.dmypy.json
132+
dmypy.json
133+
134+
# Pyre type checker
135+
.pyre/
136+
137+
# pytype static type analyzer
138+
.pytype/
139+
140+
# Cython debug symbols
141+
cython_debug/
142+
143+
144+
/.idea

demo1.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import pymysql
2+
3+
db = pymysql.connect(host='localhost', user='root', password=None, port=3306)
4+
cursor = db.cursor()
5+
cursor.execute('SELECT VERSION()')
6+
data = cursor.fetchone()
7+
print('Database version:', data)
8+
cursor.execute("CREATE DATABASE spiders DEFAULT CHARACTER SET utf8mb4")
9+
db.close()

demo2.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import pymysql
2+
3+
db = pymysql.connect(host='localhost', user='root', password=None, port=3306, db='spiders')
4+
cursor = db.cursor()
5+
sql = 'CREATE TABLE IF NOT EXISTS students (id VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, age INT NOT NULL, PRIMARY KEY (id))'
6+
cursor.execute(sql)
7+
db.close()

demo3.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pymysql
2+
3+
4+
id = '20120001'
5+
user = 'Bob'
6+
age = 20
7+
8+
db = pymysql.connect(host='localhost', user='root', password=None, port=3306, db='spiders')
9+
cursor = db.cursor()
10+
sql = 'INSERT INTO students(id, name, age) values(%ss, %ss, %ss)'
11+
try:
12+
cursor.execute(sql, (id, user, age))
13+
db.commit()
14+
except:
15+
db.rollback()
16+
db.close()

demo4.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pymysql
2+
3+
data = {
4+
'id': '20120001',
5+
'name': 'Bob',
6+
'age': 20
7+
}
8+
table = 'students'
9+
keys = ', '.join(data.keys())
10+
values = ', '.join(['%s'] * len(data))
11+
db = pymysql.connect(host='localhost', user='root', password=None, port=3306, db='spiders')
12+
cursor = db.cursor()
13+
sql = 'INSERT INTO {table}({keys}) VALUES ({values})'.format(table=table, keys=keys, values=values)
14+
try:
15+
if cursor.execute(sql, tuple(data.values())):
16+
print('Successful')
17+
db.commit()
18+
except:
19+
print('Failed')
20+
db.rollback()
21+
db.close()

demo5.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import pymysql
2+
3+
data = {
4+
'id': '20120001',
5+
'name': 'Bob',
6+
'age': 21
7+
}
8+
9+
table = 'students'
10+
keys = ', '.join(data.keys())
11+
values = ', '.join(['%s'] * len(data))
12+
db = pymysql.connect(host='localhost', user='root', password=None, port=3306, db='spiders')
13+
cursor = db.cursor()
14+
sql = 'INSERT INTO {table}({keys}) VALUES ({values}) ON DUPLICATE KEY UPDATE'.format(table=table, keys=keys, values=values)
15+
update = ','.join(["{key} = %ss".format(key=key) for key in data])
16+
sql += update
17+
try:
18+
if cursor.execute(sql, tuple(data.values())*2):
19+
print('Successful')
20+
db.commit()
21+
except:
22+
print('Failed')
23+
db.rollback()
24+
db.close()

demo6.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pymysql
2+
3+
table = 'students'
4+
condition = 'age > 20'
5+
6+
db = pymysql.connect(host='localhost', user='root', password=None, port=3306, db='spiders')
7+
cursor = db.cursor()
8+
sql = 'DELETE FROM {table} WHERE {condition}'.format(table=table, condition=condition)
9+
try:
10+
cursor.execute(sql)
11+
db.commit()
12+
except:
13+
db.rollback()
14+
15+
db.close()

demo7.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import pymysql
2+
3+
sql = 'SELECT * FROM students WHERE age >= 20'
4+
5+
db = pymysql.connect(host='localhost', user='root', password=None, port=3306, db='spiders')
6+
cursor = db.cursor()
7+
try:
8+
cursor.execute(sql)
9+
print('Count:', cursor.rowcount)
10+
one = cursor.fetchone()
11+
print('One:', one)
12+
results = cursor.fetchall()
13+
print('Results:', results)
14+
print('Results Type:', type(results))
15+
for row in results:
16+
print(row)
17+
except:
18+
print('Error')
19+
20+
sql = 'SELECT * FROM students WHERE age >= 20'
21+
try:
22+
cursor.execute(sql)
23+
print('Count:', cursor.rowcount)
24+
row = cursor.fetchone()
25+
while row:
26+
print('Row:', row)
27+
row = cursor.fetchone()
28+
except:
29+
print('Error')

0 commit comments

Comments
 (0)