随着需要存储数据的结构不断复杂化,使用数据库来存储数据是一个必须面临的问题。那么应该如何在python中使用数据库?下面就在本篇博客中介绍一下在python中使用mysql。

  首先,本博客已经假定阅读者已经安装了python和mysql,所以不会讲解关于它们的安装(如果未安装,请查阅官方文档进行下载安装)。

在python中使用pymysql操作mysql  

python的标准库中,是没有可以直接连接操作mysql的模块,首先我们应安装python的第三方模块pymysql。

使用pymysql操作mysql的步骤:

  1)使用pymysql.connect连接并登录mysql

  2) 使用connection.cursor建立游标

  3) 使用cursor.execute()或cursor.executemany()执行sql语句

例一(使用pymysql执行简单的mysql操作):

  (1) 首先在mysql中建立一张用户表

  1. CREATE TABLE `users` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `email` varchar(255) COLLATE utf8_bin NOT NULL,
  4. `password` varchar(255) COLLATE utf8_bin NOT NULL,
  5. PRIMARY KEY (`id`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
  7. AUTO_INCREMENT=1 ;

  (2) 使用pymysql连接数据库并操作这张表

  1. import pymysql
  2.  
  3. # Connect to the database
  4. # 连接mysql,host指定主机;port指定端口,如果mysql为默认端口3306可以不写;
  5. # user,password分别指定登录mysql的用户名和密码;
  6. # db指定数据库;charset指定字符集;
  7. connection = pymysql.connect(host='localhost',
  8. user='root',
  9. password='',
  10. db='test',
  11. charset='utf8mb4',
  12. cursorclass=pymysql.cursors.DictCursor)
  13.  
  14. try:
  15. with connection.cursor() as cursor:
  16. # Create a new record
  17. # 构建sql语句
  18. sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
  19. # 相当于在mysql终端执行
  20. # "INSERT INTO `users` (`email`, `password`) VALUES ('webmaster@python.org', 'very-secret')"
  21. cursor.execute(sql, ('webmaster@python.org', 'very-secret'))
  22.  
  23. # connection is not autocommit by default. So you must commit to save
  24. # your changes.
  25. # 向mysql提交更改,如果是查询语句,无需执行connection.commit()
  26. # 可以通过设置connection.autocommit()来自动提交,传入True即可
  27. connection.commit()
  28.  
  29. with connection.cursor() as cursor:
  30. # Read a single record
  31. # sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
  32. # cursor.execute(sql, ('webmaster@python.org',))
  33. sql = "SELECT * FROM `users`"
  34. # 执行cursor.execute(sql),等于在mysql终端执行sql语句。
  35. cursor.execute(sql)
  36. # 获取sql语句执行结果并打印
  37. result = cursor.fetchall()
  38. print(result)
  39. finally:
  40. # 关闭连接
  41. connection.close()

pymysql_example.py

例二(向mysql中的表插入多条信息):

  1. import pymysql
  2.  
  3. connection = pymysql.Connect(host="localhost",
  4. user="root",
  5. password="",
  6. db="test",
  7. charset="utf8mb4",
  8. cursorclass=pymysql.cursors.DictCursor)
  9.  
  10. try:
  11. # # 执行多次INSERT操作
  12. # with connection.cursor() as cursor:
  13. # users_info = [('xiaoming@123.com','simple'), ('xiaoqiang@123.com','simple'),
  14. # ('xiaozhang@123.com','very-secret'), ('xiaoli@123.com', 'simple'),
  15. # ('xiangwang@123.com','simple'), ('xiaohong@123.com','very-secret')]
  16. # sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
  17. # # 执行多次相同操作使用cursor.executemany()
  18. # cursor.executemany(sql, users_info)
  19. # connection.commit()
  20.  
  21. # 查询所有用户信息
  22. with connection.cursor() as cursor:
  23. sql = "SELECT * FROM `users`"
  24. cursor.execute(sql)
  25. result = cursor.fetchall()
  26. print("-----all users-----")
  27. for user_info in result:
  28. print(user_info)
  29.  
  30. with connection.cursor() as cursor:
  31. sql = "SELECT * FROM `users` WHERE `password`=%s"
  32. cursor.execute(sql, ('very-secret',))
  33. result = cursor.fetchall()
  34. print("-----password is very-secret-----")
  35. for user_info in result:
  36. print(user_info)
  37. finally:
  38. connection.close()

test_pymysql.py

  注:在python程序中使用pymysql,最好只执行对表的增删该查即可(使用pymysql虽然能执行原生SQL语句,但不建议使用它进行建数据库,表,修改数据库,表属性等操作(如果要进行这些操作不妨直接登录mysql,直接在mysql终端执行这些操作)。

下面将介绍一些pymysql的一些常用API(在pymysq中只有两个常用object):

(1)Connection Object:

  常用属性:

  1. host mysql主机地址
  2. user 登录用户名
  3. password 登录用户密码
  4. port mysql端口,默认3306
  5. charset 字符集
  6. connect_timeout 连接最大时间,超时自动断开。(default: 10, min: 1, max: 31536000)
  7. autocommit 是否自动提交更改。(default: False)
  8. db 使用指定的数据库
  9. cursorclass 指定cursor

  注:以上参数应在连接数据库时指定,只是常用参数(详细请参见:http://pymysql.readthedocs.io/en/latest/modules/connections.html)。

  常用方法:

  1. begin() - 开启一个事件 mysql终端执行BEGIN效果相同
  2.  
  3. close() - 关闭与mysql的连接
  4.  
  5. commit() - 提交对mysql中存储数据的更改
  6.  
  7. cursor(cursor=None) - 创建一个cursor对象,cursor类在连接时未指明,可以在此指明,使用默认cursor忽略参数即可
  8.  
  9. ping(reconnect=True) - 检测连接是否存活,如果连接超过设置的connet_timeout会自动断开,所以在进行对mysql操作前应使用此方法检测
  10.  
  11. rollback() - 使用了begin()后,对mysql的操作未提交前,可以只用此方法恢复到未操作之前
  12.  
  13. select_db(db) - 选择数据库,如果要操作的表不在连接时指定的数据库,使用此方法切换。
  14.  
  15. show_warnings() - 显示警告信息

(2)Cursor Objects:

  常用方法:

  1. execute(query, args=None) - 执行一条sql语句
  2. Parameters:
  3. query (str) 要被执行的sql语句
  4. args (tuple, list or dict) sql语句中用到的参数
  5. Returns:
  6. 多少行信息收到影响
  7.  
  8. Return type:
  9. int
  10.  
  11. 如果args是以tuple的形式指定,则按位置依次传入sql语句中;如果是以dict传入,则以关键字传入sql语句中。
  12.  
  13. executemany(query, args) - 多次执行这条sql语句
  14. 参数与上相同,不过要使用[]将多个args括起来。
  15. 此方法可提高多行INSERTREPLACE的性能。 否则,它等价于使用execute() 循环args
  16.  
  17. fetchone() - 取结果中的一行
  18.  
  19. fetchall() - 取所有的结果
  20.  
  21. fetchmany(size=None) - 取结果中的size
  22.  
  23. close() - 关闭当前cursor
  24.  
  25. max_stmt_length = 1024000 - 指定 executemany() 执行最多max_stmt_lengthsql语句

  注:只写了常用方法,详细请参见:http://pymysql.readthedocs.io/en/latest/modules/cursors.html

使用sqlalchemy操作数据库(重点)

例三(使用sqlalchemy创建一张数据表并插入数据):

  使用pymysql固然可以与mysql进行交互,但还是在源代码中使用了原生SQL语句,使代码的重用行和扩展性大大降低,这不符合面向对象的编程的特性。那么该如何像操作对象一样操作数据库呢?

  我们使用一种叫做ORM(Object Relational Mapping,简称ORM,或O/RM,或O/R mapping)的技术,是一种程序技术,用于实现面向对象编程语言里不同类型系统的数据之间的转换。在python中我们使用一个名为SQLAlchemy(基于ORM的开发组件)来进行对数据库的操作,这样就不必在源代码中使用SQL语句,大大降低了程序员学习SQL的成本,由于不必再拼接复杂的SQL语句,大大提高开发效率,并且使程序有更高的扩展性。

  1. import sqlalchemy
  2. from sqlalchemy import create_engine
  3. from sqlalchemy.ext.declarative import declarative_base
  4. from sqlalchemy import Column, Integer, String
  5. from sqlalchemy.orm import sessionmaker
  6.  
  7. # 检查sqlalchemy的版本
  8. # print(sqlalchemy.__version__)
  9.  
  10. # 创建一个engine
  11. # 传入一个URL作为第一个位置参数(格式为:dialect[+driver]://user:password@host/dbname[?key=value..])
  12. # dialect is a database name such as mysql, oracle, postgresql, ,
  13. # and driver the name of a DBAPI, such as psycopg2, pyodbc, cx_oracle, pymysql.
  14. # 打印操作数据库的过程,则设置echo=True,否则默认即可
  15. engine = create_engine('mysql+pymysql://root:123456@localhost/test')
  16.  
  17. Base = declarative_base()
  18.  
  19. # 将要创建的表结构
  20. class User(Base):
  21. # 表名
  22. __tablename__ = 'users'
  23.  
  24. # 字段名,字段属性
  25. id = Column(Integer, primary_key=True)
  26. name = Column(String(32))
  27. fullname = Column(String(64))
  28. password = Column(String(64))
  29.  
  30. def __repr__(self):
  31. return "<User(name='%s', fullname='%s', password='%s')>" % (
  32. self.name, self.fullname, self.password)
  33.  
  34. # 可以同时创建多个表,在前面以上面的形式写好所有表结构,最后统一创建
  35. Base.metadata.create_all(engine)
  36.  
  37. # 创建一个Session类
  38. # Session = sessionmaker()
  39. # Session.configure(bind=engine)
  40. # 等同于上面两行
  41. Session = sessionmaker(bind=engine)
  42. # 生成一个session实例
  43. session = Session()
  44.  
  45. # 构造要插入表中的数据
  46. ed_user = User(name='ed', fullname='Ed Jones', password='edspassword')
  47. # 将数据放入session中,如果有多条数据使用session.add_all([data1,data2,...])
  48. session.add(ed_user)
  49. # session.add_all([User(name='wendy', fullname='Wendy Williams', password='foobar'),
  50. # User(name='mary', fullname='Mary Contrary', password='xxg527'),
  51. # User(name='fred', fullname='Fred Flinstone', password='blah')])
  52. # 向数据库提交
  53. # session.commit()
  54.  
  55. data = session.query(User).filter(User.id>2).all()
  56. print(data)

sqlalchemy_test.py

  1. # 使用上面的代码生成的数据表结构
  2. mysql> desc users;
  3. +----------+-------------+------+-----+---------+----------------+
  4. | Field | Type | Null | Key | Default | Extra |
  5. +----------+-------------+------+-----+---------+----------------+
  6. | id | int(11) | NO | PRI | NULL | auto_increment |
  7. | name | varchar(32) | YES | | NULL | |
  8. | fullname | varchar(64) | YES | | NULL | |
  9. | password | varchar(64) | YES | | NULL | |
  10. +----------+-------------+------+-----+---------+----------------+
  11. 4 rows in set (0.00 sec)
  12.  
  13. # 使用上面代码插入表中的数据
  14. mysql> select * from users;
  15. +----+------+----------+-------------+
  16. | id | name | fullname | password |
  17. +----+------+----------+-------------+
  18. | 1 | ed | Ed Jones | edspassword |
  19. +----+------+----------+-------------+
  20. 1 row in set (0.00 sec)

例四(使用sqlalchemy进行对数据的查,改,删)

  1. # 查询时在filter_by(或filter)中写上条件即可,查询到的结果可能是多条,first()代表取第一条,all()代表取所有
  2. our_user = session.query(User).filter_by(name='ed').first()
  3. # 如果有多个查询条件,data = session.query(User).filter(User.id>2).filter(User.id<4).all(),这样使用即可
  4. data = session.query(User).filter(User.id>2).all()
  5. print("-------这是查询数据的结果-------")
  6. print(our_user)
  7. print(data)
  8. print('\n')
  9.  
  10. # 直接修改查询的结果,然后提交即可
  11. our_user.password = 'f8s7ccs'
  12. session.commit()
  13. new_user = session.query(User).filter_by(name='ed').first()
  14. print("-------这是修改数据的结果-------")
  15. print(new_user)
  16. print('\n')
  17.  
  18. # 先查询出要删除的数据,然后使用session.delete()和session.delete()即可
  19. data = session.query(User).filter(User.id==5).first()
  20. # print(data)
  21. session.delete(data)
  22. session.commit()

使用sqlalchemy操作数据库中的数据

例五(使用sqlalchemy实现数据表的外键关联):

  作为关系型数据库,表与表之间的外键关联是比不可少的,也是至关重要的,那么改如何使用sqlalchemy在python对象中通过类的形式映射这种关系呢? 请看下面的代码。

  1. import sqlalchemy
  2. from sqlalchemy import create_engine
  3. from sqlalchemy.ext.declarative import declarative_base
  4. from sqlalchemy import Column, Integer, String, Enum, ForeignKey
  5. from sqlalchemy.orm import sessionmaker, relationship
  6.  
  7. engine = create_engine('mysql+pymysql://root:123456@localhost/student')
  8.  
  9. Base = declarative_base()
  10.  
  11. class Student(Base):
  12. __tablename__ = 'student_info'
  13.  
  14. # 设置id, 类型为int, 不能为空, id是这张表的主键
  15. id = Column(Integer, nullable=False, primary_key=True)
  16. # 设置stu_id, 类型为int, 不能为空, id在这张表中的值唯一
  17. stu_id = Column(Integer, nullable=False, unique=True)
  18. name = Column(String(32), nullable=False, )
  19. age = Column(Integer, nullable=False, )
  20. gender = Column(Enum('F', 'M'), nullable=False)
  21.  
  22. # 查询结果的显示是此函数返回的格式
  23. def __repr__(self):
  24. return "<Student(stu_id='%s', name='%s', age='%s', gender='%s')>" % (
  25. self.stu_id, self.name, self.age, self.gender)
  26.  
  27. class Study(Base):
  28. __tablename__ = 'study_level'
  29.  
  30. id = Column(Integer, nullable=False, primary_key=True)
  31. # 设置stu_id为study_level表的外键,与student_info表中的stu_id关联
  32. stu_id = Column(Integer, ForeignKey('student_info.stu_id'))
  33. mathematics = Column(Integer)
  34. physics = Column(Integer)
  35. chemistry = Column(Integer)
  36.  
  37. # 定义关系,可以在本类中使用属性student_info查询表student_info中的数据(以同样的条件)
  38. # 也可以在Student类中使用属性study_level查询表study_level中的数据
  39. student_info = relationship('Student', backref='study_level')
  40.  
  41. def __repr__(self):
  42. return "<Study(name=%s, mathematics=%s, physics=%s, chemistry=%s)>" % (
  43. self.student_info.name, self.mathematics, self.physics, self.chemistry)
  44.  
  45. # Base.metadata.create_all(engine)
  46.  
  47. Session = sessionmaker(engine)
  48. session = Session()
  49.  
  50. # 插入4个学生信息
  51. # session.add_all([Student(stu_id=10001, name='zhangsan', age=16, gender='F'),
  52. # Student(stu_id=10002, name='lisi', age=17, gender='M'),
  53. # Student(stu_id=10003, name='wangwu', age=16, gender='M'),
  54. # Student(stu_id=10004, name='zhouliu', age=15, gender='F')])
  55. #
  56. # 插入考试成绩,成绩不到60分的科目需补考,再插入补考成绩
  57. # session.add_all([Study(stu_id=10001, mathematics=78, physics=70, chemistry=83),
  58. # Study(stu_id=10002, mathematics=87, physics=85, chemistry=92),
  59. # Study(stu_id=10003, mathematics=60, physics=54, chemistry=76),
  60. # Study(stu_id=10004, mathematics=52, physics=46, chemistry=44),
  61. # Study(stu_id=10003, physics=68),
  62. # Study(stu_id=10004, mathematics=63, physics=61, chemistry=65)])
  63. # session.commit()
  64.  
  65. # 使用这种方法查询多张表,表之间可以没有任何关系
  66. data = session.query(Student, Study).filter(Student.stu_id==Study.stu_id).all()
  67. print(data)
  68. print('\n')
  69.  
  70. # 使用下面的方法通过一张表查询其他表,表之间必须有外键关联
  71. # 因为每个学生的信息唯一,所以使用first()
  72. student = session.query(Student).filter(Student.stu_id==10003).first()
  73. print(student)
  74. # print(student.study_level)相当于Student.stu_id==10003时,下面的两行代码
  75. # data = session.query(Study).filter(session.query(Study).filter(Student.stu_id==Study.stu_id).all()).all()
  76. # print(data)
  77. print(student.study_level)
  78. print('\n')
  79.  
  80. # 因为一个学生可能会有多次考试记录,所以使用all()
  81. score = session.query(Study).filter(Study.stu_id==10003).all()
  82. print(score)
  83. # print(score[0].student_info)相当于Study.stu_id==10003时
  84. # 因为在student_info表中stu_id的值唯一,所以只有一条数据
  85. # data = session.query(Student).filter(Study[0].stu_id==Student.stu_id).first()
  86. # print(data)
  87. print(score[0].student_info)

fk_sqlalchemy.py

  1. mysql> select * from student_info;
  2. +----+--------+----------+-----+--------+
  3. | id | stu_id | name | age | gender |
  4. +----+--------+----------+-----+--------+
  5. | 1 | 10001 | zhangsan | 16 | F |
  6. | 2 | 10002 | lisi | 17 | M |
  7. | 3 | 10003 | wangwu | 16 | M |
  8. | 4 | 10004 | zhouliu | 15 | F |
  9. +----+--------+----------+-----+--------+
  10. 4 rows in set (0.00 sec)
  11.  
  12. mysql> select * from study_level;
  13. +----+--------+-------------+---------+-----------+
  14. | id | stu_id | mathematics | physics | chemistry |
  15. +----+--------+-------------+---------+-----------+
  16. | 1 | 10001 | 78 | 70 | 83 |
  17. | 2 | 10002 | 87 | 85 | 92 |
  18. | 3 | 10003 | 60 | 54 | 76 |
  19. | 4 | 10004 | 52 | 46 | 44 |
  20. | 5 | 10003 | NULL | 68 | NULL | #学号为10003的学生,只有一科成绩小于60,只补考一科
  21. | 6 | 10004 | 63 | 61 | 65 | #学号为10004的学生,三科成绩都小于60,需补考三科
  22. +----+--------+-------------+---------+-----------+
  23. 6 rows in set (0.00 sec)

  注:对有外键关联的数据表,进行数据的增删该查,与上例中使用的方式一样,不过受外键约束,约束条件同mysql中外键的约束相同。(详细请参见:http://www.cnblogs.com/God-Li/p/8157312.html)

例六(使用sqlalchemy实现mysql中多对多的关系):

  多对多的数据关系是最常见的实际生产的数据关系,比如超市的商品与顾客之间的关系(一个顾客可以买多种商品,一种商品可以被多个顾客购买),比如电影与演员的关系(一名演员可以参演多部电影,一部电影会有多个演员),这些数据是我们经常使用的,比如我们在视频网站查找电影时,会有按演员查找,对于一部电影我们也经常关注是哪些演员参演的。那么改如何使用sqlalchemy在mysql中存储这些关系呢?我们就以超市商品与顾客之间的关系来做一个示例,请看下面的代码。

  为了便于理解,我们先来看一下表结构(一共三张表)

  1. # 商品表,存储商品的名称,价格,和生产日期(为了简单只存这几样信息)
  2. mysql> select * from products;
  3. +----+-------------+-------+------------+
  4. | id | name | price | pro_date |
  5. +----+-------------+-------+------------+
  6. | 1 | iPhone8 | 6988 | 2017-09-18 |
  7. | 2 | Apple Watch | 2588 | 2017-06-20 |
  8. | 3 | Airpods | 1288 | 2017-01-11 |
  9. | 4 | MacBook | 10288 | 2017-05-13 |
  10. +----+-------------+-------+------------+
  11. 4 rows in set (0.00 sec)
  12.  
  13. # 顾客表,存储顾客的姓名(这里为了简单只存了姓名,其实还应该用性别、年龄等具体信息)
  14. mysql> select * from customers;
  15. +----+-----------+
  16. | id | name |
  17. +----+-----------+
  18. | 1 | ZhangSang |
  19. | 2 | WangWu |
  20. | 3 | XiaoMing |
  21. | 4 | LiSi |
  22. | 5 | ZhaoLiu |
  23. +----+-----------+
  24. 5 rows in set (0.00 sec)
  25.  
  26. # 商品顾客关系表,存储商品与用户的关系,可通过用户查购买了哪些商品,也可通过商品查有哪些用户购买
  27. mysql> select * from product_to_customer;
  28. +------------+-------------+
  29. | product_id | customer_id |
  30. +------------+-------------+
  31. | 4 | 4 |
  32. | 4 | 3 |
  33. | 3 | 2 |
  34. | 2 | 1 |
  35. | 2 | 4 |
  36. | 2 | 2 |
  37. | 2 | 5 |
  38. | 2 | 3 |
  39. | 1 | 1 |
  40. | 1 | 4 |
  41. | 1 | 5 |
  42. +------------+-------------+
  43. 11 rows in set (0.00 sec)

  接着我们来看一下如何使用python来创建这些表,插入并查询这些信息。

  1. import sqlalchemy
  2. from sqlalchemy import Table, Column, Integer, String, DATE, ForeignKey
  3. from sqlalchemy.orm import relationship
  4. from sqlalchemy.ext.declarative import declarative_base
  5. from sqlalchemy import create_engine
  6.  
  7. Base = declarative_base()
  8.  
  9. # 商品与顾客关系表结构
  10. product_to_customer = Table('product_to_customer', Base.metadata,
  11. Column('product_id', Integer, ForeignKey('products.id')),
  12. Column('customer_id', Integer, ForeignKey('customers.id')))
  13.  
  14. # 用户表结构
  15. class Customer(Base):
  16. __tablename__ = 'customers'
  17.  
  18. id = Column(Integer, primary_key=True)
  19. name = Column(String(32))
  20.  
  21. def __repr__(self):
  22. return self.name
  23.  
  24. # 商品表结构
  25. class Product(Base):
  26. __tablename__ = 'products'
  27.  
  28. id = Column(Integer, primary_key=True)
  29. name = Column(String(32))
  30. price = Column(Integer)
  31. pro_date = Column(DATE)
  32. customers = relationship(Customer, backref='products', secondary='product_to_customer')
  33.  
  34. def __repr__(self):
  35. return self.name
  36.  
  37. engine = create_engine('mysql+pymysql://root:123456@localhost/supermarket')
  38. Base.metadata.create_all(engine)

table_struct.py

  1. import table_struct
  2. from sqlalchemy.orm import sessionmaker
  3.  
  4. Session = sessionmaker(table_struct.engine)
  5. session = Session()
  6.  
  7. # 构建商品信息
  8. # p1 = table_struct.Product(name='iPhone8', price='6988', pro_date='2017-9-18')
  9. # p2 = table_struct.Product(name='MacBook', price='10288', pro_date='2017-5-13')
  10. # p3 = table_struct.Product(name='Airpods', price='1288', pro_date='2017-1-11')
  11. # p4 = table_struct.Product(name='Apple Watch', price='2588', pro_date='2017-6-20')
  12. #
  13. # 构建顾客信息
  14. # c1 = table_struct.Customer(name="ZhangSang")
  15. # c2 = table_struct.Customer(name="LiSi")
  16. # c3 = table_struct.Customer(name="WangWu")
  17. # c4 = table_struct.Customer(name="ZhaoLiu")
  18. # c5 = table_struct.Customer(name="XiaoMing")
  19. #
  20. # 构建商品与顾客的关系
  21. # p1.customers = [c1, c2, c4]
  22. # p2.customers = [c2, c5]
  23. # p3.customers = [c3]
  24. # p4.customers = [c1, c2, c3, c4, c5]
  25. #
  26. # session.add_all([p1, p2, p3, p4, c1, c2, c3, c4, c5])
  27. # session.commit()
  28.  
  29. # 通过顾客查询他购买了哪些商品
  30. customer_obj = session.query(table_struct.Customer).filter(table_struct.Customer.name=='XiaoMing').first()
  31. print(customer_obj.products)
  32.  
  33. # 通过商品查询有哪些顾客购买
  34. product_obj = session.query(table_struct.Product).filter(table_struct.Product.name=="iPhone8").first()
  35. print(product_obj.customers)

database_api.py

数据库MySql在python中的使用的更多相关文章

  1. Neo4j:图数据库GraphDB(四)Python中的操作

    本文总结下Python中如何操作Neo4j数据库,用到py2neo包,Pip install 一下. 1 连接neo4j数据库:跟其它数据库一样,操作前必须输入用户名和密码及地址连接一下. from ...

  2. python数据库-MySQL与python的交互

    一.python3中安装PyMySQL模块 命令安装: sudo apt-get install python-mysql 或者 pip install pymysql 2.使用在pyCharm中安装 ...

  3. MySQL基础语句与其在Python中的使用

    一.MySQL基础语句 $ mysql -u root -p (有密码时) $ mysql -u root     (无密码时) QUIT (or \q)  退出 查看当前所有数据库 show dat ...

  4. python中的MySQL数据库操作 连接 插入 查询 更新 操作

    MySQL数据库 就数据库而言,连接之后就要对其操作.但是,目前那个名字叫做qiwsirtest的数据仅仅是空架子,没有什么可操作的,要操作它,就必须在里面建立“表”,什么是数据库的表呢?下面摘抄自维 ...

  5. 用python批量向数据库(MySQL)中导入数据

    用python批量向数据库(MySQL)中导入数据 现有数十万条数据,如下的经过打乱处理过的数据进行导入 数据库内部的表格的数据格式如下与下面的表格结构相同 Current database: pyt ...

  6. python中mysql数据库的操作-sqlalchemy

    MySQLdb支持python2.*,不支持3.* ,python3里面使用PyMySQL模块代替 python3里面如果有报错  django.core.exceptions.ImproperlyC ...

  7. python中web应用与mysql数据库交互

    7使用数据库 具体使用python的DB-API,这一章里介绍如何编写代码与MYSQL数据库技术交互,这里使用一个通用的数据库API,名为DB-API. 7.1基于数据库的web应用 之前我们把日志数 ...

  8. sqlalchemy python中的mysql数据库神器

    在介绍sqlalchemy之前,我们先了解一下ORM. ORM 全称 Object Relational Mapping, 翻译过来叫对象关系映射.也就是说ORM 将数据库中的表与面向对象语言中的类建 ...

  9. 48.Python中ORM模型实现mysql数据库基本的增删改查操作

    首先需要配置settings.py文件中的DATABASES与数据库的连接信息, DATABASES = { 'default': { 'ENGINE': 'django.db.backends.my ...

随机推荐

  1. 2019 wishes

    1. 永恒目标:爱自己,依靠自己,全家人身心健康. 2. 投稿4篇+,发表2+,不管什么刊物,书稿交给出版社.//改动一下,尽量发高质量杂志和期刊. 3. 带着儿子一起学习怎么和别人主动打招呼,做个有 ...

  2. git提交代码到码云

    日常代码一般提交到github比较多,但我还是钟爱马爸爸,没错就是码云. 码云是中文版的代码托管的网站,不存在打开网速问题,使用也蛮方便的,日常自己保存托管代码已经足够,平时使用git提交代码到码云是 ...

  3. Java中的集合框架-Map

    前两篇<Java中的集合框架-Commection(一)>和<Java中的集合框架-Commection(二)>把集合框架中的Collection开发常用知识点作了一下记录,从 ...

  4. 三、Shiro授权开发

    Shiro 支持三种方式的授权: I. 编程式:通过写if/else 授权代码块完成: Subject subject =SecurityUtils.getSubject(); if(subject. ...

  5. 初试mininet(可选PyCharm)

    目录 0x00 Mininet 0x01 Important classes, methods, functions 0x02 Sample 0x04 run in shell 0x05 Output ...

  6. Ubuntu操作系统(我的是ubuntu 18.04.3 LTS)

    查看是否开启了ssh服务是否安装,使用命令: sudo ps -e |grep ssh 如果安装了的是会有sshd服务的,下面的图片是没有安装ssh服务 2 先更新资源列表,使用命令: sudo ap ...

  7. 使用mock.js进行数据模拟

    mock.js的文档真的是无力吐槽,只说明API怎么使用,完全不说明mock.js这个工具怎么用到项目里面,最有意思的是google的大部分文章复制官网的API,不管是react还是Vue都是下面的流 ...

  8. 由object元素引出的事件注册问题和层级显示问题

    项目有一个双击监控视频全屏的需求,视频播放使用的是IE下的ActiveX控件,web页面中使用HTML嵌入对象元素object.预期方案如下: 1.在开发ActiveX控件时加入双击事件. 2.通过d ...

  9. Android接口与架构(驱动开发)翻译官方文档

    Android接口与架构 Android在设备的规格与驱动方面给了你很大的自由来实现.HAL层提供了一个标准的方式来打通Android系统层与硬件层.Android系统是开源的,所以你能够在接口和性能 ...

  10. Python3 图像识别(一)

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.环境准备: 1.Python3.x(我是用的是Python3.6.5),这个问题不大,只要3.4以上就OK. ...