Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Challenge: First Table | Introduction to SQLite
Databases in Python

book
Challenge: First Table

Завдання

Swipe to start coding

Your task is to complete the code that creates an SQLite database and a table called articles. Here’s what you need to do:

  1. Connect to the database my_database.db.
  2. Create a cursor to execute SQL commands.
  3. Use the cursor to create a table named articles with the following fields:
    • id — type INTEGER.
    • title — type TEXT.
    • content — type TEXT.
    • author — type TEXT.
  4. Save the changes and close the database connection.

Рішення

import sqlite3

conn = sqlite3.connect("my_database.db") # create a new database
cursor = conn.cursor() # create a cursor

# Create a table named `articles` with the specified fields
cursor.execute('''
CREATE TABLE IF NOT EXISTS articles (
id INTEGER,
title TEXT,
content TEXT,
author TEXT
)
''')
conn.commit() # save the changes
conn.close() # close the database connection
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 5
import sqlite3

conn = sqlite3.connect("my_database.db") # create a new database
cursor = ___.___() # create a cursor

# Create a table named `articles` with the specified fields
___.___('''
CREATE TABLE IF NOT EXISTS articles (
id INTEGER,
title TEXT,
content TEXT,
author TEXT
)
''')
conn.___() # save the changes
conn.___() # close the database connection
toggle bottom row
some-alt