Hi Guys,
A couple people following the thread have asked about some database code, I did have some stuff I wrote ages ago, which for whatever reason has seemingly disappeared from my hard drive! None the less I had been promising to post some code for a long time, I finally got off my ass and wrote some new stuff…
So here a really simple class to demo straight saving maxfiles to (or any data for that matter) to a database (I was using MySQL i have tested the code with anything else).
I will post a com version later tonight… anyways enough jibba jabba here is the code, if you get any errors just post them or pm me and yes I was lazy and did not comment my code I will fix this later though…
Cheers
Dave
"""
Demo Class showing interaction with MySQLdb.
The Class assumes there is db called 'mymaxfiles' and that it has a table called 'maxfiles'. Its just a basic example of how one
my want to store some files etc...
All the methods below assume the database mymaxfile exists as well.
Crafted By,
David Mackenzie
First off I don't pretend to be some SQL hacker! I just know what I need to....
This is a simple class for interacting with simple db and table. It could easily be modified to do allot more.
You will also require MySQLdb for this to work you can download here "http://sourceforge.net/projects/mysql-python".
I should also point out that I have not debugged this a great deal, I have however had no errors thrown in all my testing,
if you do get an error just shoot it acrros to me...
Cheers
Dave
"""
import MySQLdb as mys
import os
import os.path as pt
def InsertMaxFile(thefile):
db = mys.connect(host="localhost", user="root", passwd="daveRoot", db="mymaxfiles")
mycurs = db.cursor()
mycurs.execute("INSERT INTO maxfiles (files) values('%s')" % (thefile))
db.commit()
return True
def CreateTable(self):
try:
db = mys.connect(host="localhost", user="root", passwd="daveRoot", db="mymaxfiles")
mycurs = db.cursor()
db.commit("CREATE TABLE maxfiles (id INT NOT NULL AUTO_INCREMENT, files varchar(256) NOT NULL, primary key(id));")
return True
except:
return [False, TBWrite()]
def TBWrite():
import traceback
return traceback.format_exc()
class MaxFileSaver:
def __init__(self, server, username, password, database):
try:
self.db = mys.connect(host=server, user=username, passwd=password, db=database)
self.Cursor = self.db.cursor()
self.Connected = True
self.Commited = True
except:
self.Connected = False
self.db = None
self.Cursor = None
self.Commited = True
print TBWrite()
def InsertFile(self, thefile):
if self.Connected == True:
try:
self.Cursor.execute("INSERT INTO maxfiles (files) values('%s')" % (thefile))
self.Commited = False
return True
except:
return [False, TBWrite()]
else:
return [False, "Something went wrong"]
def CommitChanges(self, yayorney):
if self.Connected:
if yayorney == True:
try:
self.db.commit()
self.Commited = True
return True
except:
self.Commited = False
return [False, TBWrite()]
else:
return [False, "Not Connected"]
def GetFiles(self):
if self.Connected:
try:
self.Cursor.execute("SELECT * FROM maxfiles")
return (self.Cursor.fetchall())
except:
return [False, TBWrite()]
def SetFile(self, theid, thevalue):
if self.Connected:
try:
print self.Cursor.execute("UPDATE maxfiles
SET files='%s' WHERE id='%s'" % (thevalue, theid))
return True
except:
return [False, TBWrite()]
else:
return [False, "Not Connected"]
def DeleteFile(self, theid):
if self.Connected:
try:
self.Cursor.execute("DELETE FROM maxfiles WHERE id=%s" % (theid))
return True
except:
return [False, TBWrite()]
else:
return [False, "Not Connected"]
def RollBackChanges(self, yayorney):
if self.Commited == True:
return [False,"Data already commited, better luck next time"]
if self.Connected:
if yayorney:
try:
self.db.rollback()
return True
except:
return [False, TBWrite()]
else:
return [False, "Not Connected"]
def ReConnect(self, server, username, password, database):
self.Connected = False
self.db = None
self.Cursor = None
self.Commited = True
try:
self.db = mys.connect(host=server, user=username, passwd=password, db=database)
self.Cursor = self.db.cursor()
self.Connected = True
except:
return [False, TBWrite()]
if __name__ == "__main__":
con = MaxFileSaver("localhost", "root", "daveRoot", "mymaxfiles")
print con.InsertFile("C:\\\\With Any luck this will work.max")
print con.CommitChanges(True)
for i in con.GetFiles():
print i
print con.DeleteFile("14")
print con.SetFile("23", "C:\\\\SomeNewFile")
print con.CommitChanges(True)