Skip to content

Commit 176a654

Browse files
committed
Added sample MySQL database connection (class-based)
1 parent 1d70e9a commit 176a654

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

mysqldb_class_instance_sample.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/env python
2+
3+
"""Sample instantiation of db class
4+
5+
This script is just a sample for dealing with a database in Python
6+
7+
== Prerequisites ==
8+
9+
The following commands are executed using the MySQL console.
10+
11+
- Create a new database 'pytest':
12+
mysql> CREATE DATABASE pytest;
13+
14+
- Create a test user 'pyuser':
15+
mysql> CREATE USER 'pyuser'@'localhost' IDENTIFIED by 'pyt3ster';
16+
17+
- Grant rights to user:
18+
mysql> GRANT ALL ON pytest.* TO 'pyuser'@'localhost';
19+
20+
For creating and populating the demo table, one should run `mysql-sample`.
21+
22+
TODO: Put the connect() code into a new class or maybe into GeneralDBCmd (refactor name!)
23+
24+
"""
25+
__author__ = 'Jan Beilicke <dev@jotbe-fx.de>'
26+
__date__ = '2011-04-01'
27+
28+
import MySQLdb as mdb
29+
from mysqldb_class_sample import GeneralDBCmd
30+
31+
if __name__ == '__main__':
32+
print '__name__: ', __name__
33+
34+
dbhost = '127.0.0.1'
35+
dbuser = 'pyuser'
36+
dbpwd = 'pyt3ster'
37+
db = 'pytest'
38+
tbl = 'tbl_test'
39+
40+
c = mdb.connect(dbhost, dbuser, dbpwd, db)
41+
42+
q = GeneralDBCmd(c, tbl)
43+
q.debug = True
44+
45+
print dir(q)
46+
print 'MySQL-Version: %s' % q.get_mysql_version()
47+
48+
print 'Querying field 1 of row 1 \'q[1][1]\': ', q[1][1]
49+
50+
for x in xrange(0,2):
51+
print 'Name (range) #%s: %s' % (x, q[x][1])
52+
53+
for x in q:
54+
print 'Name: %s' % (x[1],)

mysqldb_class_sample.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/env python
2+
3+
"""Sample database class for MySQL
4+
5+
This script is just a sample for dealing with a database class
6+
7+
"""
8+
__author__ = 'Jan Beilicke <dev@jotbe-fx.de>'
9+
__date__ = '2011-04-01'
10+
11+
import MySQLdb as mdb
12+
13+
class GeneralDBCmd:
14+
15+
""" Provide general database commands. """
16+
17+
def __init__(self, dbcon, tbl):
18+
self.dbcon = dbcon
19+
self.dbcur = self.dbcon.cursor()
20+
self.tbl = tbl
21+
self.debug = True
22+
def __getitem__(self, item):
23+
self.dbcur.execute('''
24+
SELECT * FROM %s LIMIT %s, 1;
25+
''' % (self.tbl, item))
26+
return self.dbcur.fetchone()
27+
def _query(self, q):
28+
""" Display the query in debug mode and execute it. """
29+
if self.debug: print 'Query: %s' % (q)
30+
self.dbcur.execute(q)
31+
def __iter__(self):
32+
""" Create a data set and return an iterator (self). """
33+
q = """
34+
SELECT * FROM %s
35+
""" % (self.tbl)
36+
self._query(q)
37+
# return iterator object with 'next()' method
38+
return self
39+
def next(self):
40+
""" Return the next item in dataset or tell Python to stop. """
41+
r = self.dbcur.fetchone()
42+
if not r:
43+
raise StopIteration
44+
return r
45+
def get_mysql_version(self):
46+
"""Return the MySQL version of the server"""
47+
q = """
48+
SELECT VERSION();
49+
"""
50+
self._query(q)
51+
return self.dbcur.fetchone()

0 commit comments

Comments
 (0)