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()
读取操作:
fetchone(): 这种方法获取查询结果集的下一行。结果集是一个对象时,将返回一个游标对象用于查询表.
fetchall(): 这种方法获取结果集的所有行。如果某些行已经从结果集中提取,fetchAll()方法检索结果集的其余行.
rowcount: 这是一个只读属性,返回受影响的行数execute()方法.
#!/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()
更新操作:
对任何数据库更新操作意味着更新已经可以在数据库中的一个或多个记录。以下是更新所有的记录为“M”SEX的过程。在这里,我们将所有男性年龄增加一年.
#!/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操作是必需的,当你想从数据库中删除一些记录。以下是程序删除雇员的所有记录,其中年龄超过20岁.
例子:
#!/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()
执行事务:
事务是机制,以确保数据的一致性,事务应该有以下四个属性:
原子性: 无论是事务结束或什么也没有发生在所有.
一致性: 必须启动一个事务一致的状态和离开系统是一致的状态.
隔离性: 在当前事务外,事务的中间结果是不可见的.
持久性: 一旦事务提交,效果是持久的,即使系统发生故障后.
对Python DB API 2.0提供两种方法来提交或回滚事务.
--------------------------------------------------------------------
import MySQLdb
con = MySQLdb.connect(host='localhost', user='root', passwd='root', db='hr_resume_center', charset='utf8')
cursor = con.cursor()
sql = "INSERT INTO hr_resume_core (resume_id,name,mobile,email,add_time,sys_time,version) VALUES(%s,%s,%s,%s,%s,%s,%s)"
param = [
(1,'bright','13641154657','jackieming\@sina.cn',1385625423,1385625423,1),
(2,'bright','13641154657','jackieming\@sina.cn',1385625423,1385625423,1),
(3,'bright','13641154657','jackieming\@sina.cn',1385625423,1385625423,1),
]
cursor.execute(sql,param)
cursor.close()
con.close()
分享到:
相关推荐
python连接MySQL数据库
通过python脚本连接mysql数据库, 并执行mysql数据库语句,工具类。
python连接mysql数据库,简单的例子,需要先安装pythone3.0、pymysql模块、mysql数据库才能运行
python连接mysql数据库代码。必须先下载安装mysql数据库,并启动数据库
Python 连接MySQL数据库时自动开启服务器 Python源码Python 连接MySQL数据库时自动开启服务器 Python源码Python 连接MySQL数据库时自动开启服务器 Python源码Python 连接MySQL数据库时自动开启服务器 Python源码...
通过python编写代码连接MySQL数据库,包括数据库和数据表的创建。
python连接mysql数据库python读取mysql数据dataframe写入mysql
这是一篇介绍python连接和使用mysql数据库的文档,介绍了python对mysql数据库的insert、update、delete等基本操作。
Python连接Mysql数据库1
python连接MySQL数据库的操作代码
Python访问MySQL数据库 Python访问MySQL数据库
pymysql python连接mysql数据库
如何用python连接mysql数据库
python可以用这个包来连接MySQL数据库
使用python连接mysql数据库的源代码文件,很好的代码实现资料
Python连接各个版本的mysql数据库,python-mysql-connector MySQL连接python的连接器,可以用于Python2、Python3包括最新的python3,使用起来挺方便的,用python进行数据库编程的朋友,可参考本源码。
python连接MySQL数据库操作
用python连接mysql的时候,需要用的安装版本,源码版本容易有错误提示。下边是打包了32与64版本。 MySQL-python-1.2.3.win32-py2.7.exe MySQL-python-1.2.3.win-amd64-py2.7.exe 实例 1、取得 MYSQL 的版本 # -*- ...
python编程中可以使用MySQLdb进行数据库的连接及诸如查询/插入/更新等操作,但是每次连接mysql数据库请求时,都是独立的去请求访问,相当浪费资源,而且访问数量达到一定数量时,对mysql的性能会产生较大的影响。...
一个程序与数据库进行交互的步骤: 1.和数据库建立连接; 2.执行sql语句,接收返回值; 3.关闭数据库连接。