-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmariadb.py
More file actions
98 lines (59 loc) · 1.29 KB
/
mariadb.py
File metadata and controls
98 lines (59 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python
import pymysql
db = pymysql.connect(host='192.168.1.101', port=3306, user='veli', passwd='1234', db='test1')
cursor = db.cursor()
cursor.execute("SELECT VERSION()")
data = cursor.fetchone()
print ("Database version : %s " % data)
# create table
cursor = db.cursor()
cursor.execute("DROP TABLE IF EXISTS kisi")
sql = """CREATE TABLE kisi (
isim CHAR(20) NOT NULL,
soyad CHAR(20),
yaş INT )"""
cursor.execute(sql)
db.close()
# insert
cursor = db.cursor()
sql = """INSERT INTO kisi(isim,
soyad, yas)
VALUES ('Ali', 'Akkirman', 28)"""
cursor.execute(sql)
db.commit()
db.close()
# read
cursor = db.cursor()
sql = "SELECT * FROM kisi \
WHERE yas > '%d'" % (20)
try:
cursor.execute(sql)
sonuclar = cursor.fetchall()
for veri in sonuclar:
isim = veri[0]
soyad = veri[1]
yas = veri[2]
print ("isim = %s,soyad = %s,yas = %d" % \
(isim, soyad, yas ))
except:
print ("Herhangi bir veriye ulaşılamadı")
db.close()
# update
cursor = db.cursor()
sql = "UPDATE kisi SET yas = yas + 1 \
WHERE yas = '%d'" % (20)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
db.close()
# delete
cursor = db.cursor()
sql = "DELETE FROM kisi WHERE yas > '%d'" % (25)
try:
cursor.execute(sql)
db.commit()
except:
db.rollback()
db.close()