Python and datasources
From Docunext Technology Wiki
Contents |
MySQL
On debian, I installed python-mysqldb. Unfortunately, I haven't worked with manual database connections for awhile, I've been using abstraction layers for years, and only worrying about the SQL. So I'm a little rusty, to say the least!
I'm a little confused by the concept of a "cursor" with regard to databases, but it hasn't inhibited my use of MySQL via python as of yet.
Test MySQL SQL DB
Then to test it out, I wrote TestMySQLdb.py:
# Just a quick test of how MySQLdb works
import sys
import MySQLdb
try:
db_z_barf = MySQLdb.connect(db='db', host='host', user='user', passwd='pass')
print "Success"
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
c_jimmy_laugh = db_z_barf.cursor()
tables = c_jimmy_laugh.execute("SHOW TABLES");
one_row = c_jimmy_laugh.fetchone ()
print one_row
print "\n"
rows = c_jimmy_laugh.fetchall ()
for row in rows:
print row
c_jimmy_laugh.close()
db_z_barf.close()
It will throw an error unless you supply appropriate credentials.
Python, Hashes, Dictionaries and DBMs
I have a large hash file created by httxt2dbm which was returning some odd values for keys, so I wanted to standardize them. I also wanted to remove duplicate keys. I did so by simply creating a new dbm, looping through the first keyset and creating a new key value pair in the new dbm. Worked fine, and reduced the file size from 81 to 36M!
import anydbm
db = anydbm.open("bad_bots.db","w")
db2 = anydbm.open("bad_bots2.db","c")
for k, v in db.iteritems():
db2[k] = '1'
(All the values are one).
I wrote this little script to easily test which dbm format a db file is stored in:
import sys import whichdb file = sys.argv[1:] print whichdb.whichdb(file[0])
Python Database Toolkits and Abstraction Layers