Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

find duplicate row in python sqlite database

for name in ('AlixaProDev','Salamn'): 
    cursor.execute("SELECT rowid FROM Student WHERE Name = ?", (name,))
    db_result=cursor.fetchall()
    if len(db_result)==0:
        print('There is no Student named %s'%name)
    else:
        print('Student %s found with rowids %s'%(name,','.join(map(str, next(zip(*db_result))))))Copy Code
Comment

python code to find duplicate row in sqlite database

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],
  ['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}')"
    # check if a row is already exist 
    cursor.execute("SELECT rowid FROM Student WHERE Name = ?", (name,))
    db_result=cursor.fetchall()
    if len(db_result)==0:
        cursor.execute(q)
        print('Studnet Data inserted Successfully')
    else:
        print('Student %s found with rowids %s'%(name,','.join(map(str, next(zip(*db_result))))))
  
        
# you need to commit changes as well
connection.commit()
# you also need to close  the connection
connection.close()Copy Code
Comment

PREVIOUS NEXT
Code Example
Python :: how to extract a list of values from numpy array using index list 
Python :: tqdm start bar at 
Python :: Set changed size during iteration 
Python :: Python NumPy asanyarray Function Example Scalar to an array 
Python :: Python NumPy ascontiguousarray Function Example Tuple to an array 
Python :: Python NumPy concatenate Function Example when axis equal to none 
Python :: Python NumPy row_stack Function Syntax 
Python :: how to change text in heatmap matplotlib 
Python :: Python NumPy insert Function Example Working with Scalars 
Python :: structure conditionnelle python 
Python :: add text to pdf file in python 
Python :: Python how to use __ge__ 
Python :: NumPy rot90 Example Rotating Once 
Python :: track keyboard press pynput 
Python :: NumPy invert Code When the input is a number 
Python :: django view - apiview decorator (retrieve, update or delete - GET, PUT, DELETE) 
Python :: python override inherited method data model constructor 
Python :: Python PEP (class) 
Python :: call a Python range() using range(start, stop) 
Python :: Visual Studio Code pylint: Error when all is ok 
Python :: pandas dataframe select columns multiple cell value 
Python :: Examples of incorrect code for this rule: 
Python :: phlib examples python 
Python :: Trying to use image in Flask website only shows broken img icon 
Python :: cours python 
Python :: connect labjack to python 
Python :: ring get the type a given path (file or directory) 
Python :: ring Type Hints Library user types 
Python :: matplotlib plot dpi - change format to retina instead of svg 
Python :: vreverse all elemetns of a list in place python 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =