《Python程序设计》习题与答案-python教材答案

发布时间 : 星期五 文章《Python程序设计》习题与答案-python教材答案更新完毕开始阅读

DSN = 'PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=C:/MyDB.mdb;'

sql_statement = \ conn.Open(DSN)

conn.Execute(sql_statement) conn.Close()

(5)遍历记录

rs.MoveFirst() count = 0 while 1:

if rs.EOF: break else:

count = count + 1 rs.MoveNext()

14.4 叙述使用Python操作MS SQL Server数据库的步骤。 答:

可以使用pywin32和pymssql两种不同的方式来访问MS SQL Server数据库。 先来了解一下pywin32模块访问MS SQL Server数据库的步骤。 (1)添加引用:

import adodbapi

adodbapi.adodbapi.verbose = False # adds details to the sample printout import adodbapi.ado_consts as adc

(2)创建连接:

Cfg = {'server':'192.168.29.86\\\\eclexpress','password':'xxxx','db':'pscitemp'}

constr = r\Initial Catalog=%s; Data Source=%s; user ID=%s; Password=%s; \conn = adodbapi.connect(constr)

(3)执行sql语句:

cur = conn.cursor()

sql = '''select * from softextBook where title='{0}' and remark3!='{1}''''.format(bookName,flag) cur.execute(sql) data = cur.fetchall() cur.close()

(4)执行存储过程:

#假设proName有三个参数,最后一个参数传了null

ret = cur.callproc('procName',(parm1,parm2,None)) conn.commit()

(5)关闭连接 conn.close()

接下来再通过一个示例来简单了解一下使用pymssql模块访问MS SQL Server数据库的方法。

import pymssql

conn = pymssql.connect(host='SQL01', user='user', password='password', database='mydatabase') cur = conn.cursor()

cur.execute('CREATE TABLE persons(id INT, name VARCHAR(100))')

cur.executemany(\ALUES(%d, xinos.king)\Doe') ])

conn.commit()

cur.execute('SELECT * FROM persons WHERE salesrep=xinos.king', 'John Doe') row = cur.fetchone() while row:

print \ row = cur.fetchone()

cur.execute(\ conn.close()

14.5 叙述MySQLDb模块提供的数据库访问方法。 答:

Python访问MySQL数据库可以使用MySQLDb模块,该模块主要方法有: ? commit() :提交事务。 ? rollback() :回滚事务。

? callproc(self, procname, args):用来执行存储过程,接收的参数为存储过程名和参数列表,返回值为受影响的行数。

? execute(self, query, args):执行单条sql语句,接收的参数为sql语句本身和使用的参数列表,返回值为受影响的行数。

? executemany(self, query, args):执行单条sql语句,但是重复执行参数列表里的参数,返回值为受影响的行数。

? nextset(self):移动到下一个结果集。 ? fetchall(self):接收全部的返回结果行。

? fetchmany(self, size=None):接收size条返回结果行,如果size的值大于返回的结果行的数量,则会返回cursor.arraysize条数据。

? fetchone(self):返回一条结果行。

? scroll(self, value, mode='relative'):移动指针到某一行,如果mode='relative',则表示从当前所在行移动value条;如果 mode='absolute',则表示从结果集的第一行移动value条。

第15章 多媒体编程

15.1 编程程序,在窗口上绘制一个三角形,设置三个顶点为不同的颜色,并对内部进行光滑着色。

答:

from OpenGL.GL import * from OpenGL.GLUT import * from OpenGL.GLU import * import sys

class MyPyOpenGLTest:

def __init__(self, width = 640, height = 480, title = 'MyPyOpenGLTest'): glutInit(sys.argv)

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH) glutInitWindowSize(width, height) self.window = glutCreateWindow(title) glutDisplayFunc(self.Draw) glutIdleFunc(self.Draw) self.InitGL(width, height)

#default drawing function def Draw(self):

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity()

glTranslatef(-2.0, 0.0, -8.0)

#draw 2D graphic, leaving z to be 0 glBegin(GL_POLYGON) glColor3f(1.0, 0.0, 0.0) glVertex3f(0.0, 1.0, 0.0) glColor3f(0.0, 1.0, 0.0) glVertex3f(1.0, -1.0, 0.0) glColor3f(0.0, 0.0, 1.0) glVertex3f(-1.0, -1.0, 0.0) glEnd()

glTranslatef(2.5, 0.0, 0.0)

glutSwapBuffers()

def InitGL(self, width, height): glClearColor(0.0, 0.0, 0.0, 0.0) glClearDepth(1.0)

glDepthFunc(GL_LESS)

联系合同范文客服:xxxxx#qq.com(#替换为@)