Create Table

Date: March 11th 2016
Last updated: March 11th 2016

Making a table follows the same basic syntax seen in getting started with sqlite; connect, create cursor, then execute SQL command.

import sqlite3

# Connecting to the database file
conn = sqlite3.connect('gitbook.db') 
c = conn.cursor()

# Creating a new SQLite table with 1 column
c.execute('CREATE TABLE {tn} ({nf} {ft} PRIMARY KEY)'\
        .format(tn='scoreboard', nf='score',ft='DECIMAL'))

# Committing changes and closing the connection to the database file
conn.commit()
conn.close()

You can check this worked by using sqlite3 shell.

# connect to database
[email protected]:~/$ sqlite3 gitbook.db
#SQLite version 3.8.2 2013-12-06 14:53:30
#Enter ".help" for instructions
#Enter SQL statements terminated with a ";"
sqlite> .table
# scoreboard

sqlite> PRAGMA table_info(scoreboard)
# 0|score|DECIMAL|0||1

Or view the database schema using a GUI. The following screenshot is of Sqliteman installed on Ubuntu LTS 14.04 using software manager.

sqlite make table screenshot

results matching ""

    No results matching ""