Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. import psycopg2
  2. # from psycopg2 import sql
  3. from psycopg2.extensions import AsIs
  4.  
  5. import config
  6.  
  7.  
  8. class MyDataBase:
  9.     def __init__(self, tb_name):
  10.         self.table = tb_name
  11.         self.connect = psycopg2.connect(dbname="test", host="localhost", user=config.user, password=config.password)
  12.         self.connect.autocommit = True
  13.         self.cursor = self.connect.cursor()
  14.         self.cursor.execute("DROP TABLE IF EXISTS %s", (AsIs(self.table),))
  15.         self.cursor.execute("CREATE TABLE %s (id integer, msg_count integer)", (AsIs(self.table),))
  16.        
  17.        
  18.     def insert(self, user_id, msg_count):
  19.         self.cursor.execute("INSERT INTO %s (id, msg_count) VALUES (%s, %s)", (AsIs(self.table), user_id, msg_count))
  20.        
  21.        
  22.     def update(self, user_id, msg_count):
  23.         self.cursor.execute("UPDATE %s SET msg_count=%s WHERE id=%s", (AsIs(self.table), msg_count, user_id))
  24.        
  25.        
  26.     def outprint(self):
  27.         self.cursor.execute("SELECT * FROM %s;", (AsIs(self.table),))
  28.         print(self.cursor.fetchall())
  29.        
  30.        
  31.     def closedb(self):
  32.         self.cursor.close()
  33.         self.connect.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement