标签:数据库 return ipython inner size 显示 variable nes prompt
import pymysql import pandas as pd
conn=pymysql.Connect("192.168.1.8","root","Password","mysqljingjie",charset="utf8")
conn #建立连接,连接之后打印conn‘查看是否连接成功
GRANT ALL PRIVILEGES ON . TO ‘账户名‘@‘192.168.10.83‘ IDENTIFIED BY ‘password‘ WITH GRANT OPTION;
如果要想所有的外部ip地址都能够访问使用mysql,可以执行下面: GRANT ALL PRIVILEGES ON . TO ‘账户名‘@‘%‘ IDENTIFIED BY ‘password‘ WITH GRANT OPTION;
cur=conn.cursor()#创建游标
cur.execute("select*from employee")#执行sql语句
#cur.fetchone()#返回一条结果 #cur.fetchmany()#返回指定条数 ser=cur.fetchall()#返回所有元组
columnDes = cur.description#获取描述信息
columnNames = [columnDes[i][0] for i in range(len(columnDes))]#获取列名称 df = pd.DataFrame([list(i) for i in ser],columns=columnNames) df
conn.close()#关闭连接,否者浪费资源
#sql="""insert into bank(b_id,bank_name) ,values("B0005","成都银行")"""
conn=pymysql.Connect("192.168.1.8","root","password","mysqljingjie",charset="utf8") cur=conn.cursor() cur.execute(sql) conn.commit()#提交事务 # 发生错误时回滚 conn.rollback() conn.close()
In [127]:
ser
def get_df_from_db(sql): cursor = connection.cursor() cursor.execute(sql) data = cursor.fetchall() columnDes = cursor.description #获取连接对象的描述信息 columnNames = [columnDes[i][0] for i in range(len(columnDes))] df = pd.DataFrame([list(i) for i in data],columns=columnNames) return df
In [113]:
import pandas as pd
description()
len(columnDes)
columnDes
标签:数据库 return ipython inner size 显示 variable nes prompt
原文地址:https://www.cnblogs.com/Koi504330/p/11908801.html