Use pycharm connected databases and some simple operations

General development process, we need to connect to the database pycharm, thereby to operate the database, where the main connections is mysql database, additionally, use pandas module reads the database, the basic operation is as follows:

  • Direct connection to the database
import pymysql

conn = pymysql.connect(host='localhost',port=3306,db='joker',user='root',password='root')

# 定义一个标志位,用于控制要执行那种操作
flag = 3

# 创建一个cursor(游标)对象,用于执行SQL语句
cursor = conn.cursor(pymysql.cursors.DictCursor)
'''
pymysql.cursors.DictCursor的作用:让查询结果以字典的形式展示
查询结果:{'id': 8, 'name': 'joker', 'age': 24}
'''
# 增
if flag == 0:
    # sql = 'insert into student(name,age) values("joker",24)'  # 直接将数据填充进去
    sql = 'insert into student(name,age) values(%s,%s)'       # 使用占位符占位,之后传参
    row = cursor.execute(sql,('joker',24))                   # 参数为一个(即新添加一行数据记录)时使用
    # cursor.executemany(sql,[('tom',38),('jack',26)])   # 参数为多个(即新添加多行数据记录)时使用
    print(row)
# 删
if flag == 1:
    sql = 'delete from student where name=%s'
    row = cursor.execute(sql,('joker',))
    print(row)
# 改
if flag == 2:
    # sql = 'update student set age=%s'
    sql = 'update student set age=%s where name=%s'
    row = cursor.execute(sql,(28,'tom'))
    print(row)
# 查
if flag == 3:
    sql = 'select * from student'
    cursor.execute(sql)
    print(cursor.fetchall())  # 查看全部
    # cursor.scroll(-3,'relative')
    '''
    scroll:用于控制查询开始的位置,类似于控制指针or索引
    relative:相对地址,absolute:绝对地址,2表示在各个地址上的偏移量
    '''
    cursor.scroll(2,'absolute')
    print(cursor.fetchmany(144))  # 查看指定个数,个数(参数)可无限大,取值只会取全部值为止
    print(cursor.fetchone())    # 查看一个


conn.commit()
cursor.close()
conn.close()

  • Use pandas to read the database

    import pandas as pd
    import pymysql
    
    # 创建连接对象
    conn = pymysql.connect(host='localhost',port=3306,user='root',password='cyh4414',db='joker')
    
    # 编写SQL语句
    sql = 'select * from student'
    
    # 使用pandas进行查询
    data = pd.read_sql(sql=sql,con=conn)
    print(data)
    

Guess you like

Origin www.cnblogs.com/joker-cai/p/11594964.html