Guest User

Untitled

a guest
Nov 26th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. import sys,os
  2. from models.DBHandler import DBHandler
  3.  
  4. import MySQLdb
  5. from MySQLdb.cursors import DictCursor
  6. from DBHandler import DBHandler
  7.  
  8. GET_USER = 'select * from users where id = %s'
  9. UPDATE_USER_NAME = 'update users set username = %s where id = %s'
  10.  
  11. def oldway():
  12. con = MySQLdb.connect(host='127.0.0.1',user='uname',passwd='passwd',db='dbname',cursorclass=DictCursor)
  13. curs = con.cursor()
  14. curs.execute('select * from users where id = 4;')
  15.  
  16. print 'Number of results %s' % curs.rowcount
  17.  
  18. row = curs.fetchone()
  19. jason = dict()
  20.  
  21. jason['id'] = row['id']
  22. jason['name'] = row['username']
  23. jason['age'] = row['age']
  24. jason['pets'] = []
  25.  
  26. curs.execute('select pets.name as pet_name from pets join users on pets.user_id = users.id where users.id = %s;',jason['id'])
  27. for x in range(0,curs.rowcount):
  28. jason['pets'].append(curs.fetchone()['pet_name'])
  29.  
  30. print jason
  31.  
  32. def newway(uid):
  33. db = DBHandler(dhost='127.0.0.1',duser='uname',dpass='passwd',dbname='dbname',cursor='DictCursor')
  34. db.execute(UPDATE_USER_NAME,("bagger",4))
  35. vinny = db.getrow(GET_USER,uid)
  36. print vinny
  37.  
  38. if __name__ == '__main__':
  39. newway(4)
Add Comment
Please, Sign In to add comment