Advertisement
Guest User

mysql importer python example

a guest
Nov 11th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.21 KB | None | 0 0
  1. import MySQLdb
  2. from sys import argv as sa
  3. # user arguments here (can set static)
  4.  
  5. global dbname
  6. global project
  7. global infile
  8. global ipadr
  9. global uname
  10. global pword
  11.  
  12. try:
  13.  try:
  14.   dbname=sa[1]
  15.  except Exception as e:
  16.   print """Please supply a [database] name, even if it exists already ..."""
  17.  try:
  18.   project=sa[2]
  19.  except Exception as e:
  20.   print e
  21.   print """Please supply a [table] name here"""
  22.  try:
  23.   infile=sa[3]
  24.  except Exception as e:
  25.   print e
  26.   print """Please supply the [file] to upload here"""
  27.  try:
  28.   ipadr=sa[4]
  29.  except Exception as e:
  30.   print e
  31.   print """Please give an [IP] here for the mysql instance"""
  32.  try:
  33.   uname=sa[5]
  34.  except Exception as e:
  35.   print e
  36.   print """Please supply a [username] here for the mysql instance"""
  37.  try:
  38.   pword=sa[6]
  39.  except Exception as e:
  40.   print e
  41.   print """Please supply a [password] for the mysql instance"""
  42. except Exception as e:
  43.  exit()
  44.  
  45. def conset():
  46.  global db
  47.  global cur
  48.  db = MySQLdb.connect(host=ipadr, user=uname, password=pword)
  49.  cur = db.cursor()
  50.  
  51. def docommit():
  52.  db.commit()
  53.  cur.close()
  54.  
  55. def redres(query):
  56.  cur.execute(query)
  57.  result=cur.fetchall()
  58.  for row in result:
  59.    print row
  60.  
  61. def usedb():
  62.  try:
  63.   query="use "+dbname
  64.   redres(query)
  65.  except Exception as e:
  66.   print e
  67.   pass
  68.  
  69. def credb():
  70.  try:
  71.   conset()
  72.   query="create database "+dbname+";"
  73.   redres(query)
  74.   usedb()
  75.  except Exception as e:
  76.   print e
  77.   usedb()
  78.  
  79. # make the db, dont care if it exists;
  80. def creta():
  81.  try:
  82.   query='CREATE TABLE '+str(project)+' (id INT NOT NULL AUTO_INCREMENT,email blob NOT NULL,user blob NOT NULL,domain blob NOT NULL,password blob NOT NULL,PRIMARY KEY (id));'
  83.   redres(query)
  84.  except Exception as e:
  85.   print e
  86.   pass
  87.  
  88. # alter the table, dont care if its alredy set
  89. def alta():
  90.  try:
  91.   query='ALTER TABLE '+str(project)+' AUTO_INCREMENT = 1;'
  92.   redres(query)
  93.  except Exception as e:
  94.   print e
  95.   pass
  96.  
  97. # push the file up
  98. def fileup():
  99.  try:
  100.   credb()
  101.   creta()
  102.   alta()
  103.   query='LOAD DATA LOCAL INFILE "'+infile+'" INTO TABLE '+str(project)+' FIELDS TERMINATED BY ":" (email, user, domain, password);'
  104.   redres(query)
  105.   docommit()
  106.  except Exception as e:
  107.   print e
  108.   pass
  109.  
  110. fileup()
  111. print "Finished"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement