• 190707Python-MySQL


    一、Python连接MySQL

    import pymysql
    
    conn = pymysql.connect(host='192.168.100.4', port=3306, user='dongfei', passwd='dongfei', db='dongfei')
    
    cursor = conn.cursor()
    
    effect_row = cursor.execute("SELECT * FROM students")  #执行sql语句,返回修改了几行
    
    print(cursor.fetchone())  #打印
    print(cursor.fetchone())
    print('- - - -')
    print(cursor.fetchall())
    

    二、插入数据

    import pymysql
    
    conn = pymysql.connect(host='192.168.100.4', port=3306, user='dongfei', passwd='dongfei', db='dongfei')
    
    cursor = conn.cursor()
    
    data = [
        (2,'jack',18),
        (3,'yangguo',25),
        (4,'xiaolongnv',30),
    ]
    
    effect_row = cursor.executemany("INSERT students(id,name,age) values(%s,%s,%s)", data)
    
    conn.commit()
    

    三、orm(sqlalchemy)基本使用

    import sqlalchemy
    from sqlalchemy import create_engine
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import Column, Integer, String
    
    engine = create_engine("mysql+pymysql://dongfei:dongfei@192.168.100.4/dongfei",
                           encoding='utf-8', echo=True)
    
    Base = declarative_base()
    
    class User(Base):
        __tablename__ = 'user'
        id = Column(Integer, primary_key=True)
        name = Column(String(32))
        password = Column(String(64))
    
    Base.metadata.create_all(engine)
    
    • 插入数据
    import sqlalchemy
    from sqlalchemy import create_engine
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import Column, Integer, String
    
    engine = create_engine("mysql+pymysql://dongfei:dongfei@192.168.100.4/dongfei",
                           encoding='utf-8', echo=True)
    
    Base = declarative_base()
    
    class User(Base):
        __tablename__ = 'user'
        id = Column(Integer, primary_key=True)
        name = Column(String(32))
        password = Column(String(64))
    
    Base.metadata.create_all(engine)
    
    from sqlalchemy.orm import sessionmaker
    
    Session_class = sessionmaker(bind=engine)  #创建与数据库会话的类
    Session = Session_class()  #生成session实例
    
    user_obj = User(name='dongfei', password='123456')
    print(user_obj.name, user_obj.id)
    
    Session.add(user_obj)
    
    Session.commit()
    

    四、简单查询修改

    import sqlalchemy
    from sqlalchemy import create_engine
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import Column, Integer, String
    
    engine = create_engine("mysql+pymysql://dongfei:dongfei@192.168.100.4/dongfei",
                           encoding='utf-8')
    
    Base = declarative_base()
    
    class User(Base):
        __tablename__ = 'user'
        id = Column(Integer, primary_key=True)
        name = Column(String(32))
        password = Column(String(64))
    
        def __repr__(self):
            return "<%s name:%s>" %(self.id, self.name)
    
    Base.metadata.create_all(engine)
    
    from sqlalchemy.orm import sessionmaker
    
    Session_class = sessionmaker(bind=engine)  #创建与数据库会话的类
    Session = Session_class()  #生成session实例
    
    #data = Session.query(User).filter_by().all()
    #data = Session.query(User).filter(User.id==1).all()
    data = Session.query(User).filter(User.id==1).filter(User.name=='dongfei').first()
    
    print(data)
    data.name = "dongfei2"
    data.password = "abc123"
    Session.commit()
    
  • 相关阅读:
    算法导论读书笔记(未完成)
    工作心理学(未完成)
    面试疑难点解析
    aop难点解析。
    Mybatis框架解析之Builder解析
    HashMap原理总结
    编程基础的重要性(程序员之路)
    Java HashMap详解
    Java源码分析系列之HttpServletRequest源码分析
    JFinal源码 分析之 Core包分析
  • 原文地址:https://www.cnblogs.com/L-dongf/p/11145707.html
Copyright © 2020-2023  润新知