def find_by_username(cls,username): try: conn = connect() query = 'SELECT * FROM {} WHERE `username`="{}"'.format(cls.tablename,username) cursor = conn.cursor(pymysql.cursors.DictCursor) cursor.execute(query) res = cursor.fetchone() return res except Exception as e: print(e) return None finally: close(cursor,conn)
def getByUserId(cls, user_id): try: conn = connect() cursor = conn.cursor(pymysql.cursors.DictCursor) query = "SELECT * FROM {} WHERE `user_id`={}".format( cls.tablename, user_id) cursor.execute(query) res = cursor.fetchall() return res except Exception as e: print(e) return None finally: close(cursor, conn)
def save(cls,data): try: conn = connect() cursor = conn.cursor() query = "INSERT INTO {}(`username`,`password`) VALUES(%s,%s)".format(cls.tablename) #generate password hash pass_hash = generate_password_hash(data["password"]) bind = (data["username"],pass_hash) cursor.execute(query,bind) conn.commit() return True except Exception as e: print(e) return False finally: close(cursor,conn)
def save(cls, data): try: conn = connect() cursor = conn.cursor() query = "INSERT INTO {}(`user_id`,`website`,`username`,`password`) VALUES(%s,%s,%s,%s)".format( cls.tablename) bind = (data["user"], data["website"], data["username"], data["password"]) cursor.execute(query, bind) conn.commit() return True except Exception as e: print(e) return False finally: close(cursor, conn)
import dbconfig as db try: mydb = db.open() #print(mydb) mycursor = mydb.cursor() mycursor.execute("SHOW DATABASES") for x in mycursor: print(x) mycursor.execute("SELECT * FROM todoitems") print(mycursor.fetchall()) finally: db.close(mydb)