Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
posting-permissions/posting.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
50 lines (39 sloc)
1.21 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
# Connect to the SQLite database file | |
conn = sqlite3.connect(':memory:') | |
# Create a cursor object to interact with the database | |
cursor = conn.cursor() | |
# Create a table for users | |
cursor.execute('''CREATE TABLE IF NOT EXISTS users ( | |
id INTEGER PRIMARY KEY AUTOINCREMENT, | |
username TEXT NOT NULL, | |
name TEXT NOT NULL, | |
role TEXT NOT NULL | |
)''') | |
# Create a table for posts | |
cursor.execute('''CREATE TABLE IF NOT EXISTS posts ( | |
id INTEGER PRIMARY KEY AUTOINCREMENT, | |
user_id INTEGER, | |
content TEXT NOT NULL, | |
FOREIGN KEY (user_id) REFERENCES users (id) | |
)''') | |
# Create a table for comments | |
cursor.execute('''CREATE TABLE IF NOT EXISTS comments ( | |
id INTEGER PRIMARY KEY AUTOINCREMENT, | |
post_id INTEGER, | |
user_id INTEGER, | |
content TEXT NOT NULL, | |
FOREIGN KEY (post_id) REFERENCES posts (id), | |
FOREIGN KEY (user_id) REFERENCES users (id) | |
)''') | |
# Commit the changes | |
conn.commit() | |
def insert_post_data(id, post_content): | |
conn = sqlite3.connect('community.db') | |
cursor = conn.cursor() | |
# Insert the post data into the posts table | |
cursor.execute('''INSERT INTO posts (id, content) | |
VALUES (?, ?)''', (id, post_content)) | |
# Commit the changes | |
conn.commit() | |
conn.close() | |