Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Wed Sep 26 15:26:59 2018
  5.  
  6. @author: pinateth
  7. """
  8.  
  9. #https://atcoordinates.info/2017/07/24/copying-tables-from-sqlite-to-postgresql/
  10.  
  11. import psycopg2
  12. import sqlite3
  13.  
  14. try :
  15.  
  16. #Connecteur
  17. hConnectPG = psycopg2.connect("dbname='MaBase' host='127.0.0.1' user='pinateth' password='pinateth'")
  18. hConnectSQLite = sqlite3.connect("/data/commerce/BaseSqlite.db")
  19. print('la connexion est effective')
  20. #curseur
  21. hCursorSQLite = hConnectSQLite.cursor()
  22. hCursorPG = hConnectPG.cursor()
  23.  
  24.  
  25.  
  26. #On connait le nom de la table, pas besoin de faire un script pour sélecitonner les autres tables sinon voir
  27. tableName = '1500000_Sales_Records'
  28. print('la connexion est effective2')
  29. #Création de la table avec une
  30.  
  31. hCursorPG.execute('''CREATE TABLE "{}" (
  32. Region TEXT,
  33. Country TEXT,
  34. ItemType TEXT,
  35. SalesChannel TEXT,
  36. OrderPriority INT,
  37. OrderDate DATE,
  38. OrderID BIGINT,
  39. ShipDate DATE,
  40. UnitsSold BIGINT,
  41. UnitPrice FLOAT,
  42. UnitCost FLOAT,
  43. TotalRevenue FLOAT,
  44. TotalCost FLOAT,
  45. TotalProfit FLOAT
  46. );'''.format(tableName))
  47. print("ici")
  48.  
  49. hCursorSQLite.execute('''SELECT * FROM "1500000_Sales_Records";''')
  50. datasql = hCursorSQLite.fetchall()
  51.  
  52. #Recuperer le nb de colonne pour l'insertion
  53. colcount=len(datasql[0])
  54.  
  55. forcolumn='%s,'*colcount
  56. forcolumn=forcolumn[:-1]
  57. # for j in range (len(datasql)):
  58. # for i in range (colcount):
  59.  
  60. hCursorPG.executemany("INSERT INTO commerce VALUES (%s);" % (forcolumn),datasql)
  61. hConnectPG.commit()
  62. print('Created')
  63.  
  64. # print("ici")
  65. # hCursorPG.executemany("INSERT INTO commerce VALUES %s,%s,%s,%s,%s,TO_DATE(%s,'DD/MM/YYYY'),%s,%s,%s,%s,%s,%s,%s,%s;",datasql)
  66. # hConnectPG.commit()
  67. # print('Created')
  68.  
  69. except psycopg2.Error:
  70. print('connection à la base de données à échouer')
  71. pass
  72.  
  73. #hCursorPG.execute('''CREATE TABLE "{}" (
  74. # Region TEXT,
  75. # Country TEXT,
  76. # ItemType TEXT,
  77. # SalesChannel TEXT,
  78. # OrderPriority INT,
  79. # OrderDate DATE,
  80. # OrderID BIGINT,
  81. # ShipDate DATE,
  82. # UnitsSold BIGINT,
  83. # UnitPrice FLOAT,
  84. # UnitCost FLOAT,
  85. # TotalRevenue FLOAT,
  86. # TotalCost FLOAT,
  87. # TotalProfit FLOAT
  88. # );'''.format(tableName))
  89. # print("ici")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement