Hello All.
I got a doubt.
In general, for updating remote database, we use ip address for the remote system.
Here, i would like to update two remote systems at a time.
I wrote the code like the below one.
Could anyone please give the correct advice and suggestions.


try:
import MySQLdb
import _mysql_exceptions as DB_EXC
ip = 8
while(ip <= 9):
cxn1 = MySQLdb.connect(host='10.0.2.9',host='192.168.1.2', db='navicat')
cur1 = cxn1.cursor()
cxn = MySQLdb.connect(user='root',db='navicat')
cur = cxn.cursor()

Regards
BHANU

Recommended Answers

All 6 Replies

could you put tags aroud your code, please ?
[ code=python]
your code here
[ /code]

2d point, I don't understand what you are doing

ip = 8
while(ip <= 9):
cxn1 = MySQLdb.connect(host='10.0.2.9',host='192.168.1.2', db='navicat')
cur1 = cxn1.cursor()
cxn = MySQLdb.connect(user='root',db='navicat')
cur = cxn.cursor()

You don't use ip variable, nor do you increment it.
cnx1 has 2 hosts but no user

I concur with Jice. In case you're simply asking us for suggestions of how to perform the same action with two different targets I would propose a for loop:

>>> for octet in [ '8', '9' ]:
...     ip_add = '10.0.2.' + octet
...     print 'IP Address:', ip_add
...     
IP Address: 10.0.2.8
IP Address: 10.0.2.9
>>>

An even better solution would be to come up with a function that connects to a database and returns the connection object. The input to this function would be host information, login details, etc.

HTH

See, here i do with single ip address and a database in that ip address.
I would like to add one more ip address. I mean , one more connection.

import MySQLdb
import _mysql_exceptions as DB_EXC
cxn1 = MySQLdb.connect(host='10.0.2.9', db='navicat')
cur1 = cxn1.cursor()
cxn = MySQLdb.connect(user='root',db='navicat')
cur = cxn.cursor()

You can try something like this :

dbLst=[]
for ip in ['127.0.0.1', '127.0.0.2', '127.0.0.3', '127.0.0.4']:
    cnx=MySQLdb.connect(host=ip, user='root', db='navicat')
    cur=cnx.cursor()
    dbLst.append({'cnx':cnx, 'cur':cur})

And please, if you post code wrap it between tags (see my preceeding post)

Is there any other way to do like this?

Is there any other way to do like this?

Maybe... the best would be you tell me what doesn't suit you in this way so that I could adapt this script...

But, with what you told, this is certainly the best way I can see :
- you can have any number od databases,
- you can store whatever parameter you want for each database (here, the connexion and the cursor but it could be anything else)
- easy to use after...

Here is another idea. Instead of a list of DB, you have a dictionnary which has ip adresses for keys :

dbDic={}
for ip in ['127.0.0.1', '127.0.0.2', '127.0.0.3', '127.0.0.4']:
    cnx=MySQLdb.connect(host=ip, user='root', db='navicat')
    cur=cnx.cursor()
    dbDic[ip]={'cnx':cnx, 'cur':cur}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.