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
CREATE TABLE `users` (`id` int(11) NOT NULL AUTO_INCREMENT,`email` varchar(255) COLLATE utf8_bin NOT NULL,`password` varchar(255) COLLATE utf8_bin NOT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_binAUTO_INCREMENT=1 ;
python
import pymysql.cursors# Connect to the databaseconnection = pymysql.connect(host='localhost',user='user',password='passwd',database='db',cursorclass=pymysql.cursors.DictCursor)with connection:with connection.cursor() as cursor:# Create a new recordsql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"cursor.execute(sql, ('webmaster@python.org', 'very-secret'))# connection is not autocommit by default. So you must commit to save# your changes.connection.commit()with connection.cursor() as cursor:# Read a single recordsql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"cursor.execute(sql, ('webmaster@python.org',))result = cursor.fetchone()print(result)
例1 insert
from pymysql import *def main():# 创建connection连接conn = connect(host='ip地址', port='3306 端口,int类型', database='数据库名', user='使用者名字',password='密码', charset='utf8')# 获取cursor对象cs1 = conn.cursor()# 执行sql语句query = 'insert into 表名(列名1, 列名2, 列名3, 列名4, 列名5, 列名6) values(%s, %s, %s, %s, %s, %s)'#列名1 = 值1#列名2 = 值2#列名3 = 值3#列名4 = 值4#列名5 = 值5#列名6 = 值6values = (列名1 值, 列名2 值, 列名3 值, 列名4 值, 列名5 值, 列名6 值)# 执行提交cs1.execute(query, values)# 提交之前的操作,如果之前已经执行多次的execute,那么就都进行提交conn.commit()# 关闭cursor对象cs1.close()# 关闭connection对象conn.close()if __name__ == '__main__':main()
例2 查询
cur = db.cursor()sql = 'SELECT * FROM sale_data WHERE 位置 IN (%s,%s,%s)'cur.execute(sql,("江苏“,”浙江“,”上海“))result = cur.fetchall()for item in result:print(item)
sql
select column_name,column_comment,data_type,column_typefrom information_schema.columnswhere table_name='表名' and table_schema='数据库名称';
例3
sql = 'INSERT INTO sale_data(商品,价格,成交量,卖家,位置) VALUES(%s,%s,%s,%s,%s)'cur.execute(sql,("连衣裙“,298,10000,”某店“,”北京“))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
