python-mysql

This commit is contained in:
yinkanglong
2021-12-21 15:51:51 +08:00
parent 40e916976b
commit 9e73d7200c

227
Python/python-MySQL.ipynb Normal file
View File

@@ -0,0 +1,227 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Database version : 8.0.27-0ubuntu0.20.04.1 \n"
]
}
],
"source": [
"# 创建数据库连接尝试\n",
"#!/usr/bin/python3\n",
" \n",
"import pymysql\n",
" \n",
"# 打开数据库连接\n",
"db = pymysql.connect(host='localhost',\n",
" user='root',\n",
" password='ykl123',\n",
" database='malware')\n",
" \n",
"# 使用 cursor() 方法创建一个游标对象 cursor\n",
"cursor = db.cursor()\n",
" \n",
"# 使用 execute() 方法执行 SQL 查询 \n",
"cursor.execute(\"SELECT VERSION()\")\n",
" \n",
"# 使用 fetchone() 方法获取单条数据.\n",
"data = cursor.fetchone()\n",
" \n",
"print (\"Database version : %s \" % data)\n",
" \n",
"# 关闭数据库连接\n",
"db.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 数据库插入操作\n",
"#!/usr/bin/python3\n",
" \n",
"import pymysql\n",
" \n",
"# 打开数据库连接\n",
"db = pymysql.connect(host='localhost',\n",
" user='testuser',\n",
" password='test123',\n",
" database='TESTDB')\n",
" \n",
"# 使用cursor()方法获取操作游标 \n",
"cursor = db.cursor()\n",
" \n",
"# SQL 插入语句\n",
"sql = \"INSERT INTO EMPLOYEE(FIRST_NAME, \\\n",
" LAST_NAME, AGE, SEX, INCOME) \\\n",
" VALUES ('%s', '%s', %s, '%s', %s)\" % \\\n",
" ('Mac', 'Mohan', 20, 'M', 2000)\n",
"try:\n",
" # 执行sql语句\n",
" cursor.execute(sql)\n",
" # 执行sql语句\n",
" db.commit()\n",
"except:\n",
" # 发生错误时回滚\n",
" db.rollback()\n",
" \n",
"# 关闭数据库连接\n",
"db.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 数据库查询操作\n",
"\n",
"# fetchone(): 该方法获取下一个查询结果集。结果集是一个对象\n",
"# fetchall(): 接收全部的返回结果行.\n",
"# rowcount: 这是一个只读属性并返回执行execute()方法后影响的行数。\n",
"\n",
"#!/usr/bin/python3\n",
" \n",
"import pymysql\n",
" \n",
"# 打开数据库连接\n",
"db = pymysql.connect(host='localhost',\n",
" user='testuser',\n",
" password='test123',\n",
" database='TESTDB')\n",
" \n",
"# 使用cursor()方法获取操作游标 \n",
"cursor = db.cursor()\n",
" \n",
"# SQL 查询语句\n",
"sql = \"SELECT * FROM EMPLOYEE \\\n",
" WHERE INCOME > %s\" % (1000)\n",
"try:\n",
" # 执行SQL语句\n",
" cursor.execute(sql)\n",
" # 获取所有记录列表\n",
" results = cursor.fetchall()\n",
" for row in results:\n",
" fname = row[0]\n",
" lname = row[1]\n",
" age = row[2]\n",
" sex = row[3]\n",
" income = row[4]\n",
" # 打印结果\n",
" print (\"fname=%s,lname=%s,age=%s,sex=%s,income=%s\" % \\\n",
" (fname, lname, age, sex, income ))\n",
"except:\n",
" print (\"Error: unable to fetch data\")\n",
" \n",
"# 关闭数据库连接\n",
"db.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 数据库更新操作\n",
"#!/usr/bin/python3\n",
" \n",
"import pymysql\n",
" \n",
"# 打开数据库连接\n",
"db = pymysql.connect(host='localhost',\n",
" user='testuser',\n",
" password='test123',\n",
" database='TESTDB')\n",
" \n",
"# 使用cursor()方法获取操作游标 \n",
"cursor = db.cursor()\n",
" \n",
"# SQL 更新语句\n",
"sql = \"UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE SEX = '%c'\" % ('M')\n",
"try:\n",
" # 执行SQL语句\n",
" cursor.execute(sql)\n",
" # 提交到数据库执行\n",
" db.commit()\n",
"except:\n",
" # 发生错误时回滚\n",
" db.rollback()\n",
" \n",
"# 关闭数据库连接\n",
"db.close()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 数据库删除操作\n",
"\n",
"#!/usr/bin/python3\n",
" \n",
"import pymysql\n",
" \n",
"# 打开数据库连接\n",
"db = pymysql.connect(host='localhost',\n",
" user='testuser',\n",
" password='test123',\n",
" database='TESTDB')\n",
" \n",
"# 使用cursor()方法获取操作游标 \n",
"cursor = db.cursor()\n",
" \n",
"# SQL 删除语句\n",
"sql = \"DELETE FROM EMPLOYEE WHERE AGE > %s\" % (20)\n",
"try:\n",
" # 执行SQL语句\n",
" cursor.execute(sql)\n",
" # 提交修改\n",
" db.commit()\n",
"except:\n",
" # 发生错误时回滚\n",
" db.rollback()\n",
" \n",
"# 关闭连接\n",
"db.close()"
]
}
],
"metadata": {
"interpreter": {
"hash": "6166d1592bf002ea476aa46ec8b9d5902134a5387368bb80238f621c412f8518"
},
"kernelspec": {
"display_name": "Python 3.8.10 64-bit ('pytorch': virtualenv)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}