<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    大漠駝鈴

    置身浩瀚的沙漠,方向最為重要,希望此blog能向大漠駝鈴一樣,給我方向和指引。
    Java,Php,Shell,Python,服務器運維,大數據,SEO, 網站開發、運維,云服務技術支持,IM服務供應商, FreeSwitch搭建,技術支持等. 技術討論QQ群:428622099
    隨筆 - 238, 文章 - 3, 評論 - 117, 引用 - 0
    數據加載中……

    Python Mysql

     

    http://www.kitebird.com/articles/pydbapi.html

    http://www.codegood.com/  python mysql驅動下載大全


    the Python standard for database interfaces is the Python DB-API. Most Python database interfaces adhere to this standard.

    You can choose the right database for your application. Python Database API supports a wide range of database servers:

    • GadFly
    • mSQL
    • MySQL
    • PostgreSQL
    • Microsoft SQL Server 2000
    • Informix
    • Interbase
    • Oracle
    • Sybase

    Here is the list of available Python databases interfaces:

    Python Database Interfaces and APIs
                

    You must download a separate DB API module for each database you need to access. For example, if you need to access an Oracle database as well as a MySQL database, you must download both the Oracle and the MySQL database modules.

    The DB API provides a minimal standard for working with databases, using Python structures and syntax wherever possible. This API includes the following:

    • Importing the api module.

    • Acquiring a connection with the database.

    • Issuing SQL statements and stored procedures.

    • Closing the connection

    We would learn all the concepts using MySQL so let's talk about MySQLdb module only.

    What is MySQLdb?

    MySQLdb is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2.0, and is built on top of the MySQL C API.

    How do I install the MySQLdb?

    Before proceeding you make sure you have MySQLdb installed on your machine. Just type the following in your Python script and execute it:

    #!/usr/bin/python
                import MySQLdb
                

    If it produces following result then it means MySQLdb module is not installed:

    Traceback (most recent call last):
                File "test.py", line 3, in <module>
                import MySQLdb
                ImportError: No module named MySQLdb
                

    To install MySQLdb module, download it from MySQLdb Download page and proceed as follows:

    $ gunzip MySQL-python-1.2.2.tar.gz
                $ tar -xvf MySQL-python-1.2.2.tar
                $ cd MySQL-python-1.2.2
                $ python setup.py build
                $ python setup.py install
                

    Note: Make sure you have root privilege to install above module.

    Database Connection:

    Before connecting to a MySQL database make sure followings:

    • You have created a database TESTDB.

    • You have created a table EMPLOYEE in TESTDB.

    • This table is having fields FIRST_NAME, LAST_NAME, AGE, SEX and INCOME.

    • User ID "testuser" and password "test123" are set to access TESTDB

    • Python module MySQLdb is installed properly on your machine.

    • You have gone through MySQL tutorial to understand MySQL Basics.

    Example:

    Following is the example of connecting with MySQL database "TESTDB"

    #!/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()
                # execute SQL query using execute() method.
                cursor.execute("SELECT VERSION()")
                # Fetch a single row using fetchone() method.
                data = cursor.fetchone()
                print "Database version : %s " % data
                # disconnect from server
                db.close()
                

    While running this script, its producing following result at my Linux machine.

    Database version : 5.0.45
                

    If a connection is established with the datasource then a Connection Object is returned and saved into db for further use otherwise db is set to None. Next db object is used to create a cursor object which in turn is used to execute SQL queries.

    Finally before coming out it ensures that database connection is closed and resources are released.

    Creating Database Table:

    Once a database connection is established, we are ready to create tables or records into the database tables using execute method of the created cursor.

    Example:

    First let's create Database table EMPLOYEE:

    #!/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()
                # Drop table if it already exist using execute() method.
                cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
                # Create table as per requirement
                sql = """CREATE TABLE EMPLOYEE (
                FIRST_NAME  CHAR(20) NOT NULL,
                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' )" % \
                ('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()
                

    Example:

    Following code segment is another form of execute 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 use either fetchone() method to fetch single record or fetchall method to fetech multiple values from a database table.

    • fetchone(): This method fetches the next row of a query result set. A result set is an object that is returned when 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 from the 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/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 = "SELECT * FROM EMPLOYEE \
                WHERE INCOME > '%d'" % (1000)
                try:
                # Execute the SQL command
                cursor.execute(sql)
                # Fetch all the rows in a list of lists.
                results = cursor.fetchall()
                for row in results:
                fname = row[0]
                lname = row[1]
                age = row[2]
                sex = row[3]
                income = row[4]
                # Now print fetched result
                print "fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \
                (fname, lname, age, sex, income )
                except:
                print "Error: unable to fecth data"
                # disconnect from server
                db.close()
                

    This will produce following result:

    fname=Mac, lname=Mohan, age=20, sex=M, income=2000
                

    Update Operation:

    UPDATE Operation on any databasse means to update one or more records which are already available in the database. Following is the procedure to update all the records having SEX as 'M'. Here we will increase AGE of all the males by one year.

    Example:

    #!/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 UPDATE required records
                sql = "UPDATE EMPLOYEE SET AGE = AGE + 1
                WHERE SEX = '%c'" % ('M')
                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()
                

    DELETE Operation:

    DELETE operation is required when you want to delete some records from your database. Following is the procedure to delete all the records from EMPLOYEE where AGE is more than 20.

    Example:

    #!/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 DELETE required records
                sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20)
                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()
                

    Performing Transactions:

    Transactions are a mechanism that ensures data consistency. Transactions should have the following four properties:

    • Atomicity: Either a transaction completes or nothing happens at all.

    • Consistency: A transaction must start in a consistent state and leave the system is a consistent state.

    • Isolation: Intermediate results of a transaction are not visible outside the current transaction.

    • Durability: Once a transaction was committed, the effects are persistent, even after a system failure.

    The Python DB API 2.0 provides two methods to either commit or rollback a transaction.

    Example:

    You already have seen how we have implemented transations. Here is again similar example:

    # Prepare SQL query to DELETE required records
                sql = "DELETE FROM EMPLOYEE WHERE AGE > '%d'" % (20)
                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()
                

    COMMIT Operation:

    Commit is the operation which gives a green signal to database to finalize the changes and after this operation no change can be reverted back.

    Here is a simple example to call commit method.

        db.commit()
                

    ROLLBACK Operation:

    If you are not satisfied with one or more of the changes and you want to revert back those changes completely then use rollback method.

    Here is a simple example to call rollback metho.

       db.rollback()
                

    Disconnecting Database:

    To disconnect Database connection, use close() method.

        db.close()
                

    If the connection to a database is closed by the user with the close() method, any outstanding transactions are rolled back by the DB. However, instead of depending on any of DB lower level implementation details, your application would be better off calling commit or rollback explicitly.

    Handling Errors:

    There are many sources of errors. A few examples are a syntax error in an executed SQL statement, a connection failure, or calling the fetch method for an already canceled or finished statement handle.

    The DB API defines a number of errors that must exist in each database module. The following table lists these exceptions.

    Exception Description
    Warning Used for non-fatal issues. Must subclass StandardError.
    Error Base class for errors. Must subclass StandardError.
    InterfaceError Used for errors in the database module, not the database itself. Must subclass Error.
    DatabaseError Used for errors in the database. Must subclass Error.
    DataError Subclass of DatabaseError that refers to errors in the data.
    OperationalError Subclass of DatabaseError that refers to errors such as the loss of a connection to the database. These errors are generally outside of the control of the Python scripter.
    IntegrityError Subclass of DatabaseError for situations that would damage the relational integrity, such as uniqueness constraints or foreign keys.
    InternalError Subclass of DatabaseError that refers to errors internal to the database module, such as a cursor no longer being active.
    ProgrammingError Subclass of DatabaseError that refers to errors such as a bad table name and other things that can safely be blamed on you.
    NotSupportedError Subclass of DatabaseError that refers to trying to call unsupported functionality.

    Your Python scripts should handle these errors but before using any of the above exceptions, make sure your MySQLdb has support for that exception. You can get more information about them by reading the DB API 2.0 specification.

    posted on 2011-03-04 16:09 草原上的駱駝 閱讀(1147) 評論(0)  編輯  收藏 所屬分類: Python

    主站蜘蛛池模板: 999久久久免费精品国产| 人妻在线日韩免费视频| 青青视频观看免费99| 亚洲白色白色在线播放| 亚洲综合日韩中文字幕v在线| 亚洲熟妇色自偷自拍另类| 香港a毛片免费观看| 免费国产小视频在线观看| 亚洲AV无码成人专区片在线观看| 亚洲欧洲日韩极速播放| 插B内射18免费视频| 亚洲色欲啪啪久久WWW综合网| AAA日本高清在线播放免费观看| 大学生一级特黄的免费大片视频| 亚洲VA中文字幕无码一二三区| 美女羞羞喷液视频免费| 亚洲精品一级无码鲁丝片| 中国videos性高清免费| 亚洲白色白色永久观看| 永久黄网站色视频免费直播 | 毛片a级毛片免费观看免下载 | 日本视频免费高清一本18| 久久青青成人亚洲精品| 一级毛片正片免费视频手机看| 无码人妻久久一区二区三区免费丨| 国产亚洲福利精品一区| 99在线精品免费视频九九视| 亚洲精品无码专区在线播放| 国产成人精品日本亚洲专区| 亚洲a一级免费视频| 国产午夜亚洲精品国产| 国产乱辈通伦影片在线播放亚洲 | 亚洲人成图片网站| 美女内射毛片在线看免费人动物| 亚洲日韩中文字幕在线播放| 99国产精品免费观看视频| 亚洲国产午夜精品理论片在线播放| 免费观看激色视频网站bd| 免费激情网站国产高清第一页| 国产成人精品免费直播| 污视频在线观看免费|