Александр К-ш

Зарегистрирован с 11.10.2020
Комментарии
11 октября 2020

Хотелось бы иметь полный код примера в файле для sqlite, который можно запустить и он будет работать сразу, а то копируя кусочки, не получается запустить. Выдает ошибку.

import sqlite3 from sqlite3 import Error

def create_connection(path): connection = None try: connection = sqlite3.connect(path) print("Connection to SQLite DB successful") except Error as e: print(f"The error '{e}' occurred")

return connection

connection = create_connection("sm_app.sqlite")

def execute_query(connection, query): cursor = connection.cursor() try: cursor.execute(query) connection.commit() print("Query executed successfully") except Error as e: print(f"The error '{e}' occurred")

create_users_table = """ CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER, gender TEXT, nationality TEXT ); """

execute_query(connection, create_users_table)

create_posts_table = """ CREATE TABLE IF NOT EXISTS posts( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, description TEXT NOT NULL, user_id INTEGER NOT NULL, FOREIGN KEY (user_id) REFERENCES users (id) ); """

execute_query(connection, create_posts_table)

FOREIGN KEY (user_id) REFERENCES users (id) FOREIGN KEY (post_id) REFERENCES posts (id) ); """

create_likes_table = """ CREATE TABLE IF NOT EXISTS likes ( id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER NOT NULL, post_id integer NOT NULL, FOREIGN KEY (user_id) REFERENCES users (id) FOREIGN KEY (post_id) REFERENCES posts (id) ); """

execute_query(connection, create_comments_table)
execute_query(connection, create_likes_table)

Ответить