Installation and use of MYSQLDB

Source: Internet
Author: User

First, installation

Install the compiled version number (this method is quick and easy):

Http://www.codegood.com/downloads

Download according to your own system, double-click Install, fix

Then import MySQLdb. See if it's successful

My, win7,32 bit, 2.7 version number

Mysql-python-1.2.3.win-amd32-py2.7.exe

Second, use

#!/usr/bin/python
# Encoding:utf-8
Import Time,mysqldb
# Open Database connection
db = MySQLdb.connect ("localhost", "root", "root", "Python")
# get an action cursor using the cursor () method
cursor = Db.cursor ()

#删除表
sql = "DROP table if exists Thinkgamer"
Cursor.execute (SQL)

#创建
sql = "CREATE table if not exists Thinkgamer (name varchar (+) Primary key,created int (10))"
Cursor.execute (SQL)

#写入
sql = "INSERT into Thinkgamer (name,created) VALUES (%s,%s)"
param = ("AAA", Int (Time.time ()))
n = cursor.execute (Sql,param)
print ' Insert ', n

#写入多行
sql = "INSERT into Thinkgamer (name,created) VALUES (%s,%s)"
param = (("BBB", Int (Time.time ())), ("CCC", +), ("ddd", 44))
n = Cursor.executemany (Sql,param)
Print "Insertmany", n

#更新
sql= "Update Thinkgamer set name=%s where name= ' aaa '"
param = ("zzz")
n = cursor.execute (Sql,param)
Print "Updata", n

#查询
n = Cursor.execute ("SELECT * from Thinkgamer")
For row in Cursor.fetchall ():
Print row
For R in row:
Print R

#删除
sql = "Delete from Thinkgamer where name =%s"
param = ("BBB")
n = cursor.execute (Sql,param)
Print "Delete", n

#查询
n = Cursor.execute ("SELECT * from Thinkgamer")
Print Cursor.fetchall ()

Cursor.close ()

#提交
Db.commit ()
#关闭
Db.close ()

Output Result:

Insert 1
Insertmany 3
Updata 1
(' zzz ', 1436067892L)
zzz
1436067892
(' BBB ', 1436067892L)
Bbb
1436067892
(' CCC ', 33L)
Ccc
33
(' DDD ', 44L)
Ddd
44
Delete 1
(' zzz ', 1436067892L), (' CCC ', 33L), (' DDD ', 44L))


A lot of other details please poke: mysqldb User ' s Guide

Installation and use of MYSQLDB

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.