Python

Soo, I managed to ‘write’ (borrow mostly), enough Python code to connect to a MySQL DB, send it a query, return the results and output it to the command line in a form that is semi-readable. I had a couple of issues with the looping system, mostly because I’ve been absent from coding lately, but I eventually got it figured out.

Next:

Find an email script working

Write to an Excel sheet and then email it somewhere

Of note:

MySQLdb is a Python module necessary to connect to a MySQL database, it connects to the Python DB API (Ad-libbing a little here), which I believe is a C-Library. Anyways, the point is, if you’re doing this on a HostGator shared account you will have to contact support and have them install MySQLdb onto your server(s).

Like the above, if you want to add any other Python modules that aren’t installed you just need to shoot an email to support and they’ll install it for you. I had them also do this with xlwt, a module used to write data to an Excel document.

Code is below the line.

# import MySQL module
import MySQLdb
# connect
db = MySQLdb.connect(
host=”localhost”,
user=”mordy3_wrdp5″,
passwd=”mmv{OOXfXl9b”,
db=”mordy3_wrdp5″)
# create a database cursor
cursor = db.cursor()
# execute SQL select statement
cursor.execute(“SELECT c.comment_author, c.comment_author_email, c.comment_author_ip FROM wp_comments c”)
# get and display one row at a time
rows = cursor.fetchall()
cursor.close
db.close()
”’for row in rows:
for value in row:
print row[0], “–>”, row[1], “–>”, row[2]
”’
print rows
for row in rows:
data = ” “
for dataset in row:
data = data + dataset + “–>”
print data

# import MySQL module

import MySQLdb

# connect

db = MySQLdb.connect(

host=”localhost”,

user=”mordy3_wrdp5″,

passwd=”mmv{OOXfXl9b”,

db=”mordy3_wrdp5″)

# create a database cursor

cursor = db.cursor()

# execute SQL select statement

cursor.execute(“SELECT c.comment_author, c.comment_author_email, c.comment_author_ip FROM wp_comments c”)

# get and display one row at a time

rows = cursor.fetchall()

cursor.close

db.close()

”’for row in rows:

for value in row:

print row[0], “–>”, row[1], “–>”, row[2]

”’

print rows

for row in rows:

data = ” ”

for dataset in row:

data = data + dataset + “–>”

print data