pymysql索引操作

主题:mysql表格已经创建好后,索引的操作

1.查看索引:

      show  index  from 表名;

2.创建索引:

      create  index  索引名  on  表名(字段名(长度));

3.删除索引:

     drop  index  索引名  on 表名;

代码示意如下:

import pymysql

conn = pymysql.connect(
        host='127.0.0.1',
        port=3306,
        user='root',
        password='mysql',
        db='testdb',
        charset='utf8'
)
cursor = conn.cursor()


# 查看索引
# show index from 表名;
sql = 'show index from school_student;'
cursor.execute(sql)
print('索引名:', cursor.fetchall())


# 创建索引
# create index 索引名称 on 表名(字段名称(长度));
creat_index_sql = 'create index index_name on school_student(name);'
cursor.execute(creat_index_sql)
conn.commit()


# 删除索引
# drop index 索引名 on 表名;
drop_index_sql = 'drop index index_name on school_student;'
cursor.execute(drop_index_sql)
conn.commit()

-------------------------------------------END--------------------------------------------- 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
pandas和pymysql是两个Python库,用于数据处理和数据库连接。pandas库提供了丰富的数据结构和数据分析工具,可以方便地进行数据处理和分析。而pymysql库则是用于连接和操作MySQL数据库的工具。 在使用pandas和pymysql时,可以通过设置参数来实现不同的功能。例如,可以通过设置index_col参数将某一列指定为行索引,从而在读取数据库数据时将该列作为行索引。\[1\]另外,还可以使用params参数来配置执行SQL语句时的参数,例如在执行查询语句时可以通过params参数传入查询条件。\[2\] 下面是一个示例代码,演示了如何使用pandas和pymysql连接数据库并执行SQL操作: ```python # 导入pandas和pymysql包 import pandas as pd import pymysql # 返回一个Connection对象 db_conn = pymysql.connect( host='localhost', port=3306, user='root', password='0508', database='example', charset='utf8' ) # 执行SQL操作 sql = "select * from t_file" df = pd.read_sql(sql, con=db_conn) # 打印结果 print(df) ``` 在上述代码中,我们首先通过pymysql库的connect方法连接到MySQL数据库。然后,使用pandas的read_sql方法执行SQL查询语句,并将结果存储在DataFrame对象df中。最后,我们可以通过打印df来查看查询结果。\[3\] #### 引用[.reference_title] - *1* *2* *3* [Pandas 读取MySql 数据(完整版)](https://blog.csdn.net/li123456hkho/article/details/120491217)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值