Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Switching Databases

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


victorsubervi at gmail

Nov 23, 2009, 2:47 AM

Post #1 of 8 (263 views)
Permalink
Switching Databases

Hi;
I have the following code:

import MySQLdb
...
user, passwd, db, host = login()
db = MySQLdb.connect(host, user, passwd, 'cart')
cursor= db.cursor()
...
cursor.close()
db = MySQLdb.connect(host, user, passwd, db)
cursor= db.cursor()

Now, python complains about me opening a new connection. But I thought I'd
closed the first one! So, I replaced the last 3 lines with this:

cursor.execute('use %s;' % db)

but it didn't like that, either. Any way to switch databases?
TIA,
Victor


carsten.haese at gmail

Nov 23, 2009, 6:17 AM

Post #2 of 8 (253 views)
Permalink
Re: Switching Databases [In reply to]

Victor Subervi wrote:
> Hi;
> I have the following code:
>
> import MySQLdb
> ...
> user, passwd, db, host = login()
> db = MySQLdb.connect(host, user, passwd, 'cart')
> cursor= db.cursor()
> ...
> cursor.close()
> db = MySQLdb.connect(host, user, passwd, db)
> cursor= db.cursor()
>
> Now, python complains about me opening a new connection.

Do not paraphrase error messages. Copy and paste the actual error
message and traceback.

> But I thought
> I'd closed the first one!

You thought you did, but did you? The code snippet above doesn't show
any code that closes a database connection.

> So, I replaced the last 3 lines with this:
>
> cursor.execute('use %s;' % db)
>
> but it didn't like that, either.

Do not paraphrase error messages. Copy and paste the actual error
message and traceback.

--
Carsten Haese
http://informixdb.sourceforge.net

--
http://mail.python.org/mailman/listinfo/python-list


victorsubervi at gmail

Nov 23, 2009, 6:45 AM

Post #3 of 8 (252 views)
Permalink
Re: Switching Databases [In reply to]

On Mon, Nov 23, 2009 at 9:17 AM, Carsten Haese <carsten.haese [at] gmail>wrote:

> You thought you did, but did you? The code snippet above doesn't show
> any code that closes a database connection.
>

Would you be so kind as to tell me exactly what code *does* close a
database, then? That would solve this handily. Other than than, when I am at
a computer when I can provide you error messages, I will, but it all boils
down to that code that closes the database, doesn't it? Since my code,
according to you (and it seems logical) didn't, I sure would appreciate your
providing the correct code that would.
Thank you,
V


carsten.haese at gmail

Nov 23, 2009, 7:08 AM

Post #4 of 8 (253 views)
Permalink
Re: Switching Databases [In reply to]

Victor Subervi wrote:
> On Mon, Nov 23, 2009 at 9:17 AM, Carsten Haese <carsten.haese [at] gmail
> <mailto:carsten.haese [at] gmail>> wrote:
>
> You thought you did, but did you? The code snippet above doesn't show
> any code that closes a database connection.
>
>
> Would you be so kind as to tell me exactly what code *does* close a
> database, then?

Assuming that db is the name of the database connection object,
db.close() closes the connection.

> That would solve this handily. Other than than, when I
> am at a computer when I can provide you error messages, I will, but it
> all boils down to that code that closes the database, doesn't it?

Only if your diagnosis of the problem is accurate, and I have my doubts
about that. A MySQL database server is perfectly capable of entertaining
multiple simultaneous connections, so I find it unlikely that your
inability to open a second connection is caused by not closing the first
connection.

As I said, the best way we can help you is if you copy the actual error
message so that we may diagnose the actual problem and suggest a
solution that fixes the problem.

--
Carsten Haese
http://informixdb.sourceforge.net

--
http://mail.python.org/mailman/listinfo/python-list


victorsubervi at gmail

Nov 23, 2009, 7:26 AM

Post #5 of 8 (253 views)
Permalink
Re: Switching Databases [In reply to]

On Mon, Nov 23, 2009 at 10:08 AM, Carsten Haese <carsten.haese [at] gmail>wrote:

> As I said, the best way we can help you is if you copy the actual error
> message so that we may diagnose the actual problem and suggest a
> solution that fixes the problem.
>

That gave me the idea that I should simply open two separate connections.
Here's the error that got thrown with the second connection:

A problem occurred in a Python script. Here is the sequence of function
calls leading up to the error, in the order they occurred.
/var/www/html/angrynates.com/cart/createProducts2.py 117 </body>
118 </html>
119 '''
120
121 createProducts2()
*createProducts2* = <function createProducts2>
/var/www/html/angrynates.com/cart/createProducts2.py in *createProducts2*()
35 sst.append(bits[2])
36 sstp.append(bits[1])
37 db2 = MySQLdb.connect(host, user, passwd, db)
38 cursor2 = db.cursor()
39 created = []
db2 *undefined*, *global* *MySQLdb* = <module 'MySQLdb' from
'/usr/lib64/python2.4/site-packages/MySQLdb/__init__.pyc'>, MySQLdb.*connect
* = <function Connect>, *host* = 'localhost', *user* = 'beno', *passwd* =
'Kj9rg2', *db* = <_mysql.connection open to 'localhost' at 28772c0>
/usr/lib64/python2.4/site-packages/MySQLdb/__init__.py in
*Connect*(*args=('localhost',
'beno', 'Kj9rg2', <_mysql.connection open to 'localhost' at 28772c0>),
**kwargs={}) 73 """Factory function for connections.Connection."""
74 from connections import Connection
75 return Connection(*args, **kwargs)
76
77 connect = Connection = Connect
*Connection* = <class 'MySQLdb.connections.Connection'>, *args* =
('localhost', 'beno', 'Kj9rg2', <_mysql.connection open to 'localhost' at
28772c0>), *kwargs* = {}
/usr/lib64/python2.4/site-packages/MySQLdb/connections.py in
*__init__*(self=<_mysql.connection
closed at 28d95c0>, *args=('localhost', 'beno', 'Kj9rg2', <_mysql.connection
open to 'localhost' at 28772c0>), **kwargs={}) 162
kwargs2['client_flag'] = client_flag
163
164 super(Connection, self).__init__(*args, **kwargs2)
165
166 self.encoders = dict([ (k, v) for k, v in conv.items()
*builtin* *super* = <type 'super'>, *global* *Connection* = <class
'MySQLdb.connections.Connection'>, *self* = <_mysql.connection closed at
28d95c0>, ).*__init__* = <bound method Connection.__init__ of
<_mysql.connection closed at 28d95c0>>, *args* = ('localhost', 'beno',
'Kj9rg2', <_mysql.connection open to 'localhost' at 28772c0>), *kwargs2* =
{'client_flag': 196608, 'conv': {0: <class 'decimal.Decimal'>, 1: <type
'int'>, 2: <type 'int'>, 3: <type 'long'>, 4: <type 'float'>, 5: <type
'float'>, 7: <function mysql_timestamp_converter>, 8: <type 'long'>, 9:
<type 'int'>, 10: <function Date_or_None>, ...}}*TypeError*: connect()
argument 4 must be string, not Connection
args = ('connect() argument 4 must be string, not Connection',)

TIA,
V


carsten.haese at gmail

Nov 23, 2009, 7:59 AM

Post #6 of 8 (261 views)
Permalink
Re: Switching Databases [In reply to]

Victor Subervi wrote:
> On Mon, Nov 23, 2009 at 10:08 AM, Carsten Haese <carsten.haese [at] gmail
> <mailto:carsten.haese [at] gmail>> wrote:
>
> As I said, the best way we can help you is if you copy the actual error
> message so that we may diagnose the actual problem and suggest a
> solution that fixes the problem.
>
>
> That gave me the idea that I should simply open two separate
> connections. Here's the error that got thrown with the second connection:
>
> [snip...]
>
> 37 db2 = MySQLdb.connect(host, user, passwd, db)
>
> [snip...]
>
> TypeError: connect() argument 4 must be string, not Connection

Have you tried to *read* and *understand* this error message? My guess
is no. The error message tells you all you need to know. You need to
pass a string as argument 4, but you aren't. <<db>> is a database
connection object, not a string. Change <<db>> to a string containing
the name of the MySQL database to which you want to connect.

--
Carsten Haese
http://informixdb.sourceforge.net

--
http://mail.python.org/mailman/listinfo/python-list


victorsubervi at gmail

Nov 23, 2009, 8:05 AM

Post #7 of 8 (255 views)
Permalink
Re: Switching Databases [In reply to]

On Mon, Nov 23, 2009 at 10:59 AM, Carsten Haese <carsten.haese [at] gmail>wrote:

> Have you tried to *read* and *understand* this error message? My guess
> is no. The error message tells you all you need to know. You need to
> pass a string as argument 4, but you aren't. <<db>> is a database
> connection object, not a string. Change <<db>> to a string containing
> the name of the MySQL database to which you want to connect.
>

Yes, Carsten, I did read the error message. And the obvious passed me by
LOL! Sorry. Thanks,
V


tjreedy at udel

Nov 23, 2009, 11:56 AM

Post #8 of 8 (246 views)
Permalink
Re: Switching Databases [In reply to]

Victor Subervi wrote:

> A problem occurred in a Python script. Here is the sequence of function
> calls leading up to the error, in the order they occurred.

[snip hmtl error report]

Please post plain text - ascii or utf8. On my newsreader, what you
posted, with a mismash of colors, bold, italic, and sizes, is not readable.

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.