Python Tutorial ( PDFDrive )
LAST_NAME CHAR(20),AGE INT,SEX CHAR(1),INCOME FLOAT )"""cursor.execute(sql)# disconnect from serverdb.close()INSERT Operation: INSERT operation is required when you want to create your records into a database table.EXAMPLE: Following is the example, which executes SQL INSERT statement to create a record into EMPLOYEE table:#!/usr/bin/pythonimport MySQLdb# Open database connectiondb = MySQLdb.connect("localhost","testuser","test123","TESTDB" )# prepare a cursor object using cursor() methodcursor = 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 commandcursor.execute(sql)# Commit your changes in the databasedb.commit()except:# Rollback in case there is any errordb.rollback()# disconnect from serverdb.close()Above example can be written as follows to create SQL queries dynamically:#!/usr/bin/pythonimport MySQLdb# Open database connectiondb = MySQLdb.connect("localhost","testuser","test123","TESTDB" )# prepare a cursor object using cursor() methodcursor = 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' )" % \TUTORIALS POINT Simply Easy Learning
('Mac', 'Mohan', 20, 'M', 2000)try:# Execute the SQL commandcursor.execute(sql)# Commit your changes in the databasedb.commit()except:# Rollback in case there is any errordb.rollback()# disconnect from serverdb.close()EXAMPLE: Following code segment is another form of execution where you can pass parameters directly:..................................user_id = "test123"password = "password"con.execute('insert into Login values("%s", "%s")' % \(user_id, password))..................................READ Operation: READ Operation on any databasse means to fetch some useful information from the database.Once our database connection is established, we are ready to make a query into this database. We can useeither fetchone() method to fetch single record or fetchall() method to fetech multiple values from a databasetable.• fetchone(): This method fetches the next row of a query result set. A result set is an object that is returnedwhen a cursor object is used to query a table.• fetchall(): This method fetches all the rows in a result set. If some rows have already been extracted fromthe result set, the fetchall() method retrieves the remaining rows from the result set.• rowcount: This is a read-only attribute and returns the number of rows that were affected by an execute()method.EXAMPLE: Following is the procedure to query all the records from EMPLOYEE table having salary more than 1000:#!/usr/bin/pythonimport MySQLdb# Open database connectiondb = MySQLdb.connect("localhost","testuser","test123","TESTDB" )# prepare a cursor object using cursor() methodcursor = db.cursor()# Prepare SQL query to INSERT a record into the database.sql = "SELECT * FROM EMPLOYEE \WHERE INCOME > '%d'" % (1000)try:# Execute the SQL commandTUTORIALS POINT Simply Easy Learning
- Page 291 and 292: # stat informationstatinfo = os.sta
- Page 293 and 294: Description The method tcgetpgrp()
- Page 295 and 296: os.tempnam(dir, prefix)Parameters
- Page 297 and 298: Description The method ttyname() re
- Page 299 and 300: Return Value This method does not
- Page 301 and 302: Return Value This method returns t
- Page 303 and 304: KeyErrorNameErrorUnboundLocalErrorE
- Page 305 and 306: ......................else:If there
- Page 307 and 308: print "Error: can\'t find file or r
- Page 309 and 310: self.args = argSo once you defined
- Page 311 and 312: 'Optional class documentation strin
- Page 313 and 314: For the above class let's try to ac
- Page 315 and 316: print 'Calling child method'c = Chi
- Page 317 and 318: Data Hiding: An object's attribute
- Page 319 and 320: groups()This method returns all mat
- Page 321 and 322: SYNTAX: re.sub(pattern, repl, strin
- Page 323 and 324: \s Matches whitespace. Equivalent t
- Page 325 and 326: Backreferences: This matches a prev
- Page 327 and 328: CHAPTER20Python CGI ProgrammingWhat
- Page 329 and 330: If you click hello.py, then this pr
- Page 331 and 332: Simple URL Example : Get Metho
- Page 333 and 334: </form>The result of this code is t
- Page 335 and 336: text_content = form.getvalue('textc
- Page 337 and 338: Here is an example of how to retrie
- Page 339 and 340: CHAPTER21Python Database AccessThe
- Page 341: EXAMPLE: Following is the example o
- Page 345 and 346: DELETE Operation: DELETE operation
- Page 347 and 348: ProgrammingErrorSubclass of Databas
- Page 349 and 350: The socket Module: To create a so
- Page 351 and 352: Protocol Common function Port No Py
- Page 353 and 354: Subject: SMTP e-mail testThis is a
- Page 355 and 356: try:smtpObj = smtplib.SMTP('localho
- Page 357 and 358: count = 0while count < 5:time.sleep
- Page 359 and 360: Synchronizing Threads: The threadi
- Page 361 and 362: thread = myThread(threadID, tName,
- Page 363 and 364: <stars>10</stars><description>Talk
- Page 365 and 366: print "Rating:", self.ratingelif se
- Page 367 and 368: Format: DVDRating: PGDescription: T
- Page 369 and 370: Tkinter Widgets Tkinter provides v
- Page 371 and 372: fgfontheighthighlightcolorimagejust
- Page 373 and 374: WidthXscrollincrementXscrollcommand
- Page 375 and 376: pattern when it is over the checkbu
- Page 377 and 378: The Entry widget is used to accept
- Page 379 and 380: E1 = Entry(top, bd =5)E1.pack(side
- Page 381 and 382: Optionanchorbgbitmapbdcursorfontfgh
- Page 383 and 384: selectmodeDetermines how many items
- Page 385 and 386: Syntax: Here is the simple syntax t
- Page 387 and 388: Menu The goal of this widget is to
- Page 389 and 390: filemenu.add_command(label="Exit",
- Page 391 and 392: Example: Try the following example
LAST_NAME CHAR(20),
AGE INT,
SEX CHAR(1),
INCOME FLOAT )"""
cursor.execute(sql)
# disconnect from server
db.close()
INSERT Operation:
INSERT operation is required when you want to create your records into a database table.
EXAMPLE:
Following is the example, which executes SQL INSERT statement to create a record into EMPLOYEE table:
#!/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()
Above example can be written as follows to create SQL 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' )" % \
TUTORIALS POINT
Simply Easy Learning