一、pymysql模块安装

Pymysql模块使用操作-LMLPHP

二、测试数据库连接

'''
测试数据库连接.py
'''

from pymysql import Connection

con = None
try:
    # 创建数据库连接
    con = Connection(
        host="localhost",
        port=3306,
        user="root",
        password="XXXXX"
    )
    # 测试链接
    print(con.get_host_info())
    print(con.get_server_info())
except Exception as e:
    print("异常", e)
finally:
    if con:
        # 关闭连接
        con.close();

运行之后返回信息,则成功 

Pymysql模块使用操作-LMLPHP

三、 创建t_user表

'''
pymysql执行DDL.py
'''

from pymysql import Connection

con = None
try:
    # 创建数据库连接
    con = Connection(
        host="localhost",
        port=3306,
        user="root",
        password="XXXX",
        database="db_pymysql"
    )
    # 创建游标对象
    cursor = con.cursor()

    # 创建sql
    sql = """
        CREATE TABLE t_user (
        id INT(11) NOT NULL AUTO_INCREMENT,
        name VARCHAR(10) NOT NULL,
        age INT(11) NOT NULL,
        PRIMARY KEY (id)
        ) ENGINE=InnoDB default charset=utf8
    """
    cursor.execute(sql)
except Exception as e:
    print("异常", e)
finally:
    if con:
        # 关闭连接
        con.close();

执行结果

Pymysql模块使用操作-LMLPHP

四、 执行insert,新增数据

'''
pymasq执行insert操作.py
'''

from pymysql import Connection

con = None
try:
    # 创建数据库连接
    con = Connection(
        host="localhost",
        port=3306,
        user="root",
        password="XXXX",
        database="db_pymysql",
        autocommit=True
    )
    # 创建游标对象
    cursor = con.cursor()
    sql = """
          insert into t_user value(null,'kaka',43)
    """
    cursor.execute(sql)

except Exception as e:
    print("异常", e)
finally:
    if con:
        # 关闭连接
        con.close();

执行结果

Pymysql模块使用操作-LMLPHP

五、执行update,更新数据

'''
pymasq执行update操作.py
'''

from pymysql import Connection

con = None
try:
    # 创建数据库连接
    con = Connection(
        host="localhost",
        port=3306,
        user="root",
        password="XXXX",
        database="db_pymysql",
        autocommit=True
    )
    # 创建游标对象
    cursor = con.cursor()
    sql = "update t_user set age=99 where id=1"
    cursor.execute(sql)

except Exception as e:
    print("异常", e)
finally:
    if con:
        # 关闭连接
        con.close();

 执行结果

Pymysql模块使用操作-LMLPHP

六、 执行update,删除数据

'''
pymasq执行delete操作.py
'''

from pymysql import Connection

con = None
try:
    # 创建数据库连接
    con = Connection(
        host="localhost",
        port=3306,
        user="root",
        password="XXXX",
        database="db_pymysql",
        autocommit=True
    )
    # 创建游标对象
    cursor = con.cursor()
    sql = "delete from  t_user  where id=1"
    cursor.execute(sql)
except Exception as e:
    print("异常", e)
finally:
    if con:
        # 关闭连接
        con.close();

执行结果

Pymysql模块使用操作-LMLPHP

七、  执行select,查询数据

 先增加3条数据messi,wangs,zada

'''
pymasq执行select查询.py
'''

from pymysql import Connection

con = None
try:
    # 创建数据库连接
    con = Connection(
        host="localhost",
        port=3306,
        user="root",
        password="XXXX",
        database="db_pymysql"
    )
    # 创建游标对象
    cursor = con.cursor()
    sql = "select * from t_user"
    cursor.execute(sql)
    result = cursor.fetchall()
    # 打印查询结果
    for i in result:
        print(i)
except Exception as e:
    print("异常", e)
finally:
    if con:
        # 关闭连接
        con.close();

执行结果

Pymysql模块使用操作-LMLPHP

11-02 16:02