~ Python, Databases and SQlite


Python, Databases and SQlite

Connecting to a database and creating your first table in Python and SQLite

It is as simple as it looks! Simple copy the code below, no need to download anything, and on "running" the program, it should create a little database for you. It may make sense for you to work in a specific folder, so you know where to look for the database once created

Code

import sqlite3

def create_table():
    conn = sqlite3.connect('test.db')
    print("Opened database")
    conn.execute('''CREATE TABLE STUDENT
         (ID INT PRIMARY KEY     NOT NULL,
         NAME           TEXT    NOT NULL,
         AGE            INT     NOT NULL,
         COMMENT        CHAR(50),
         POINTS         REAL);''')
    print("A database table has been created now")
    conn.close()


create_table()