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()
posted @ 2019-07-07 11:12  生生不息.连绵不绝  阅读(168)  评论(0编辑  收藏  举报