Python之Mysql及SQLAlchemy操作總結

來源:互聯網
上載者:User

標籤:nmake   一個   varchar   add   integer   條件判斷   engine   ssi   sele   

一、Mysql命令總結 1.建立庫
create database test1;
2.授權一個使用者
grant all privileges on *.* to ‘feng‘@‘%‘ identified by ‘[email protected]‘;
3.建立表
create table Teacher(teaId int not null,teaname varchar(100),age int,sex enum(‘M‘, ‘F‘),phone int);
4.查詢
select * from tabel_name where 條件1 and  條件2
5.增加
insert into table_name (id, name, age, sex, grander) values (1, ‘feng‘, 30, ‘M‘, 99), (2, ‘ajing‘, 45, ‘F‘, 88);
6.修改
update table_name set id=10 where 條件判斷
7.刪除
delete from table_name  where 條件判斷drop table table_name
8.聯集查詢
select a.id, b.name from A a join B b on a.id=b.tid
9.建立索引
create index idx_庫名_表名_列名1_列名2 (列名1, 列名2)
10.查看sql是否走索引
explain select * from student where name=‘ling‘
11.連結資料庫

Python2 使用的是MySQLdb
python3 使用的pymysql  pip安裝

 (1) 建立連結和遊標
注意:在mysql串連中,盡量使用一個串連,確保mysql的並發數

conn = pymysql.connect(host=‘‘, port=, user=‘‘, passwd=‘‘, db=‘‘)cus = conn.curse()

(2)執行sql

sql = "select * from Student;"cus.execute(sql)cus.fetchone()  擷取單個  傳回值  tuplecus.fetchall()  擷取多個  傳回值  list(單個元素是tuple)cus.fetchmany(size=n)  擷取多個

(3) 關閉遊標和串連

cus.close()conn.close()

注意結合try exception finally的使用


二、SQLAlchemy操作總結 1. 建立引擎
engine = create_engine(‘mysql+pymysql://username:[email protected]:port/db‘)
2. 建立session
DBsession = sessionmaker(bind=engine)session = DBsession()
3.建立表
a. 獲得engineb. metadata = MetaData(engine)c. student = Table(‘表名‘, metadata, Colume(‘id‘, Integer, primary_key=True), Colume(‘name‘, String(50))d. metadata.create_all()
4.增加

(1) 先要有一個模型

Base = declarative_base(0class Student(Base):    __tablename__ = ‘student‘    id = Column(Integer, primary_key=True)    name = Column(String(100), primary_key=True)

(2)匯入模型類,執行個體化該類,

sutdent1 = Student(1, ‘ling‘)c. session.add(單一實例)      session.add_all([執行個體1, 執行個體2])
5. 查詢
filter和filter_by的區別filter:可以使用>  < 等,但是列必須是: 表.列,   filter的等號是==session.query(Student).filter(Student.id>100)filter 不支援組合查詢session.query(Student).filter(Studnet.id>100).filter(name==‘ling‘)filter_by: 可以直接寫列,不支援< >  filter_by 等於是==session.query(Student).filter_by(id==10)filter_by 可以支援組合查詢session.query(Student).filter_by(name==‘ling‘ and id==‘342‘)select * from student where name like ‘%ling%‘;模糊查詢含有ling的關鍵字

模糊查詢

session.query(Student).filter(Student.name like(‘%ling%‘))

擷取資料的時候有兩個方法:
one()   tuple
all()   list(單個元素是tuple)
如果在查詢中不寫one(), 或者all()  出來的就是sql語句


6. 更新

(1) 先查出來
(2) 跟新一下類所對應的屬性值就ok
(3) session.commit()

student1 = session.query(Student).filter(Student.id==1001)student1.name = "test"session.commit()
7. 刪除

(1)先查出來
(2)直接調用delete()方法就可以
(3) 提交一下

8.統計, 分組,排序

統計:count()
只需要在查出來以後, 把one或者all替換成count()
統計有多少個

分組:group_by
查出來以後,把one或者all替換成group_by(屬性)

Python之Mysql及SQLAlchemy操作總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.