pymysql 于pycharm中操作mysql

pymysql是外部模块需引入

连接mysql数据库中的某个库

import pymysql #导入pymysql模块

db=pymysql.connect(host='127.0.0.1',user='root',password='123',database='class')
# 获取库 .connect 建立连接 host=数据库ip user=用户名 password=密码 database=数据库

创建表

import pymysql #导入pymysql模块

db=pymysql.connect(host='127.0.0.1',user='root',password='123',database='class')
# 获取库 .connect 建立连接 host=数据库ip user=用户名 password=密码 database=数据库

cur=db.cursor()
# 使用cursor()方法获取 操作游标
# 游标:游标的设计是一种数据缓冲区的思想,用来存放sql语句执行结果。
# 游标是在先从数据表中检索除数据之后才能继续灵活操作的技术。类似于指针,指向数据结构堆栈中的指针,用来pop出所指向的数据,并且只能每次取一个。

cur.execute('drop table if exists employee')
# 使用execute()方法执行sql语句 删除表employee 如果它存在

sql=input('>>>')
# 写入创建表的sql语句

cur.execute(sql)
# 使用execute()方法执行sql语句

db.close()
# 关闭数据库连接

修改(增/删/改)表中数据

import pymysql #导入pymysql模块

db=pymysql.connect(host='127.0.0.1',user='root',password='123',database='class')
# 获取库 .connect 建立连接 host=数据库ip user=用户名 password=密码 database=数据库
cur=db.cursor(cursor=pymysql.cursors.DictCursor)
# 使用cursor()方法获取 操作游标 
# cursor=pymysql.cursors.DictCursor 是将获取信息以字典形式存储
# 游标:游标的设计是一种数据缓冲区的思想,用来存放sql语句执行结果。 # 游标是在先从数据表中检索除数据之后才能继续灵活操作的技术。类似于指针,指向数据结构堆栈中的指针,用来pop出所指向的数据,并且只能每次取一个。 sql=input('>>>') # 写入sql语句 try: cur.execute(sql) # 执行sql语句并存在于游标 db.commit() # 提交到数据库执行 except: db.rollback() # 若发生错误则回滚 db.close() # 关闭数据库连接

查询表中数据

获取所有 fetchall( )    ps:(因占用内存 故不常用) 

import pymysql #导入pymysql模块

db=pymysql.connect(host='127.0.0.1',user='root',password='123',database='class')
# 获取库 .connect 建立连接 host=数据库ip user=用户名 password=密码 database=数据库
cur=db.cursor(cursor=pymysql.cursors.DictCursor)
# 使用cursor()方法获取 操作游标
# 游标:游标的设计是一种数据缓冲区的思想,用来存放sql语句执行结果。
# 游标是在先从数据表中检索除数据之后才能继续灵活操作的技术。类似于指针,指向数据结构堆栈中的指针,用来pop出所指向的数据,并且只能每次取一个。

sql=input('>>>')
# 写入创建表的sql语句

try:
    cur.execute(sql)
    # 执行sql语句并存在于游标
    results=cur.fetchall()
    # 获取所有的记录列表
    for row in results:
        nid=row[0]
        age=row[1]
        sex=row[2]
        print(f'id={nid},age={age},sex={sex}')
except:
    print('Error:unable to fetch data')
db.close()
# 关闭数据库连接

获取下一条信息 fetchone( )

import pymysql #导入pymysql模块

db=pymysql.connect(host='127.0.0.1',user='root',password='123',database='class')
# 获取库 .connect 建立连接 host=数据库ip user=用户名 password=密码 database=数据库
cur=db.cursor(cursor=pymysql.cursors.DictCursor)
# 使用cursor()方法获取 操作游标
# 游标:游标的设计是一种数据缓冲区的思想,用来存放sql语句执行结果。
# 游标是在先从数据表中检索除数据之后才能继续灵活操作的技术。类似于指针,指向数据结构堆栈中的指针,用来pop出所指向的数据,并且只能每次取一个。

sql=input('>>>')
# 写入创建表的sql语句

try:
    cur.execute(sql)
    # 执行sql语句并存在于游标

    results=cur.fetchone()
    # 获取一条记录
    print(results)
except:
    print('Error:unable to fetch data')
db.close()
# 关闭数据库连接

 

获取多条数据 fetchmany( )  ps:(常用于分页)

import pymysql #导入pymysql模块

db=pymysql.connect(host='127.0.0.1',user='root',password='123',database='class')
# 获取库 .connect 建立连接 host=数据库ip user=用户名 password=密码 database=数据库
cur=db.cursor(cursor=pymysql.cursors.DictCursor)
# 使用cursor()方法获取 操作游标
# 游标:游标的设计是一种数据缓冲区的思想,用来存放sql语句执行结果。
# 游标是在先从数据表中检索除数据之后才能继续灵活操作的技术。类似于指针,指向数据结构堆栈中的指针,用来pop出所指向的数据,并且只能每次取一个。

sql=input('>>>')
# 写入创建表的sql语句

# cur.execute(sql)
# # 使用execute()方法执行sql语句

try:
    cur.execute(sql)
    # 执行sql语句并存在于游标

    results=cur.fetchmany(int)
    # 获取int条记录 超过数据总量无影响
    print(results)

except:
    print('Error:unable to fetch data')

db.close()
# 关闭数据库连接

猜你喜欢

转载自www.cnblogs.com/lttlpp61007188/p/10871295.html