Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- """
- Created on Wed Sep 26 15:26:59 2018
- @author: pinateth
- """
- #https://atcoordinates.info/2017/07/24/copying-tables-from-sqlite-to-postgresql/
- import psycopg2
- import sqlite3
- try :
- #Connecteur
- hConnectPG = psycopg2.connect("dbname='MaBase' host='127.0.0.1' user='pinateth' password='pinateth'")
- hConnectSQLite = sqlite3.connect("/data/commerce/BaseSqlite.db")
- print('la connexion est effective')
- #curseur
- hCursorSQLite = hConnectSQLite.cursor()
- hCursorPG = hConnectPG.cursor()
- #On connait le nom de la table, pas besoin de faire un script pour sélecitonner les autres tables sinon voir
- tableName = '1500000_Sales_Records'
- print('la connexion est effective2')
- #Création de la table avec une
- hCursorPG.execute('''CREATE TABLE "{}" (
- Region TEXT,
- Country TEXT,
- ItemType TEXT,
- SalesChannel TEXT,
- OrderPriority INT,
- OrderDate DATE,
- OrderID BIGINT,
- ShipDate DATE,
- UnitsSold BIGINT,
- UnitPrice FLOAT,
- UnitCost FLOAT,
- TotalRevenue FLOAT,
- TotalCost FLOAT,
- TotalProfit FLOAT
- );'''.format(tableName))
- print("ici")
- hCursorSQLite.execute('''SELECT * FROM "1500000_Sales_Records";''')
- datasql = hCursorSQLite.fetchall()
- #Recuperer le nb de colonne pour l'insertion
- colcount=len(datasql[0])
- forcolumn='%s,'*colcount
- forcolumn=forcolumn[:-1]
- # for j in range (len(datasql)):
- # for i in range (colcount):
- hCursorPG.executemany("INSERT INTO commerce VALUES (%s);" % (forcolumn),datasql)
- hConnectPG.commit()
- print('Created')
- # print("ici")
- # 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)
- # hConnectPG.commit()
- # print('Created')
- except psycopg2.Error:
- print('connection à la base de données à échouer')
- pass
- #hCursorPG.execute('''CREATE TABLE "{}" (
- # Region TEXT,
- # Country TEXT,
- # ItemType TEXT,
- # SalesChannel TEXT,
- # OrderPriority INT,
- # OrderDate DATE,
- # OrderID BIGINT,
- # ShipDate DATE,
- # UnitsSold BIGINT,
- # UnitPrice FLOAT,
- # UnitCost FLOAT,
- # TotalRevenue FLOAT,
- # TotalCost FLOAT,
- # TotalProfit FLOAT
- # );'''.format(tableName))
- # print("ici")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement