Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help a noob with a script.

Status
Not open for further replies.

petrosky

Technical User
Aug 1, 2001
512
AU
Hi,

Firstly, I am very new to the Python world.

My scripts purpose is to export a MYSQL table from one server to another periodically as the original table is updated.

After trying to follow quite a few web examples...

I have the following.

Code:
import MySQLdb
import time
import re

try:
  mconn = MySQLdb.connect (host = "192.168.1.3",
                        user = "username",
                        passwd = "password",
                        db = "dbname")
  mcursor = mconn.cursor()

  mconn2 = MySQLdb.connect (host = "localhost",
                          user = "username",
                          passwd = "password",
                          db = "otherdbname")
  mcursor2 = mconn2.cursor()
except MySLQdb.Error, e:
  print "Error %d: %s" % (e.args[0], e.args[1])
  sys.exit (1)

# Read the values from original MYSQL database

mcursor = mconn.cursor (MySQLdb.cursors.DictCursor)
mcursor.execute ("SELECT brand, model FROM tblproducts WHERE Active = 1 Order by brand asc")
result_set = mcursor.fetchall ()
for row in result_set:
  print "%s, %s" % (row["brand"], row["model"])
print "Number of rows returned: %d" % mcursor.rowcount

mcursor.close ()

This indeed prints the results of the table to the screen.

I have tried many ways to incorporate an Insert into statement using the above loop.

If anyone has any better way for me to pipe the results of this first select into an insert. I would appreciate hearing any suggestions.

Peter.

Remember- It's nice to be important,
but it's important to be nice :)
 
Sorry...as with all noobs learning something new. I jumped in way too early and posted for help. It turned out to be me attempting to insert string values in this format 'string'

I have worked out the above (simple) problem and will post the full script in case any other people pass by looking for some help.

Cheers,

Peter.



Remember- It's nice to be important,
but it's important to be nice :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top