Guest User

Untitled

a guest
Jul 19th, 2018
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. import mysql.connector
  2. import os
  3. cnx = mysql.connector.connect(user='cheevu_rs',password="dummy123",database="lp")
  4. cur = cnx.cursor()
  5. def fn():
  6.     with open('file.txt', 'r') as fp:
  7.                 next(fp)
  8.                 for line in fp:
  9.                     print (line)
  10.                     words=line.split(" ")
  11.                     print (words)
  12.                     question_marks=[]
  13.                     a=0
  14.                     while a<len(words):
  15.                         question_marks.append('"%s"')
  16.                         a+=1
  17.                     quests = ','.join(question_marks)
  18.                     print (quests)
  19.  
  20.                     print(tuple(words))
  21.                     p="INSERT INTO tabel VALUES(%s)" % quests
  22.                     print (p%tuple(words))
  23.                     cur.execute(p%tuple(words))
  24.                     cnx.commit()
  25.  
  26. ip = input("Does table exist already? (y/n)")
  27. if ip =="y":
  28.     f=1
  29. else:
  30.     f=0
  31. line = open("file.txt").readline()
  32. print (line)
  33. if f==0:
  34.     words=line.split(" ")
  35.     print (words)
  36.     for i in words:
  37.         if i == words[0]:
  38.             cur.execute("CREATE TABLE tabel(%s %s);" % (i,"VARCHAR(50)"))
  39.         else:
  40.             cur.execute("ALTER TABLE tabel ADD COLUMN %s %s;" % (i,"VARCHAR(50)"))
  41.     fn()
  42. else:
  43.     fn()
Add Comment
Please, Sign In to add comment