daily pastebin goal
13%
SHARE
TWEET

Untitled

a guest Jun 30th, 2018 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # import MySQLdb
  2. import os
  3.  
  4. import pymysql.cursors
  5.  
  6. _HOSTNAME="127.0.0.1"
  7. _DATABASE="listing_td_all"
  8. _USERNAME="root"
  9. _PASSWORD="root"
  10.  
  11. def sql_execute(sql_cmd):
  12.     # Connect to the database
  13.     connection = pymysql.connect(host=_HOSTNAME,
  14.                                  user=_USERNAME,
  15.                                  password=_PASSWORD,
  16.                                  db=_DATABASE,
  17.                                  charset='utf8mb4',
  18.                                  cursorclass=pymysql.cursors.DictCursor)
  19.     try:
  20.         with connection.cursor() as cursor:
  21.             cursor.execute(sql_cmd)
  22.             connection.commit()
  23.             if (sql_cmd.startswith(('CREATE','SELECT', 'SHOW'))):
  24.                 return cursor.fetchall()
  25.     except Exception as err:
  26.         connection.rollback()
  27.         print(err)
  28.     connection.close()
  29.  
  30. def sql_insert(sql_cmd, params):
  31.     # Connect to the database
  32.     connection = pymysql.connect(host=_HOSTNAME,
  33.                                  user=_USERNAME,
  34.                                  password=_PASSWORD,
  35.                                  db=_DATABASE,
  36.                                  charset='utf8mb4',
  37.                                  cursorclass=pymysql.cursors.DictCursor)
  38.     try:
  39.         with connection.cursor() as cursor:
  40.             cursor.execute(sql_cmd,params)
  41.             connection.commit()
  42.     except Exception as err:
  43.         connection.rollback()
  44.         print(err)
  45.     # if not (sql_cmd.startswith(('INSERT','UPDATE','DELETE'))):
  46.     #     raise MySQLdb.Error('Query error! It must be insert or update query.')
  47.     last_row_id = connection.insert_id()
  48.     connection.close()
  49.     return last_row_id
  50.  
  51. def sql_execute_with_param(sql_cmd, params):
  52.     # Connect to the database
  53.     connection = pymysql.connect(host=_HOSTNAME,
  54.                                  user=_USERNAME,
  55.                                  password=_PASSWORD,
  56.                                  db=_DATABASE,
  57.                                  charset='utf8mb4',
  58.                                  cursorclass=pymysql.cursors.DictCursor)
  59.     try:
  60.         with connection.cursor() as cursor:
  61.             cursor.execute(sql_cmd,params)
  62.             connection.commit()
  63.             if (sql_cmd.startswith(('CREATE','SELECT', 'SHOW'))):
  64.                 return cursor.fetchall()
  65.     except Exception as err:
  66.         connection.rollback()
  67.         print(err)
  68.     connection.close()
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