码迷,mamicode.com
首页 > 数据库 > 详细

190707Python-MySQL

时间:2019-07-07 12:47:27      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:str   lte   查询   values   first   生成   enc   修改   long   

一、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()

190707Python-MySQL

标签:str   lte   查询   values   first   生成   enc   修改   long   

原文地址:https://www.cnblogs.com/L-dongf/p/11145707.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!