Advertisement
Guest User

Untitled

a guest
May 1st, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. import mysql.connector
  2. from mysql.connector import errorcode
  3. import re
  4. from prettytable import PrettyTable
  5.  
  6. def new_record():
  7. s=""
  8. a=""
  9. b=""
  10. print("Please enter the table name")
  11. name=input(">>")
  12. ini_query="SELECT COLUMN_NAME as cn FROM information_schema.COLUMNS WHERE TABLE_NAME='{}';".format(name)
  13. cursor.execute(ini_query)
  14. data=cursor.fetchall()
  15. for i in range(len(data)):
  16. a+=str(data[i])
  17. s=re.findall(r"\w+", a)
  18. entered_data=s
  19. for i in range(len(s)):
  20. entered_data[i]=input("please enter {}:".format(s[i]))
  21. b+="%s"+","
  22. add_record=("INSERT INTO {} VALUES({});".format(name,b[0:-1]))
  23.  
  24. cursor.execute(add_record,entered_data)
  25. cnx.commit()
  26.  
  27. def print_data():
  28. a=""
  29. c=""
  30. b=0
  31. d=0
  32. tab=[]
  33. print("Enter the table name")
  34. name=input(">>")
  35. ini_query="SELECT COLUMN_NAME as cn FROM information_schema.COLUMNS WHERE TABLE_NAME='{}';".format(name)
  36. cursor.execute(ini_query)
  37. data=cursor.fetchall()
  38. for i in range(len(data)):
  39. a+=str(data[i])
  40. b+=1
  41. s=re.findall(r"\w+", a)
  42. t=PrettyTable(s)
  43. query="SELECT * FROM {}".format(name)
  44. cursor.execute(query)
  45. data=cursor.fetchall()
  46. for i in range(len(data)):
  47. c+=str(data[i])
  48. n=re.findall(r"\w+", c)
  49.  
  50. for i in range(len(n)):
  51. if d<b:
  52. d+=1
  53. tab.append(n[i])
  54. else:
  55. d=1
  56. t.add_row(tab)
  57. tab=[]
  58. tab.append(n[i])
  59. print(t)
  60. def create_table():
  61. a=""
  62. d=""
  63. att_list={}
  64. print("Enter the table name:")
  65. name=str(input())
  66. print("Please enter number of attributes:")
  67. num=int(input())
  68. for i in range(num):
  69. print("Enter the {}.attribute name:".format(i+1))
  70. key=str(input())
  71. print("Enter the {} data type:".format(key))
  72. value=str(input())
  73. att_list[key]=value
  74. for i in att_list:
  75. d="{} {},".format(i,att_list[i])
  76. a+=d
  77. init="({})".format(a[0:-1])
  78. new_table="""CREATE TABLE {} {};""".format(name,init)
  79. try:
  80. print("Creating table second:")
  81. cursor.execute(new_table)
  82. except mysql.connector.Error as e:
  83. if e.errno==errorcode.ER_TABLE_EXISTS_ERROR:
  84. print("already exists")
  85. else:
  86. print(e.msg)
  87. else:
  88. print("done")
  89. cnx.commit()
  90. def menu():
  91. exitt="no"
  92. while exitt=="no":
  93. print ("What do you want to do now?")
  94. print("1-create a table")
  95. print("2-add new record")
  96. print("3-Print table")
  97. print("4-exit")
  98. inp=str(input(">>"))
  99. if inp=="1":
  100. create_table()
  101. elif inp=="2":
  102. new_record()
  103. elif inp=="3":
  104. print_data()
  105. elif inp=="4":
  106. exitt="yes"
  107. else:
  108. menu()
  109.  
  110. if __name__=='__main__':
  111.  
  112. cnx=mysql.connector.connect(user='******',password='******',host='*******',database='******')
  113. cursor=cnx.cursor()
  114.  
  115. menu()
  116. cursor.close()
  117. cnx.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement