• Python中使用MySQLdb连接MySQL 分类: database 2014-01-29 15:37 358人阅读 评论(0) 收藏


    ‘’‘

    #选择数据库     conn.select_db('python');  

    提交操作:conn.commit()

    回滚操作:conn.rollback()

    获取最近查询表的字段个数:conn.field_count

    上次查询或更新所发生行数:cursor.rowcount


    excute(sql , 单挑数据元组) 、executemany( sql , 序列)


    插入一条记录:execute

    1. value = [1,"inserted ?"];    
    2. #插入一条记录    
    3. cursor.execute("insert into test values(%s,%s)",value);  

    插入多条记录:executemany

    1. values=[]    
    2. #生成插入参数值    
    3. for i in range(20):    
    4.     values.append((i,'Hello mysqldb, I am recoder ' + str(i)))    
    5. #插入多条记录     
    6. cursor.executemany("""insert into test values(%s,%s) """,values);   

    获取一条记录:fetchone

    1. #获取一条记录,每条记录做为一个元组返回    
    2. print "只获取一条记录:"   
    3. result = cursor.fetchone();    
    4. print result    
    5. #print 'ID: %s   info: %s' % (result[0],result[1])    
    6. print 'ID: %s   info: %s' % result    

    获取多条记录:fetchmany

    1. #获取5条记录,注意由于之前执行有了fetchone(),所以游标已经指到第二条记录了,也就是从第二条开始的所有记录    
    2. print "只获取5条记录:"   
    3. results = cursor.fetchmany(5)    
    4. for r in results:    
    5.     print r
    获取所有记录:fetchall

    1. print "获取所有结果:"   
    2. #重置游标位置,0,为偏移量,mode=absolute | relative,默认为relative,    
    3. cursor.scroll(0,mode='absolute')    
    4. #获取所有结果    
    5. results = cursor.fetchall()    
    6. for r in results:    
    7.     print r 
    重置游标位置:

    cursor.scroll(0,mode='absolute')

    '''

    主要是安装的MySQLdb,直接sudo apt-get install python-mysqldb,安装完成之后可以在Python解释器中测试一下,输入

    import MySQLdb #注意大小写!! 如果不报错,就证明安装成功了,可能继续了

    先写一个最简单的,创建一个数据库:

    1. #!/usr/bin/env python    
    2. #coding=utf-8    
    3. import MySQLdb    
    4.    
    5. #建立和数据库系统的连接    
    6. conn = MySQLdb.connect(host='localhost', user='root',passwd='longforfreedom')    
    7.    
    8. #获取操作游标     
    9. cursor = conn.cursor()    
    10. #执行SQL,创建一个数据库.    
    11. cursor.execute("""create database python """)    
    12.    
    13. #关闭连接,释放资源    
    14. cursor.close();    
    创建数据库,创建表,插入数据,插入多条数据
    1. #!/usr/bin/env python    
    2. #coding=utf-8    
    3. import MySQLdb    
    4.    
    5. #建立和数据库系统的连接    
    6. conn = MySQLdb.connect(host='localhost', user='root',passwd='longforfreedom')    
    7.    
    8. #获取操作游标     
    9. cursor = conn.cursor()    
    10. #执行SQL,创建一个数据库.    
    11. cursor.execute("""create database if not exists python""")    
    12.    
    13. #选择数据库    
    14. conn.select_db('python');    
    15. #执行SQL,创建一个数据表.    
    16. cursor.execute("""create table test(id int, info varchar(100)) """)    
    17.    
    18. value = [1,"inserted ?"];    
    19. #插入一条记录    
    20. cursor.execute("insert into test values(%s,%s)",value);    
    21.    
    22. values=[]    
    23. #生成插入参数值    
    24. for i in range(20):    
    25.     values.append((i,'Hello mysqldb, I am recoder ' + str(i)))    
    26. #插入多条记录     
    27. cursor.executemany("""insert into test values(%s,%s) """,values);    
    28.    
    29. #关闭连接,释放资源    
    30. cursor.close();    
    查询和插入的流程差不多,只是多了一个得到查询结果的步骤
     
    1. #!/usr/bin/env python    
    2. #coding=utf-8    
    3.    
    4. import MySQLdb    
    5.    
    6. conn = MySQLdb.connect(host='localhost', user='root', passwd='longforfreedom',db='python')    
    7. cursor = conn.cursor()    
    8. count = cursor.execute('select * from test')    
    9. print '总共有 %s 条记录',count    
    10.    
    11. #获取一条记录,每条记录做为一个元组返回    
    12. print "只获取一条记录:"   
    13. result = cursor.fetchone();    
    14. print result    
    15. #print 'ID: %s   info: %s' % (result[0],result[1])    
    16. print 'ID: %s   info: %s' % result     
    17.    
    18. #获取5条记录,注意由于之前执行有了fetchone(),所以游标已经指到第二条记录了,也就是从第二条开始的所有记录    
    19. print "只获取5条记录:"   
    20. results = cursor.fetchmany(5)    
    21. for r in results:    
    22.     print r    
    23.    
    24. print "获取所有结果:"   
    25. #重置游标位置,0,为偏移量,mode=absolute | relative,默认为relative,    
    26. cursor.scroll(0,mode='absolute')    
    27. #获取所有结果    
    28. results = cursor.fetchall()    
    29. for r in results:    
    30.     print r    
    31. conn.close()   
  • 相关阅读:
    IDE有毒
    Netbeans 8.2关于PHP的新特性
    什么是人格
    谁该赋予一款产品灵魂?
    自从升级到macOS后,整个人都不好了
    公司不是大家庭
    性能各个指标分析
    Sqlserver2012 alwayson部署攻略
    初探Backbone
    SQL Server AlwaysOn架构及原理
  • 原文地址:https://www.cnblogs.com/think1988/p/4627950.html
Copyright © 2020-2023  润新知