Difference between revisions of "Python insert into mysql"
From MyWiki
(Created page with "<source lang="python"> #!/usr/bin/python import MySQLdb # Open database connection db = MySQLdb.connect("localhost","testuser","test123","TESTDB" ) # prepare a cursor objec...") |
|||
(One intermediate revision by the same user not shown) | |||
Line 26: | Line 26: | ||
db.close() | db.close() | ||
+ | </source> | ||
+ | Creating queries dynamically <br> | ||
+ | <source lang="python"> | ||
+ | #!/usr/bin/python | ||
+ | |||
+ | import MySQLdb | ||
+ | |||
+ | # Open database connection | ||
+ | db = MySQLdb.connect("localhost","testuser","test123","TESTDB" ) | ||
+ | |||
+ | # prepare a cursor object using cursor() method | ||
+ | cursor = db.cursor() | ||
+ | |||
+ | # Prepare SQL query to INSERT a record into the database. | ||
+ | sql = "INSERT INTO EMPLOYEE(FIRST_NAME, \ | ||
+ | LAST_NAME, AGE, SEX, INCOME) \ | ||
+ | VALUES ('%s', '%s', '%d', '%c', '%d' )" % \ | ||
+ | ('Mac', 'Mohan', 20, 'M', 2000) | ||
+ | try: | ||
+ | # Execute the SQL command | ||
+ | cursor.execute(sql) | ||
+ | # Commit your changes in the database | ||
+ | db.commit() | ||
+ | except: | ||
+ | # Rollback in case there is any error | ||
+ | db.rollback() | ||
+ | |||
+ | # disconnect from server | ||
+ | db.close() | ||
+ | |||
+ | |||
+ | |||
+ | </source> | ||
+ | |||
+ | Passing parameters directly<br> | ||
+ | |||
+ | <source lang="python"> | ||
+ | .................................. | ||
+ | user_id = "test123" | ||
+ | password = "password" | ||
+ | |||
+ | con.execute('insert into Login values("%s", "%s")' % \ | ||
+ | (user_id, password)) | ||
+ | .................................. | ||
</source> | </source> |
Latest revision as of 11:57, 25 February 2016
#!/usr/bin/python import MySQLdb # Open database connection db = MySQLdb.connect("localhost","testuser","test123","TESTDB" ) # prepare a cursor object using cursor() method cursor = db.cursor() # Prepare SQL query to INSERT a record into the database. sql = """INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES ('Mac', 'Mohan', 20, 'M', 2000)""" try: # Execute the SQL command cursor.execute(sql) # Commit your changes in the database db.commit() except: # Rollback in case there is any error db.rollback() # disconnect from server db.close()
Creating queries dynamically
#!/usr/bin/python import MySQLdb # Open database connection db = MySQLdb.connect("localhost","testuser","test123","TESTDB" ) # prepare a cursor object using cursor() method cursor = db.cursor() # Prepare SQL query to INSERT a record into the database. sql = "INSERT INTO EMPLOYEE(FIRST_NAME, \ LAST_NAME, AGE, SEX, INCOME) \ VALUES ('%s', '%s', '%d', '%c', '%d' )" % \ ('Mac', 'Mohan', 20, 'M', 2000) try: # Execute the SQL command cursor.execute(sql) # Commit your changes in the database db.commit() except: # Rollback in case there is any error db.rollback() # disconnect from server db.close()
Passing parameters directly
.................................. user_id = "test123" password = "password" con.execute('insert into Login values("%s", "%s")' % \ (user_id, password)) ..................................