Skip to content
Permalink
main
Switch branches/tags

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?
Go to file
 
 
Cannot retrieve contributors at this time
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()