49 | Please activate JavaScript to enable the search 50 | functionality. 51 |
52 |54 | From here you can search these documents. Enter your search 55 | words into the box below and click "search". Note that the search 56 | function will automatically search for all of the words. Pages 57 | containing fewer words won't appear in the result list. 58 |
59 | 64 | 65 |PySQLPool is at the heart a Thread Safe MySQL Connection Pooling library for use with the MySQLDB Python bindings.
51 |Part of the PySQLPool is the MySQL Query class that handles all the thread safe connection locking, 52 | connection management in association with the Connection Manager, and cursor management. 53 | Leaving all the work you have to do in your code is write MySQL Queries. Saving you hours of work.
54 |In an attempt to help answer some questions preemptively. A FAQ page has been create along 51 | side the with the docs. Lets hope this helps.
52 |Currently debugging is very limited, but we do contain a simple debugging capability. You can 55 | enable debugging by assigning PySQLQuery.logging_path to a string value of a file to write the 56 | debugging output to. This will cause every query executed and any errors to be written to the 57 | supplied file.
58 |Example:
59 |import PySQLPool
60 |
61 | PySQLPool.PySQLQuery.logging_path = '/path/to/file.txt'
62 | At this time PySQLPool does not wrap the exceptions that come out of MySQLdb. So any and all errors 68 | thrown from any of your queries and/or other actions by MySQLdb will be of its types. Of which the base is 69 | MySQLdb.Error. To find out what error you have caused. MySQLdb.Error contains a two-element tuple called args. 70 | The 1st value will contain the MySQL error number, and the second will contain the error message.
71 |Example:
72 |try:
73 | connection = PySQLPool.getNewConnection(username='root', password='123456', host='localhost', db='mydb')
74 | query = PySQLPool.getNewQuery(connection)
75 | query.Query('select * from table')
76 | except MySQLdb.Error, e:
77 | print "Error %d: %s" % (e.args[0], e.args[1])
78 | sys.exit (1)
79 | | 54 | |
| 68 | |
|
| 75 | |
| 94 | |
| 105 | |
|
115 |
|
|
122 |
|
|
| 129 | |
|
| 137 | |
|
145 |
|
|
| 159 | |
| 171 | |
|
| 181 | |
| 190 | |
|
The basic usage of PySQLPool is pretty simple as you can see in this basic example:
55 |import PySQLPool
56 |
57 | connection = PySQLPool.getNewConnection(username='root', password='123456', host='localhost', db='mydb')
58 | query = PySQLPool.getNewQuery(connection)
59 | query.Query('select * from table')
60 | for row in query.record:
61 | print row['column']
62 | Here is how you would select a set of data from the DB and print it out
67 |>>> query.Query('select * from table')
68 | >>> for row in query.record:
69 | >>> print 'Column Value:', row['column']
70 | Column Value: 0
71 | Here is how you would update a table and see how many rows your update statement affected
77 |>>> query.Query('update table set column = 1')
78 | >>> print 'Affected Rows:', query.affectedRows
79 | Affected Rows: 10
80 | Here is how you would insert a new row and get back its insert ID for Auto-Increment indexes
86 |>>> query.Query('insert into table ('column') values ('1')')
87 | >>> print 'Last Insert ID:', query.lastInsertID
88 | Last Insert ID: 11
89 | When using PySQLPool with threads you need to do things a little bit different from a single threaded app. 95 | The following example will show you a basic code structure you would use with a thread:
96 |import PySQLPool
97 | import threading
98 |
99 | connection = PySQLPool.getNewConnection(username='root', password='123456', host='localhost', db='mydb')
100 |
101 | class MyThread(threading.thread):
102 | def __init__(self):
103 | ...
104 |
105 | def run(self):
106 | query = PySQLPool.getNewQuery(connection)
107 | query.Query('insert into table ('column') values ('1')')
108 | By requesting a new PySQLQuery object in the threads run method you allow the pooling layer to create new connections 110 | to the DB as needed for each thread. Don’t worry you will not create 1 connect per thread but will create new ones as 111 | demand rises based on speed of threads and query execution time, but you will not create more then the limit.
112 |The max connection limit is a limiter on the auto creation of new connections based on thread demand. The limit is set 116 | globally to be used by each set of connection credentials. What that means is that for each set of connection arguments 117 | you use, specifically the host/username/password/db, the max connection limiter will keep those connections <= to its 118 | value. There currently is no way to set a limit per connection credentials.
119 |By default the limit is set to 10, but you can change it farely easy.:
120 |import PySQLPool
121 | PySQLPool.getNewPool().maxActiveConnections = 1
122 | Sometimes you may be dealing with a very large dataset and would wish not to load it all up into memory on your local 128 | machine, but would rather let it stream back from the MySQL server row by row. You can achieve this by simply changing 129 | out what method you call on your PySQLQuery object.:
130 |import PySQLPool
131 |
132 | connection = PySQLPool.getNewConnection(username='root', password='123456', host='localhost', db='mydb')
133 | query = PySQLPool.getNewQuery(connection)
134 | query.QueryOne('select * from table')
135 | for row in query.record:
136 | print row['column']
137 | As you will notice we switched out the query.Query() with a query.QueryOne(). This causes PySQLPool/MySQLdb to return 140 | back to you a Python Generator that will fetch from the server row by row as you iterate through the results.
141 |