MySQL Table में record insert करने के लिए हमें define किये गए attributes / columns and उनके type के according values insert करनी होती हैं। और जिन fields में value insert करनी होती है , सिर्फ उन्ही fields को select किया जाता है।


अगर आपको याद हो, पिछले Topic में एक tbl_users name की table बनायी थी जिसे RENAME करके users कर दिया था , उसी table में हम कुछ records को INSERT करेंगे।


users table में 4 attributes / columns define किये गए थे जिनमे से 1 attributes id primary key बनाया था और उसे AUTO_INCREMENT set किया था। means जैसे ही कोई record insert होगा इसकी value automatically increase होती रहेगी।

Python MySQL Insert Example

Python में Insert करने के लिए आपको insert करने वाले columns के लिए %s pass करना होता है then जो भी values insert करनी है उसका tuple बनाकर execute() method में pass किया जाता है।

Copy Fullscreen Close Fullscreen
#import mysql module's connector class.
import mysql.connector as myconn
mydb = myconn.connect(
  host = 'localhost',
  user = 'root',
  password = '',
  database = 'python_db'
)
db_cursor = mydb.cursor()
query = "INSERT INTO users(first_name, last_name, email) VALUES (%s, %s, %s)"
insert_values = ('Rahul', 'Kumar', 'myemail@gmail.com')
db_cursor.execute(query, insert_values)
mydb.commit()
# db_cursor.rowcount returns the number of records inserted just now.
print(db_cursor.rowcount, 'Record Inserted')
Output
C:\Users\Rahulkumar\Desktop\python>python mysql_insert.py
1 Record Inserted

rowcount property अभी insert किये गए records का number return करता है।

Note : query execute करने के बाद कभी भी commit() करना न भूले , नहीं table में records insert नहीं होंगे।


हालाँकि अगर आप चाहें तो , normally MySQL query में ही values को pass करते हैं वैसे भी record insert कर सकते हैं।
For Example -

db_cursor = mydb.cursor()
query = "INSERT INTO users(first_name, last_name, email) VALUES ('Mohit', 'Kumar', 'xyz@gmail.com')"
db_cursor.execute(query)
mydb.commit()

Note* जहाँ तक हो सके query में direct values रखकर records insert न करें, इससे SQL Injection के chances बढ़ जाते हैं। इसलिए हमेशा पहला तरीका ही use करें।

Note*

जिन भी columns की default values set होती हैं , उन्हें आप insert न भी करो तो वहां पर default value set हो जाती है। जैसे users table में email की default value NULL है।
See Example -

db_cursor = mydb.cursor()
query = "INSERT INTO users(first_name, last_name) VALUES (%s, %s)"
insert_values = ('Girish', 'Kumar')
db_cursor.execute(query, insert_values)
mydb.commit()

Python MySQL Insert Multiple Records

Table में multiple records को एक साथ insert करने के लिए executemany() method का use किया जाता है , यह method insert values के tuples की list as a parameter accept करता है।

Python MySQL Insert Multiple Records Example
Copy Fullscreen Close Fullscreen
import mysql.connector as myconn
mydb = myconn.connect(
  host = 'localhost',
  user = 'root',
  password = '',
  database = 'python_db'
)
db_cursor = mydb.cursor()
query = "INSERT INTO users(first_name, last_name, email) VALUES (%s, %s, %s)"
# list of tuples.
insert_values = [
  ('Gyan Singh', 'Yadav', 'gyanxyz@email.com'),
  ('Mohan', 'Singh', 'mohan1232@email.com'),
  ('Sohan', 'singh', 'sihan1321@email.com')
]
db_cursor.executemany(query, insert_values)
mydb.commit()
print(db_cursor.rowcount, 'Record Inserted')
Output
C:\Users\Rahulkumar\Desktop\python>python insert_multiple.py
3 Record Inserted

तो कुछ इस तरह से Python में MySQL Table में multiple records को एक साथ insert करते हैं।

Python MySQL Get Inserted id

कभी कभी हमें हाल ही में insert किये गए record की id की जरूरत पड़ती है। cursor की lastrowid property का use करके आप सबसे last में inserted record की id get कर सकते हैं।
See Example -

Copy Fullscreen Close Fullscreen
import mysql.connector as myconn
mydb = myconn.connect(
  host = 'localhost',
  user = 'root',
  password = '',
  database = 'python_db'
)
db_cursor = mydb.cursor()
query = "INSERT INTO users(first_name, last_name) VALUES (%s, %s)"
insert_value = ('Raju', 'verma')
db_cursor.execute(query, insert_value)
mydb.commit()
print('Last inserted records\'s id is :', db_cursor.lastrowid)
Output
C:\Users\Rahulkumar\Desktop\python>python insert_multiple.py
Last inserted records's id is : 8

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers