forked from edengzv/Python-MySQLdb-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysqldb_test.py
100 lines (87 loc) · 2.34 KB
/
mysqldb_test.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/python
#coding=utf8
#author:kantian
#date:2013-10-20
import sys
import MySQLdb
#define the connection param
hostname='localhost'
username='root'
password='gnefgnehz'
dbname='test'
tbname='mytest'
port= 3306
charset='utf8'
try:
conn = MySQLdb.connect(
host=hostname,
user=username,
passwd=password,
db=dbname,
port=port,
charset=charset,
)
cursor = conn.cursor()
#如果表已经存在,则删除·
cursor.execute("""DROP TABLE IF EXISTS %s""" % (tbname,))
#创建表
cursor.execute("""CREATE TABLE IF NOT EXISTS %s(
id int(11) not null auto_increment,
name char(20) not null,
PRIMARY KEY(id)
)ENGINE=MyISAM;
""" % (tbname,))
#向刚创建的表中插入数据
cursor.execute("""
INSERT INTO %s (name)
VALUES
('Monday'),('Thesday'),('Wednesday'),('Thursday'),('Friday'),('Saturday'),('Sunday');
""" % (tbname,))
#获取所有查询结果
print 'fetchall():'
cursor.execute("""
SELECT * FROM %s;
""" % (tbname,))
res = cursor.fetchall()
for k in res:
print 'id %d:%s' % (k[0],k[1])
#获取3条查询结果
print 'fetchmany():N=3'
cursor.execute("""
SELECT * FROM %s;
""" % (tbname,))
res = cursor.fetchmany(3)
for k in res:
print 'id %d:%s' % (k[0],k[1])
#获取一条查询结果
print 'fetchone()'
cursor.execute("""
SELECT * FROM %s;
""" % (tbname,))
res = cursor.fetchone()
print res[0],res[1]
print cursor.rowcount
#删除一条数据
cursor.execute("""
DELETE FROM %s WHERE id = 1;
""" % (tbname,))
print cursor.rowcount
#更新一条数据
cursor.execute("""
UPDATE %s SET name='Congratulations!' WHERE id=2
""" % (tbname,))
cursor.close()
#创建一个字典型的游标,这样可以用名称访问,而不通过下表访问
cursor = conn.cursor(MySQLdb.cursors.DictCursor)
cursor.execute("""
SELECT * FROM %s WHERE id = 5;
""" % (tbname,))
row = cursor.fetchone()
print row['name']
#关闭游标
cursor.close()
#关闭连接
conn.close()
except MySQLdb.Error,e:
print 'mysql error %d:%s',(e.args[0],e.args[1])
sys.exit()