Python mysqldb Module connection operation MySQL DB instance _python

Source: Internet
Author: User

MySQL is a good open source database, it is now widely used, so it is necessary to briefly introduce the method of using Python to operate the MySQL database. The Python operations database requires the installation of a third-party module, which is downloaded and documented in http://mysql-python.sourceforge.net/.

Since Python's database module has specialized database module specifications, in fact, no matter what kind of database used in a similar way, here is a demonstration of the code:

#-*-encoding:gb2312-*-import os, sys, stringimport mysqldb# connection Database Try:conn = MySQLdb.connect (h ost= ' localhost ', user= ' root ', passwd= ' xxxx ', db= ' test1 ') except Exception, E:print e sys.exit () # Gets the cursor object to manipulate the cursor = Conn.cursor () # CREATE TABLE sql = "CREATE table if not exists test1 (name varchar (+) primary key, age int (4))" Cursor.execute (SQL) #  Insert Data sql = "INSERT into test1 (name, age) VALUES ('%s ',%d)"% ("Zhaowei", "") try:cursor.execute (SQL) except Exception, E:  Print esql = "INSERT into test1 (name, age) VALUES ('%s ',%d)"% ("Zhang San", +) Try:cursor.execute (SQL) except Exception, E: Print e# insert Multiple sql = "INSERT into test1 (name, age) values (%s,%s)" val = (("John Doe", 24), ("Harry", 25), ("Hung Six", "Up") TRY:CURSOR.E Xecutemany (SQL, Val) except Exception, E:print e# querying out data sql = "SELECT * from Test1" cursor.execute (sql) AllData = cursor.fetc Hall () # If there is data returned, on the loop output, AllData is a two-dimensional list if Alldata:for rec in Alldata:print rec[0], Rec[1]cursor.close () conn.close () 
   

The above is the cloud Community small series for your carefully prepared content, in the cloud Habitat Community Blog, quiz, public number, people, courses and other related content, welcome to continue using the upper right corner search button to search Python mysqldb module mysqldb MySQL python, MySQL Database design example, MySQL database instance, MySQL database development instance, MySQL database instance download, so that you get more relevant knowledge.

Python mysqldb Module connection operation MySQL DB instance _python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.