Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

creating a sqlite3 table in python and inserting data in it

import sqlite3
# it will create a databse with name sqlite.db
connection= sqlite3.connect('sqlite.db') 
cursor= connection.cursor()
table_query = '''CREATE TABLE  if not Exists Student
               (Name text, Course text, Age real)'''
       
cursor.execute(table_query)   

# student list 

students_data = [
 ['AlixaProDev','CS',19],
 ['Alixawebdev','BBa',21],
  ['AskALixa','Software',22]
]
insert_q = []


# creating the insert query for each student
for std_data in students_data:
    name = std_data[0]
    course = std_data[1]
    age = std_data[2]
    q=f"INSERT INTO Student VALUES ('{name}','{course}','{age}')"
    insert_q.append(q)

# executing the insert queries
for q in insert_q:
    cursor.execute(q)


# you need to commit changes as well
connection.commit()
# you also need to close  the connection
connection.close()
Source by www.alixaprodev.com #
 
PREVIOUS NEXT
Tagged: #creating #table #python #inserting #data
ADD COMMENT
Topic
Name
1+3 =