Writing /var/lib/dokuwiki/data/meta/teaching/ie0117/proyectos/2013/ii/proyecto_gps_-_drivecr/database_methods.meta failed
teaching:ie0117:proyectos:2013:ii:proyecto_gps_-_drivecr:database_methods
DBmethods.py
These are the methods the directly manipulate the databases. The server and client programs use these methods for retrieving and modifying the required information by importing the class and using it in their respective codes. Without these methods, client and server comunitacion, and other features of the app may become far more difficult to implement.
#!/usr/bin/env python #Connection import import MySQLdb #Creating the connection db = MySQLdb.connect(host='localhost', user='root', passwd='Mery.246891', db='prueba1') #Creting a cursor cursor = db.cursor() #------------------------------------Users table methods------------------------------------# #Retrieving the password of an user: def get_password(username): try: print "Por aqui voy" cursor.execute("select password from usuario where username='%s';" % username) return cursor.fetchone() [0] except: return "None/*/" #Registering a new user: def write_user(username, name, password, email): try: result = cursor.execute("INSERT INTO `usuario` (`username`, `name`, `password`, `email`) VALUES ( '%s' , '%s', '%s', '%s');" % (username, name, password , email)) db.commit() except: result = 0 return result #Changing the privileges of a user: def modify_privileges(username, privileges): result = cursor.execute("UPDATE Users SET privileges=%s WHERE username = '%s';" % (privileges, username)) db.commit() return result #Changing the password of an user def change_password(username, password): result = cursor.execute("UPDATE Users SET password='%s' WHERE username = '%s';" % (password, username)) db.commit() return result #Retrieving the privileges of an user def get_privileges(username): cursor.execute("SELECT privileges FROM Users WHERE username= '%s';" % username) return cursor.fetchone()[0] #Retrieving the email of an user def get_email(username): cursor.execute("SELECT mail FROM Users WHERE username= '%s';" % username) return cursor.fetchone()[0] #Adding a submit to an user def add_submit(username): try: result = cursor.execute("UPDATE Users SET submits = submits + 1 WHERE username = '%s';" % username) db.commit() except: result = 0 return result #Retrieving the times an user has submitted something def get_submits(username): cursor.execute("SELECT submits FROM Users WHERE username= '%s';" % username) return cursor.fetchone()[0]
teaching/ie0117/proyectos/2013/ii/proyecto_gps_-_drivecr/database_methods.txt · Last modified: 2022/09/20 00:08 (external edit)