def insert(title): title = "'"+Request(title)['Title']+"'" sel = select(column='title', where='title= '+ title) if sel==[]: sql_insert = """insert into MOVIES (TITLE) VALUES ("""+title+""");""" conn = create_connection(path()) if conn is not None: execSql(conn, sql_insert) else: print("Error! cannot create the database connection.") trim() title = 'title = ' + title for row in select(column='title', where=title): for key, val in Request(row[0]).items(): for column in getcolumn(): if key.lower() == column.lower(): if key.lower() == 'title': title = val continue update(key, val, title) else: print('There is already film named ' + title)
def getcolumn(): sql_insert = """SELECT name FROM PRAGMA_TABLE_INFO('MOVIES');""" conn = create_connection(path()) if conn is not None: try: c = conn.cursor() c.execute(sql_insert) conn.commit() rows = c.fetchall() columnname = [] for row in rows: if row[0] == 'ID': continue columnname.append(row[0]) except Error as e: print(e) else: print("Error! cannot create the database connection.") return columnname
def trim(): conn = create_connection(path()) sql_insert = """UPDATE MOVIES SET TITLE = trim(TITLE);""" if conn is not None: execSql(conn, sql_insert) else: print("Error! cannot create the database connection.")
def update(column, val, title): conn = create_connection(path()) val = val.replace('\'', '\'\'') sql_insert = """UPDATE MOVIES SET """ + column + """='""" + val + """' WHERE TITLE='""" + title + """';""" print(sql_insert) if conn is not None: execSql(conn, sql_insert) else: print("Error! cannot create the database connection.")
def select(where='id>', val='0', sort='id', sortway='asc', column='*'): if sort == 'cast': sort = '`cast`' if where == 'cast': where = '`cast`' if where == 'id>': sql_select = """select """ + column + """ from MOVIES where """ + where + """=""" + val + """ order by """ + sort + """ """ + sortway + """;""" else: sql_select = """select """ + column + """ from MOVIES where """ + where + """ order by """ + sort + """ """ + sortway + """;""" #print(sql_select) conn = create_connection(path()) if conn is not None: selected = execSql(conn, sql_select) return selected else: print("Error! cannot create the database connection.")