190707Python-MySQL

Posted l-dongf

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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:[email protected]/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:[email protected]/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:[email protected]/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的主要内容,如果未能解决你的问题,请参考以下文章

190707Python-Redis