How to insert data in Table using Python
1.? Placeholder
2.Using python tuple and keyname placeholder
import sqlite3 db = sqlite3.connect('ganesha.db') id1 = 1 name1= 'yogesh' id2 = 2 name = 'vikas' cursor = db.cursor() # ? Placeholder # cursor.execute('insert into info3(id,name) values(?,?)',(id1,name1)) cursor.execute('insert into info3(id,name) values(:id,:name)',{'id':id2,'name':name}) db.commit()
agar hame, table me bahot sare members ek sath add karna ho to uska code kaise likhe ???
jaisa ki :
import sqlite3
db = sqlite3.connect(“ins.db”)
cursor = db.cursor()
sql = “””
CREATE TABLE IF NOT EXISTS students(Id INTEGER PRIMARY KEY AUTOINCREMENT,Name TEXT,Age INT, Gender TEXT)
“””
cursor.execute(sql) #this generate the table.
insert = “INSERT INTO students(Name, Age, Gender) VALUES(‘{}’,'{}’,'{}’)”.format(‘Nagesh aditya’,21,’M’)
insert = “INSERT INTO students(Name, Age, Gender) VALUES(‘{}’,'{}’,'{}’)”.format(‘aditya’,19,’M’)
insert = “INSERT INTO students(Name, Age, Gender) VALUES(‘{}’,'{}’,'{}’)”.format(‘adi’,20,’M’)
cursor.execute(insert)
db.commit()
is code me sirf last wale ka output show ho rha hai, kya ap mistake bata denge, please..!