API

基础操作:增加、删除、修改、查询
connection对象支持的方法如下:
cursor():使用该连接创建并返回游标
commit():提交当前事务
rollback():回滚当前事务
close():关闭连接
cursor对象支持的方法如下:
execute(op):执行一个数据库的查询命令
fetchone():取得结果集的下一行
fetchmany(size):获取结果集的下几行
fetchall():获取结果集中的所有行
rowcount():返回数据条数或影响行数
close():关闭游标对象
向数据库新增数据:
op = “insert into table_name(column1,column2,…,columnN) values(value1,value2,…,valueN)”
cursor.execute(op)
删除数据库中的数据:
op = “delete from table_name where condition_statement”
cursor.execute(op)
修改数据库中的数据:
op = “update table_name set column1=value1,column2=value2,…,columnN=valueN where condition_statement”
cursor.execute(op)
查询数据库中的数据:
op = “select * from table_name where condition_statement”
cursor.execute(op)

Example

The following examples make use of a simple table

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=utf8mb4 COLLATE=utf8mb4_bin
  7. AUTO_INCREMENT=1 ;

python

  1. import pymysql.cursors
  2. # Connect to the database
  3. connection = pymysql.connect(host='localhost',
  4. user='user',
  5. password='passwd',
  6. database='db',
  7. cursorclass=pymysql.cursors.DictCursor)
  8. with connection:
  9. with connection.cursor() as cursor:
  10. # Create a new record
  11. sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
  12. cursor.execute(sql, ('webmaster@python.org', 'very-secret'))
  13. # connection is not autocommit by default. So you must commit to save
  14. # your changes.
  15. connection.commit()
  16. with connection.cursor() as cursor:
  17. # Read a single record
  18. sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
  19. cursor.execute(sql, ('webmaster@python.org',))
  20. result = cursor.fetchone()
  21. print(result)

例1 insert

  1. from pymysql import *
  2. def main():
  3. # 创建connection连接
  4. conn = connect(host='ip地址', port='3306 端口,int类型', database='数据库名', user='使用者名字',
  5. password='密码', charset='utf8')
  6. # 获取cursor对象
  7. cs1 = conn.cursor()
  8. # 执行sql语句
  9. query = 'insert into 表名(列名1, 列名2, 列名3, 列名4, 列名5, 列名6) values(%s, %s, %s, %s, %s, %s)'
  10. #列名1 = 值1
  11. #列名2 = 值2
  12. #列名3 = 值3
  13. #列名4 = 值4
  14. #列名5 = 值5
  15. #列名6 = 值6
  16. values = (列名1 值, 列名2 值, 列名3 值, 列名4 值, 列名5 值, 列名6 值)
  17. # 执行提交
  18. cs1.execute(query, values)
  19. # 提交之前的操作,如果之前已经执行多次的execute,那么就都进行提交
  20. conn.commit()
  21. # 关闭cursor对象
  22. cs1.close()
  23. # 关闭connection对象
  24. conn.close()
  25. if __name__ == '__main__':
  26. main()

例2 查询

  1. cur = db.cursor()
  2. sql = 'SELECT * FROM sale_data WHERE 位置 IN (%s,%s,%s)'
  3. cur.execute(sql,("江苏“,”浙江“,”上海“))
  4. result = cur.fetchall()
  5. for item in result:
  6. print(item)

sql

  1. select column_name,column_comment,data_type,column_type
  2. from information_schema.columns
  3. where table_name='表名' and table_schema='数据库名称';

例3

  1. sql = 'INSERT INTO sale_data(商品,价格,成交量,卖家,位置) VALUES(%s,%s,%s,%s,%s)'
  2. cur.execute(sql,("连衣裙“,298,10000,”某店“,”北京“))
  3. db.commit()

error

insert

pymysql.err.ProgrammingError: (1064, “You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near

参考
https://pymysql.readthedocs.io/en/latest/index.html
https://www.cnblogs.com/zhuozige/p/13131200.html
https://www.w3school.com.cn/sql/sql_delete.asp