Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python sqlite insert

import sqlite3

conn = sqlite3.connect('mysqlite.db')
c = conn.cursor()

#records or rows in a list
records = [(1, 'Glen', 8),
			(2, 'Elliot', 9),
			(3, 'Bob', 7)]

#insert multiple records in a single query
c.executemany('INSERT INTO students VALUES(?,?,?);',records);

print('We have inserted', c.rowcount, 'records to the table.')

#commit the changes to db			
conn.commit()
#close the connection
conn.close()
Comment

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()
Comment

insert data in sqlite database in python

import sqlite3
# it will create a databse with name sqlite.db
connection= sqlite3.connect('sqlite.db') 
cursor= connection.cursor()
table_query = '''CREATE TABLE Student
               (Name text, Course text, Age real)'''
               
cursor.execute(table_query)
# you need to commit changes as well
connection.commit()
# you also need to close  the connection
connection.close()   
Comment

PREVIOUS NEXT
Code Example
Python :: download unsplash images script 
Python :: download unsplash images python no api 
Python :: how ro have a incresing variable in python 
Python :: axis labels python 
Python :: functions in python 
Python :: python typecast 
Python :: cite pandas python 
Python :: how to add labels on bar of barchart seaborn 
Python :: python sliding window 
Python :: beautifulsoup getting data from a website 
Python :: best scraping package in python 
Python :: json to argparse 
Python :: how to make a python program on odd and even 
Python :: python array join 
Python :: how to make curl request python 
Python :: pandas df by row index 
Python :: python any in list 
Python :: python tkinter button dynamic button command 
Python :: urllib.error.HTTPError: HTTP Error 404: Not Found 
Python :: Python using webbrowser 
Python :: NLP text summarization with Luhn 
Python :: login url 
Python :: python slice list 
Python :: Normalize columns in pandas dataframe 
Python :: how to remove a list of numbers from a list in python 
Python :: sort a dictionary by value then key 
Python :: super title python 
Python :: string list to list 
Python :: python append row to 2d array 
Python :: list to dic 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =