python学習ノート:MySQLdb


#1.  MySQLdb  
import MySQLdb  

#2.        
conn = MySQLdb.connect(user='root',db='test',passwd='112358')#   root,      test,  112358

#3.python       execute()   sql  
cur = conn.cursor()#      

#3.1        
>>> cur.execute("insert into test_table(name,sex,birth,birthaddr)values('quyi','m','1990-05-05','Xian')")
1L
conn.commit()#    ,     MySQL          

#3.2        
>>> cur.execute("delete from test_table where name='quyi'")
1L
conn.commit()

#3.3       
>>> cur.execute("update test_table set name='qxm' where name='xmQu'")
2L
conn.commit()

#3.4       
>>> cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)#                   
>>> cur.fetchmany(cur.execute('select * from test_table'))#
(('Tree', 'm', datetime.date(1990, 5, 5), 'China'), ('LiuKI', 'f', datetime.date(1995, 4, 1), 'HK'), ('LiuKI', 'f', datetime.date(1995, 4, 1),         'HK'), ('LiuKi', 'f', datetime.date(1995, 4, 1), 'HK'), ('LiuKi', 'f', datetime.date(1995, 4, 1), 'HK'), ('LiuKi', 'f', datetime.date(1995, 4, 1),     'HK'), ('LiuKi', 'f', datetime.date(1995, 4, 1), 'HK'), ('bao', 'm', datetime.date(1990, 5, 5), 'China'), ('qxm', 'm', datetime.date(1959, 11, 18), 'China'), ('bao', 'm', datetime.date(1990, 5, 5), 'China'), ('qxm', 'm', datetime.date(1959, 11, 18), 'China'))
#          
cur.scroll(0,'absolute')#cur.scroll(n,'relative')        n ,   ( ),   ( )#cur.scroll(n,'absolute')        


#4.       
cur.close()
conn.close()