zoukankan      html  css  js  c++  java
  • python的mysql数据库操作

    python操作mysql数据库

    Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口。

    Python 数据库接口支持非常多的数据库,你可以选择适合你项目的数据库:

    • GadFly
    • mSQL
    • MySQL
    • PostgreSQL
    • Microsoft SQL Server 2000
    • Informix
    • Interbase
    • Oracle
    • Sybase

    我目前使用的是mysql数据库,所以这里只是记录了python中mysql的使用

    在写mysql的python代码之前  ,需要确保电脑上面已经安装了mysql

    如何安装MySQLdb?

    为了用DB-API编写MySQL脚本,必须确保已经安装了MySQL。复制以下代码,并执行:

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
    
    import MySQLdb

    如果执行后的输出结果如下所示,意味着你没有安装 MySQLdb 模块:

    Traceback (most recent call last):
      File "test.py", line 3, in <module>
        import MySQLdb
    ImportError: No module named MySQLdb

    安装MySQLdb,请访问 http://sourceforge.net/projects/mysql-python 

    直接看代码来理解mysql语句在python中的使用

     1 #!/usr/bin/python
     2 
     3 # -*- coding: UTF-8 -*-
     4 
     5 import MySQLdb  
     6 
     7 #包的导入
     8 
     9 db = MySQLdb.connect("ip地址,本机为localhost""用户名","密码","表名")
    10 
    11 #打开数据库的连接 
    12 
    13 cursor = db.cursor()
    14 
    15 #使用cursor()方法获得操作游标
    16 
    17 cursor.execute("Mysql语句")
    18 
    19 #使用execute()方法执行SQL语句
    20 
    21 data = cursor.fetchone()
    22 
    23 #使用fetchone()方法获取一条数据
    24 
    25 print data
    26 
    27 #输出获取得到的数据
    28 
    29 db.close()
    30 
    31 #关闭数据库的连接
     1 #!/usr/bin/python
     2 # -*- coding: UTF-8 -*-
     3 
     4 import MySQLdb
     5 
     6 # 打开数据库连接
     7 db = MySQLdb.connect("localhost","testuser","test123","TESTDB" )
     8 
     9 # 使用cursor()方法获取操作游标 
    10 cursor = db.cursor()
    11 
    12 # SQL 插入语句
    13 sql = "INSERT INTO EMPLOYEE(FIRST_NAME, 
    14        LAST_NAME, AGE, SEX, INCOME) 
    15        VALUES ('%s', '%s', '%d', '%c', '%d' )" % 
    16        ('Mac', 'Mohan', 20, 'M', 2000)
    17 try:
    18    # 执行sql语句
    19    cursor.execute(sql)
    20    # 提交到数据库执行
    21    db.commit()
    22 except:
    23    # 发生错误时回滚
    24    db.rollback()
    25 
    26 # 关闭数据库连接
    27 db.close()

    实例:

    以下代码使用变量向SQL语句中传递参数:

    1 user_id = "test123"
    2 password = "password"
    3 
    4 con.execute('insert into Login values("%s", "%s")' % 
    5              (user_id, password))
    获取mysql语句执行之后的返回结果:
    在第一个代码中间,就写了一个获取一行返回结果的方法,下面是获取所有结果的
    1 fetchone(): 该方法获取下一个查询结果集。结果集是一个对象
    2 fetchall():接收全部的返回结果行.
    3 rowcount: 这是一个只读属性,并返回执行execute()方法后影响的行数。



  • 相关阅读:
    leetcode Convert Sorted List to Binary Search Tree
    leetcode Convert Sorted Array to Binary Search Tree
    leetcode Binary Tree Level Order Traversal II
    leetcode Construct Binary Tree from Preorder and Inorder Traversal
    leetcode[105] Construct Binary Tree from Inorder and Postorder Traversal
    证明中序遍历O(n)
    leetcode Maximum Depth of Binary Tree
    限制 button 在 3 秒内不可重复点击
    HTML 和 CSS 画三角形和画多边行基本原理及实践
    在线前端 JS 或 HTML 或 CSS 编写 Demo 处 JSbin 与 jsFiddle 比较
  • 原文地址:https://www.cnblogs.com/52why/p/8193876.html
Copyright © 2011-2022 走看看