To create an SQLite database

import sqlite3

def create_table():
   con = sqlite3.connect('naverDB')     

   cursor = con.cursor()                

# To create user table
   cursor.execute('''create table user(
                       id char(20),
                       username char(20),
                       email char(20),
                       birth char(20) )
                  ''')

   con.commit()
   con.close()

if __name__=='__main__':
    create_table()

To insert data with insert SQL:

import sqlite3

try:
    # To connect to the database
    con = sqlite3.connect('naverDB')

    cursor = con.cursor()
    
    while True:
        data1 = input('User ID?')
        if data1 == '':
            break
        data2 = input('User Name?')
        data3 = input('User Email?')
        data4 = input('User Date of Birth?')

        # sql = "insert into user values('" + data1 + "','" + data2 + "','" + data3 + "','" + data4 + "')"
        sql = "insert into user values(?, ?, ?, ?)"
        cursor.execute(sql, (data1,data2,data3,data4))

    con.commit()

except Exception as err:
    print(err)
    print('Failed to connect to the database')
finally:
    con.close()

To search the inserted data


import sqlite3

try:
    # To connect to the database
    con = sqlite3.connect('naverDB')

    cursor = con.cursor()

    cursor.execute('select * from user')

    print( User ID\t User Name\t User Email\t User DOB')
    print('---------------------------------------')

    # row = cursor.fetchone()                 
    # print(type(row))                        # <class 'tuple'>
    # print(row)                             

    rows = cursor.fetchall()                  # To search all
    print(type(rows))                         # <class 'list'>
    print(rows)

    for r in rows:
        print(r[0], r[1], r[2], r[3])

except Exception as err:
    print(err)
    print('Failed to connect database')
finally:

To update 


import sqlite3

try:
    con = sqlite3.connect('naverDB')

    cursor = con.cursor()

    id = input('Insert User ID.')
    email = input('Insert a new user email.')

    sql = "update user set email=? where id=?"
    cursor.execute(sql, (email, id))

    con.commit()

    cursor.execute('select * from user')
    rows = cursor.fetchall()
    for r in rows:
        print(r[0],r[1],r[2],r[3])

except Exception as err:
    print(err)
    print('Failed to connect to database')
finally:
    con.close()

To delete

import sqlite3

try:
    con = sqlite3.connect('naverDB')

    cursor = con.cursor()

    id = input('Insert User ID to delete.')

    sql = 'delete from user where id = ?'

    cursor.execute(sql, (id,) )
    con.commit()
    print('Successfully deleted.')

    cursor.execute('select * from user')
    rows = cursor.fetchall()
    for r in rows:
        print(r[0],r[1],r[2],r[3])

except Exception as err:
    print(err)
    print('Failed to connect')
finally:
    con.close()

'Python' 카테고리의 다른 글

Python) Regular Expression  (0) 2022.11.21
Python) Database connection with MySQL  (0) 2022.11.20
Python) Class and Method  (0) 2022.11.18
Python) Database connection with Oracle  (0) 2022.11.18
Python) try: except:  (0) 2022.11.14

+ Recent posts