Advertisement
Guest User

Untitled

a guest
Apr 5th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. import psycopg2
  2. import random
  3. import MySQLdb
  4.  
  5.  
  6. try:
  7. conn = psycopg2.connect("dbname='musicbrainz' user='musicbrainz' host='localhost' password='musicbrainz'")
  8. conn1 = MySQLdb.connect(host = "localhost", user = "root", passwd = "40OZlike", db = "plalyst")
  9. conn2 = MySQLdb.connect(host = "localhost", user = "root", passwd = "40OZlike", db = "plalyst")
  10. print("connections made...")
  11. cur = conn.cursor()
  12. cur1 = conn1.cursor()
  13. cur2 = conn2.cursor()
  14. print("cursors created....")
  15. rnumbers = random.sample(range(1, 22660511), 100000)
  16. print("Random numbers generated...")
  17. for eachnum in rnumbers:
  18. print(eachnum)
  19. songName=""
  20. while(songName==""):
  21. cur.execute("""select name from track where id = %s """, (eachnum,))
  22. rows = cur.fetchall()
  23. print(rows)
  24. if not len(rows)==0:
  25. songName = rows[0][0]
  26. eachnum+=1
  27. print("Got the track name:")
  28. print(songName)
  29. sql = "INSERT into Song (name) values ( '"+songName+"')"
  30. print(sql)
  31. cur1.execute(sql)
  32. cur1.execute('commit')
  33. print("inserted into the song table....")
  34. cur.execute("""select distinct t.name from track tr
  35. join recording r
  36. on tr.recording = r.id
  37. join recording_tag rt
  38. on rt.recording = r.id
  39. join tag t
  40. on
  41. rt.tag= t.id
  42. where tr.name = %s
  43. order by t.name""", (rows[0][0],))
  44. print("got the tag names....")
  45. rows = cur.fetchall()
  46. print(rows)
  47. for row in rows:
  48. print(row)
  49. qry = "select * from Tag where name='"+row[0]+"'"
  50. print(qry)
  51. cur2.execute(qry)
  52. row2 = cur2.fetchall()
  53. print("getting if the tag is already in")
  54. if (len(row2)==0):
  55. print("taag not present...")
  56. qry1 = "INSERT INTO Tag (name) VALUES ('"+row[0]+"')"
  57. print(qry1)
  58. cur1.execute(qry1)
  59. cur1.execute('commit')
  60. print("inserted into the tag table..")
  61. print("getting song id...")
  62. qry2 = "select id from Song where name='"+songName+"'"
  63. print(qry2)
  64. cur2.execute(qry2)
  65. songId = cur2.fetchall()[0][0]
  66. print(row[0])
  67. qry3 = "select id from Tag where name='"+row[0]+"'"
  68. print(qry3)
  69. #done till here problem with no no tag
  70. cur2.execute(qry3)
  71. print(cur2.fetchall())
  72. tagId = cur2.fetchall()[0][0]
  73. qry1 = "INSERT INTO SongTag VALUES ('"+songId+"','"+tagId+"')"
  74. cur2.execute(qry4)
  75. print("inserted into the songtag table..")
  76. gc.collect()
  77. cur.close()
  78. cur1.close()
  79. cur2.close()
  80. conn.close()
  81. conn1.close()
  82. conn2.close()
  83. except:
  84. conn1 = MySQLdb.connect(host = "localhost", user = "root", passwd = "40OZlike", db = "plalyst")
  85. cur = conn1.cursor()
  86. cur.execute("SET FOREIGN_KEY_CHECKS = 0;")
  87. cur.execute("Truncate table Song")
  88. cur.execute("Truncate table Tag")
  89. cur.execute("Truncate table SongTag")
  90. cur.execute("SET FOREIGN_KEY_CHECKS = 1;")
  91. cur.close()
  92. conn1.close()
  93. print(e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement