create sqlite database and table in python

Solutions on MaxInterview for create sqlite database and table in python by the best coders in the world

showing results for - "create sqlite database and table in python"
Hanna
12 May 2020
1
2            
3                
4            
5         import sqlite3
6from sqlite3 import Error
7
8
9def create_connection(db_file):
10    """ create a database connection to the SQLite database
11        specified by db_file
12    :param db_file: database file
13    :return: Connection object or None
14    """
15    conn = None
16    try:
17        conn = sqlite3.connect(db_file)
18        return conn
19    except Error as e:
20        print(e)
21
22    return conn
23
24
25def create_table(conn, create_table_sql):
26    """ create a table from the create_table_sql statement
27    :param conn: Connection object
28    :param create_table_sql: a CREATE TABLE statement
29    :return:
30    """
31    try:
32        c = conn.cursor()
33        c.execute(create_table_sql)
34    except Error as e:
35        print(e)
36
37
38def main():
39    database = r"C:\sqlite\db\pythonsqlite.db"
40
41    sql_create_projects_table = """ CREATE TABLE IF NOT EXISTS projects (
42                                        id integer PRIMARY KEY,
43                                        name text NOT NULL,
44                                        begin_date text,
45                                        end_date text
46                                    ); """
47
48    sql_create_tasks_table = """CREATE TABLE IF NOT EXISTS tasks (
49                                    id integer PRIMARY KEY,
50                                    name text NOT NULL,
51                                    priority integer,
52                                    status_id integer NOT NULL,
53                                    project_id integer NOT NULL,
54                                    begin_date text NOT NULL,
55                                    end_date text NOT NULL,
56                                    FOREIGN KEY (project_id) REFERENCES projects (id)
57                                );"""
58
59    # create a database connection
60    conn = create_connection(database)
61
62    # create tables
63    if conn is not None:
64        # create projects table
65        create_table(conn, sql_create_projects_table)
66
67        # create tasks table
68        create_table(conn, sql_create_tasks_table)
69    else:
70        print("Error! cannot create the database connection.")
71
72
73if __name__ == '__main__':
74    main()Code language: Python (python)
queries leading to this page
create an sqlite database with pythonmake tables sqlite pythonsqlite make new database pythonmaking a new database sqlite pyhtonhow to create a database with sqllite with pythoncreate table sqlite3 pythonhow to make a sqlite database in pythonpython sqlite make tablehow to create a table in sqlite using pythoncreate sqlite database pythoncreate an sqlite database pythoncreate table in sqlite pythoncreate sqlite3 table pythonpython create sqlite databasecreate table in sqlite3 pythoncreate sqlite table pythoncreate sqlite3 database in pythonsqlite create table pythoncreate database sqlite pyhtonhow to create table with sqlite python python create sqlite database with tablespython script to create sqlite databasepython sqlite create tablepython code to create sqlite databasecreate table sqlite pythoncreate new sqlite database pythonsqlite python create tablecreate sqlite in pythonpython sqlite3 create database 5ccreate sqlite table pytohnpython create a sqlite database store dataframe as a tablepython sqlite3 create databasecreate a table in sqlite3 pythonhow to create sqllite db in pythonsqlite3 create table and database pythonsqlite3 python create an databasehow to create sql table in python sqlite3python sqlite create database sqlite3 python create tablehow to create database in python sqlite3sqlite3 create table pythoncreate table sqlite python sqlalchemysqlite create db pythoncreate a sqlite database pythonpython sqlite3 create tablepython sqlte create database with tablehow to create table in sqlite3 pythonsqlite3 python create databasesqlite in python3 create tablehow to create sqlite database in pythoncreate new table sqlite pythonsqlite python creat tablehow to create table in sqlite pythoncreate sqlite database in pythoncreate sqlite database and table in python