SHARE
TWEET

Untitled

a guest Jan 24th, 2020 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sqlite3
  2.  
  3. class Database:
  4.     def __init__(self, database):
  5.         self.conn = sqlite3.connect(database)
  6.         self.curs = self.conn.cursor() # Ausf├╝hrung
  7.  
  8.         create_table = """
  9.        CREATE TABLE IF NOT EXISTS user (
  10.            user_id INTEGER PRIMARY KEY NOT NULL,
  11.            vorname TEXT,
  12.            nachname TEXT,
  13.            position TEXT,
  14.            berechtigung INTEGER
  15.        )
  16.        """
  17.  
  18.         self.curs.execute(create_table)
  19.         self.conn.commit()
  20.  
  21.     def newuser(self, user_id, ber, vorname="", nachname="", pos=""):
  22.         insert_user = """
  23.        INSERT INTO user (user_id, vorname, nachname, position, berechtigung) VALUES (?,?,?,?,?)
  24.        """
  25.         try:
  26.             self.curs.execute(insert_user, (user_id, vorname, nachname, pos, ber))
  27.             self.conn.commit()
  28.             return True, ""
  29.         except Exception as e:
  30.             return False, e
  31.  
  32.     def deleteuser(self, user_id):
  33.         delete_user = """
  34.        DELETE FROM user WHERE user_id = (?)
  35.        """
  36.         self.curs.execute(delete_user, (user_id,))
  37.         self.conn.commit()
  38.         return True
  39.    
  40.     def changepermission(self, user_id, ber):
  41.         change_permission = """
  42.        UPDATE user SET berechtigung = (?) WHERE user_id = (?)
  43.        """
  44.         if ber == 1 or ber == 0:
  45.             self.curs.execute(change_permission, (ber, user_id))
  46.             self.conn.commit()
  47.             return True
  48.         else:
  49.             return False
  50.    
  51.     def getalluser(self):
  52.         self.curs.execute("SELECT * FROM user")
  53.         return self.curs.fetchall()
  54.    
  55.     def onlyoneuser(self, user_id):
  56.         self.curs.execute("SELECT * FROM user WHERE user_id = (?)", (user_id,))
  57.         return self.curs.fetchone()
  58.  
  59. if __name__ == "__main__":
  60.     test = Database("user.db")
  61.     test.newuser(3, 1, None)
  62.     test.newuser(2, 0)
  63.     print(test.newuser(1, 1, None))
  64.     print(test.newuser(1, 1, None))
  65.     print(test.deleteuser(3))
  66.     print(test.changepermission(2, 1))
  67.     print(test.getalluser())
  68.     print(test.onlyoneuser(5))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top