import MySQLdbtry:
conn =MySQLdb.connect('127.0.0.1','root','123456',charset = 'gb2312')
conn.select_db('python')
cur=conn.cursor()
sql1 = 'drop database python'
sql2 = 'create database if not exists python'
sql3 = 'create database python'
sql4 = 'create table module(m_id int not null,m_name VARCHAR(25),m_size int)'
sql5 = 'create table if not exists demo(d_id int not null,d_name varchar(25),m_size int default 0)'
values=[] for i in range(1):
values.append((i,'mysql',i+1))
sql6 = 'insert into module values(%s,%s,%s)'
values = [1,'MySQLdb',5]
sql6 = "insert into module VALUES('%d','%s','%d')"%(2,'MySQLdb',7)
sql7 = "update module set m_name='MySql' where m_id=0 and m_size=0"
sql8 = "delete from module where m_id=1 and m_size=0"
sql9 = "select * from module where m_id=1"
cur.execute(sql9)
count = cur.execute(sql9)
print u'查询结果数量:',count
result = cur.fetchone()
print u'单条查询结果:',result
result = cur.fetchmany(2) print u'多条查询结果:',result
result = cur.fetchall() print u'所有不同的查询结果:',result for data in result: print data
conn.commit()
cur.close()
conn.close()except MySQLdb.Error,e: print "Mysql Error %d: %s" % (e.args[0], e.args[1])