zoukankan      html  css  js  c++  java
  • python连接MySQL

        再python3中连接mysql的模块位pymysql,我们可以通过pip来安装:

    1 pip install pymysql

    这样我们就可以使用该模块连接mysql了。

    连接mysql用到pymysql的Connect()模块,

     1 Connect(*args, **kwargs)  #Connect方法
     2     Establish a connection to the MySQL database. Accepts several
     3     arguments:
     4 
     5     host: Host where the database server is located
     6     user: Username to log in as
     7     password: Password to use.
     8     database: Database to use, None to not use a particular one.
     9     port: MySQL port to use, default is usually OK. (default: 3306)
    10     bind_address: When the client has multiple network interfaces, specify the interface from which to connect to the host. Argument can be a hostname or an IP address.
    11     unix_socket: Optionally, you can use a unix socket rather than TCP/IP.
    12     charset: Charset you want to use.

    连接数据库后需要设置游标(cursor)进行数据库的执行操作conn.cursor():

    1 >>> help(conn.cursor) #创建游标
    2 Help on method cursor in module pymysql.connections:
    3 
    4 cursor(cursor=None) method of pymysql.connections.Connection instance
    5     Create a new cursor to execute queries with

    设置了cursor后,就可以写sql语句,使用cursor的实例去执行了,执行用到execute()方法:

     1 >>> help(cursor.execute)   #execute方法
     2 Help on method execute in module pymysql.cursors:
     3 
     4 execute(query, args=None) method of pymysql.cursors.Cursor instance
     5     Execute a query
     6 
     7     :param str query: Query to execute.
     8 
     9     :param args: parameters used with query. (optional)
    10     :type args: tuple, list or dict
    11 
    12     :return: Number of affected rows
    13     :rtype: int
    14 
    15     If args is a list or tuple, %s can be used as a placeholder in the query.
    16     If args is a dict, %(name)s can be used as a placeholder in the query.

    执行sql语句后,查询返回的结果,可以使用cursor的fetchall()函数查看,这会返回一个元组,每个元组的元素还是一个元组cursor.fetchall():

    1 >>> help(cursor.fetchall)
    2 Help on method fetchall in module pymysql.cursors:
    3 
    4 fetchall() method of pymysql.cursors.Cursor instance
    5     Fetch all the rows

    可以使用for循环逐个查看结果。

    下面是一个完整的例子:

     1 import pymysql
     2 
     3 conn=pymysql.Connect(host='192.168.37.130',port=3306,user='root',password='123456',database='user')
     4 cursor=conn.cursor(pymysql.cursors.DictCursor)
     5 sql='select * from users'
     6 res_num=cursor.execute(sql)
     7 print(res_num)
     8 res_content=cursor.fetchall()
     9 for i in res_content:
    10     print(i)
    View Code 

    上面模拟了一个简单的数据库查询操作。下面我们在写一个简单的例子:

     1 import pymysql
     2 
     3 conn=pymysql.Connect(host='192.168.37.130',port=3306,user='root',password='123456',database='user')
     4 cursor=conn.cursor(pymysql.cursors.DictCursor)
     5 name=input('Input name:').strip()
     6 sql='select * from users where name like %s'
     7 res_num=cursor.execute(sql,name )
     8 if res_num>0:
     9     res_content=cursor.fetchall()
    10     for i in res_content:
    11         print(i)
    12 else:
    13     print('Not in database')
    View Code
  • 相关阅读:
    【Python之路】第三篇--Python基本数据类型
    【Python之路】第二篇--初识Python
    【Python之路】第一篇--Linux基础命令
    noip模拟测试7
    noip模拟测试6
    动态添加select的option [转载]
    javaweb报错:java.lang.NumberFormatException: null
    Javascript获取select的选中值和选中文本(转载)
    动态生成select框内容
    IO(Input&Output)流の介绍
  • 原文地址:https://www.cnblogs.com/mingxiazhichan/p/7148192.html
Copyright © 2011-2022 走看看