karideo/back/transfer_bdd/v0.0...v1.0/transfert_univers.py

90 lines
1.8 KiB
Python
Raw Normal View History

#!/usr/bin/python3
# -*- coding: utf-8 -*-
##
## @author Edouard DUPIN
##
## @copyright 2012, Edouard DUPIN, all right reserved
##
## @license MPL v2.0 (see license file)
##
#pip install paho-mqtt --user
from realog import debug
import json
import os
import random
import copy
from dateutil import parser
import datetime
2020-02-18 18:43:18 +01:00
import db
connection = db.connect_bdd();
2020-02-15 09:08:40 +01:00
def file_read_data(path):
if not os.path.isfile(path):
return ""
file = open(path, "r")
data_file = file.read()
file.close()
return data_file
debug.info("Load old BDD: ")
data = file_read_data('bdd_univers.json')
my_old_bdd = json.loads(data)
debug.info("create the table:")
2020-02-18 18:43:18 +01:00
c = connection.cursor()
# Create table
c.execute('''
2020-02-15 09:08:40 +01:00
CREATE TABLE univers (
2020-02-18 18:43:18 +01:00
id SERIAL PRIMARY KEY,
deleted BOOLEAN,
create_date TIMESTAMPTZ NOT NULL,
modify_date TIMESTAMPTZ NOT NULL,
name TEXT NOT NULL,
description TEXT,
2020-02-18 18:43:18 +01:00
covers INTEGER[] REFERENCES data(id))
''')
def list_to_string(data):
out = "";
for elem in data:
if out != "":
out += "/"
out +=str(elem)
return out
#sqlite3 bdd_group.db3 "SELECT * from data"
debug.info("insert elements: ")
iii = 0;
for elem in my_old_bdd:
iii+=1;
debug.info("[" + str(iii) + "/" + str(len(my_old_bdd)) + "] send new element")
id = elem["id"]
name = elem["name"]
if "description" not in elem.keys():
description = None
else:
description = elem["description"]
if "covers" not in elem.keys():
covers = []
else:
covers = elem["covers"]
if covers == None:
covers = [];
2020-02-18 18:43:18 +01:00
request_insert = (id, name, description, covers)
c.execute('INSERT INTO univers VALUES (%s,false,now,now,%s,%s,%s)', request_insert)
# Save (commit) the changes
2020-02-18 18:43:18 +01:00
connection.commit()
# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
2020-02-18 18:43:18 +01:00
connection.close()