1、pip install pymysql
2、
# coding:UTF-8
import traceback, pymysql # pip install pymysql
def main(): # 主函数
try:
conn = pymysql.connect(host="localhost", port=3306, charset="UTF8", user="root", password="mysqladmin",
database="yootk") # 数据库连接
print("MySQL数据库连接成功,当前的数据库版本为:%s" % conn.get_server_info())
print("MySQL数据库连接成功,事务提交模式:%s" % conn.get_autocommit())
except Exception:
print(traceback.format_exc())
finally:
conn.close() # 数据库为资源操作,资源操作的最后必须关闭
if __name__ == "__main__": # 判断程序执行名称
main() # 调用主函数
3、
# coding:UTF-8
import traceback, pymysql # pip install pymysql
def main(): # 主函数
try:
conn = pymysql.connect(host="localhost", port=3306, charset="UTF8", user="root", passwd="mysqladmin",
database="yootk") # 数据库连接
print("MySQL数据库连接成功,当前的数据库版本为:%s" % conn.get_server_info())
print("MySQL数据库连接成功,事务提交模式:%s" % conn.get_autocommit())
except Exception:
print(traceback.format_exc())
finally:
conn.close() # 数据库为资源操作,资源操作的最后必须关闭
if __name__ == "__main__": # 判断程序执行名称
main() # 调用主函数
4、
# coding:UTF-8
import traceback, pymysql # pip install pymysql
# 传统的pymysql组件支持的是原生的SQL
SQL = "INSERT INTO user(name,age,birthday,salary,note) VALUES ('小李老师', 18, '2010-09-19', 8000.0, 'www.yootk.com')"
def main(): # 主函数
try:
conn = pymysql.connect(host="localhost", port=3306, charset="UTF8", user="root", passwd="mysqladmin",
database="yootk") # 数据库连接
cmd = conn.cursor() # 获得一个数据库的操作对象
cmd.execute(SQL) # 执行SQL语句
conn.commit() # 提交事务,如果不提交事务,更新不生效
print("更新影响的数据行数:%s" % cmd.rowcount)
print("最后一次增长ID:%s" % cmd.lastrowid)
except Exception:
print(traceback.format_exc())
finally:
conn.close() # 数据库为资源操作,资源操作的最后必须关闭
if __name__ == "__main__": # 判断程序执行名称
main() # 调用主函数
5、
# coding:UTF-8
import traceback, pymysql # pip install pymysql
# 传统的pymysql组件支持的是原生的SQL
SQL = "UPDATE user SET name='周沐言', age=26 WHERE uid=26"
def main(): # 主函数
try:
conn = pymysql.connect(host="localhost", port=3306, charset="UTF8", user="root",
passwd="mysqladmin", database="yootk") # 数据库连接
cmd = conn.cursor() # 获得一个数据库的操作对象
cmd.execute(SQL) # 执行SQL语句
conn.commit() # 提交事务,如果不提交事务,更新不生效
print("更新影响的数据行数:%s" % cmd.rowcount)
except Exception:
print(traceback.format_exc())
finally:
conn.close() # 数据库为资源操作,资源操作的最后必须关闭
if __name__ == "__main__": # 判断程序执行名称
main() # 调用主函数
6、
# coding:UTF-8
import traceback, pymysql # pip install pymysql
# 传统的pymysql组件支持的是原生的SQL
SQL = "DELETE FROM user WHERE uid=26"
def main(): # 主函数
try:
conn = pymysql.connect(host="localhost", port=3306, charset="UTF8", user="root",
passwd="mysqladmin", database="yootk") # 数据库连接
cmd = conn.cursor() # 获得一个数据库的操作对象
cmd.execute(SQL) # 执行SQL语句
conn.commit() # 提交事务,如果不提交事务,更新不生效
print("更新影响的数据行数:%s" % cmd.rowcount)
except Exception:
print(traceback.format_exc())
finally:
conn.close() # 数据库为资源操作,资源操作的最后必须关闭
if __name__ == "__main__": # 判断程序执行名称
main() # 调用主函数
7、
# coding:UTF-8
import traceback, pymysql # pip install pymysql
# 传统的pymysql组件支持的是原生的SQL
SQL = "SELECT uid,name,age,birthday,salary,note FROM user"
def main(): # 主函数
try:
conn = pymysql.connect(host="localhost", port=3306, charset="UTF8", user="root",
passwd="mysqladmin", database="yootk") # 数据库连接
cmd = conn.cursor() # 获得一个数据库的操作对象
cmd.execute(SQL) # 执行SQL查询语句
for user_row in cmd.fetchall(): # 返回查询结果
uid = user_row[0]
name = user_row[1]
age = user_row[2]
birthday = user_row[3]
salary = user_row[4]
note = user_row[5]
print("用户ID:%s、姓名:%s、年龄:%s、birthday:%s、salary:%s、note:%s" % (uid, name, age, birthday, salary, note))
except Exception:
print(traceback.format_exc())
finally:
conn.close() # 数据库为资源操作,资源操作的最后必须关闭
if __name__ == "__main__": # 判断程序执行名称
main() # 调用主函数
8、
# coding:UTF-8
import traceback, pymysql # pip install pymysql
# 传统的pymysql组件支持的是原生的SQL
SQL = "SELECT COUNT(*) FROM user"
def main(): # 主函数
try:
conn = pymysql.connect(host="localhost", port=3306, charset="UTF8", user="root",
passwd="mysqladmin", database="yootk") # 数据库连接
cmd = conn.cursor() # 获得一个数据库的操作对象
cmd.execute(SQL) # 执行SQL查询语句
print("user表的数据行数:%s" % cmd.fetchone())
except Exception:
print(traceback.format_exc())
finally:
conn.close() # 数据库为资源操作,资源操作的最后必须关闭
if __name__ == "__main__": # 判断程序执行名称
main() # 调用主函数
9、
# coding:UTF-8
import traceback, pymysql # pip install pymysql
name = "Mr'Yootk" # 模拟为键盘输入
age = 16 # 模拟为键盘输入
birthday = "2016-06-28" # 模拟为键盘输入
salary = 6000.00 # 模拟为键盘输入
note = "www.yootk.com"
SQL = "INSERT INTO user(name,age,birthday,salary,note) VALUES ('%s', %s, '%s', %s, '%s')" % (name, age, birthday, salary, note)
print(SQL)
def main(): # 主函数
try:
conn = pymysql.connect(host="localhost", port=3306, charset="UTF8", user="root",
passwd="mysqladmin", database="yootk") # 数据库连接
cmd = conn.cursor() # 获得一个数据库的操作对象
cmd.execute(SQL) # 执行SQL语句
conn.commit() # 提交事务,如果不提交事务,更新不生效
print("更新影响的数据行数:%s" % cmd.rowcount)
print("最后一次增长ID:%s" % cmd.lastrowid)
except Exception:
print(traceback.format_exc())
finally:
conn.close() # 数据库为资源操作,资源操作的最后必须关闭
if __name__ == "__main__": # 判断程序执行名称
main() # 调用主函数
10、
# coding:UTF-8
import traceback, pymysql # pip install pymysql
name = "Mr'Yootk" # 模拟为键盘输入
age = 16 # 模拟为键盘输入
birthday = "2016-06-28" # 模拟为键盘输入
salary = 6000.00 # 模拟为键盘输入
note = "www.yootk.com"
SQL = "INSERT INTO user(name,age,birthday,salary,note) VALUES (%s, %s, %s, %s, %s)" # 预处理的模式
def main(): # 主函数
try:
conn = pymysql.connect(host="localhost", port=3306, charset="UTF8", user="root",
passwd="mysqladmin", database="yootk") # 数据库连接
cmd = conn.cursor() # 获得一个数据库的操作对象
cmd.execute(query=SQL, args=[name, age, birthday, salary, note]) # 执行SQL语句
conn.commit() # 提交事务,如果不提交事务,更新不生效
print("更新影响的数据行数:%s" % cmd.rowcount)
print("最后一次增长ID:%s" % cmd.lastrowid)
except Exception:
print(traceback.format_exc())
finally:
conn.close() # 数据库为资源操作,资源操作的最后必须关闭
if __name__ == "__main__": # 判断程序执行名称
main() # 调用主函数
11、
# coding:UTF-8
import traceback, pymysql # pip install pymysql
# 传统的pymysql组件支持的是原生的SQL
SQL = "SELECT uid,name,age,birthday,salary,note FROM user WHERE uid=%s" # 占位符
def main(): # 主函数
try:
conn = pymysql.connect(host="localhost", port=3306, charset="UTF8", user="root",
passwd="mysqladmin", database="yootk") # 数据库连接
cmd = conn.cursor() # 获得一个数据库的操作对象
cmd.execute(query=SQL, args=[1]) # 执行SQL查询语句
for user_row in cmd.fetchall(): # 返回查询结果
uid = user_row[0]
name = user_row[1]
age = user_row[2]
birthday = user_row[3]
salary = user_row[4]
note = user_row[5]
print("用户ID:%s、姓名:%s、年龄:%s、birthday:%s、salary:%s、note:%s" % (uid, name, age, birthday, salary, note))
except Exception:
print(traceback.format_exc())
finally:
conn.close() # 数据库为资源操作,资源操作的最后必须关闭
if __name__ == "__main__": # 判断程序执行名称
main() # 调用主函数
12、
# coding:UTF-8
import traceback, pymysql # pip install pymysql
# 传统的pymysql组件支持的是原生的SQL
SQL = "SELECT uid,name,age,birthday,salary,note FROM user WHERE name LIKE %s LIMIT %s, %s" # 占位符
def main(): # 主函数
keyword= "%沐言%" # 模糊查询关键字
current_page = 1 # 当前所在页
line_size = 2 # 每页显示的数据行
try:
conn = pymysql.connect(host="localhost", port=3306, charset="UTF8", user="root",
passwd="mysqladmin", database="yootk") # 数据库连接
cmd = conn.cursor() # 获得一个数据库的操作对象
cmd.execute(query=SQL, args=[keyword, (current_page - 1) * line_size , line_size]) # 执行SQL查询语句
for user_row in cmd.fetchall(): # 返回查询结果
uid = user_row[0]
name = user_row[1]
age = user_row[2]
birthday = user_row[3]
salary = user_row[4]
note = user_row[5]
print("用户ID:%s、姓名:%s、年龄:%s、birthday:%s、salary:%s、note:%s" % (uid, name, age, birthday, salary, note))
except Exception:
print(traceback.format_exc())
finally:
conn.close() # 数据库为资源操作,资源操作的最后必须关闭
if __name__ == "__main__": # 判断程序执行名称
main() # 调用主函数
13、
# coding:UTF-8
import traceback, pymysql # pip install pymysql
SQL = "INSERT INTO user(name,age,birthday,salary,note) VALUES (%s, %s, %s, %s, %s)" # 预处理的模式
def main(): # 主函数
try:
conn = pymysql.connect(host="localhost", port=3306, charset="UTF8", user="root",
passwd="mysqladmin", database="yootk") # 数据库连接
cmd = conn.cursor() # 获得一个数据库的操作对象
data_list = [] # 创建一个列表
for num in range(1001):
data_list.append(("沐言优拓 - %s" % num, 18, "2008-08-19", 5000.00, "www.yootk.com"))
if num % 20 == 0: # 每保存20条数据就进行更新
cmd.executemany(query=SQL, args=data_list)
data_list.clear()
conn.commit() # 提交事务,如果不提交事务,更新不生效
except Exception:
print(traceback.format_exc())
finally:
conn.close() # 数据库为资源操作,资源操作的最后必须关闭
if __name__ == "__main__": # 判断程序执行名称
main() # 调用主函数
14、
# coding:UTF-8
import traceback, pymysql # pip install pymysql
SQL = "INSERT INTO user(name,age,birthday,salary,note) VALUES (%s, %s, %s, %s, %s)" # 预处理的模式
def main(): # 主函数
try:
conn = pymysql.connect(host="localhost", port=3306, charset="UTF8", user="root",
passwd="mysqladmin", database="yootk") # 数据库连接
conn.autocommit(True) # 自动事务提交
cmd = conn.cursor() # 获得一个数据库的操作对象
cmd.execute(query=SQL, args=("李沐言", 18, "2008-08-19", 5000.00, "www.yootk.com"))
except Exception:
print(traceback.format_exc())
finally:
conn.close() # 数据库为资源操作,资源操作的最后必须关闭
if __name__ == "__main__": # 判断程序执行名称
main() # 调用主函数
15、
# coding:UTF-8
import traceback, pymysql # pip install pymysql
SQL = "INSERT INTO user(name,age,birthday,salary,note) VALUES (%s, %s, %s, %s, %s)" # 预处理的模式
def main(): # 主函数
try:
conn = pymysql.connect(host="localhost", port=3306, charset="UTF8", user="root",
passwd="mysqladmin", database="yootk", autocommit=True) # 数据库连接
cmd = conn.cursor() # 获得一个数据库的操作对象
cmd.execute(query=SQL, args=("王沐言", 18, "2008-08-19", 5000.00, "www.yootk.com"))
except Exception:
print(traceback.format_exc())
finally:
conn.close() # 数据库为资源操作,资源操作的最后必须关闭
if __name__ == "__main__": # 判断程序执行名称
main() # 调用主函数
16、
# coding:UTF-8
import traceback, pymysql # pip install pymysql
SQL_A = "INSERT INTO user(name, ,note) VALUES ('小李', 'www.yootk.com')"
SQL_B = "INSERT INTO user(name, ,note) VALUES ('Mr'Yootk', 'www.yootk.com')"
SQL_C = "INSERT INTO user(name, ,note) VALUES ('小沐', 'www.yootk.com')"
def main(): # 主函数
try:
conn = pymysql.connect(host="localhost", port=3306, charset="UTF8", user="root",
passwd="mysqladmin", database="yootk", autocommit=True) # 数据库连接
cmd = conn.cursor() # 获得一个数据库的操作对象
cmd.execute(query=SQL_A)
cmd.execute(query=SQL_B)
cmd.execute(query=SQL_C)
except Exception:
print(traceback.format_exc())
finally:
conn.close() # 数据库为资源操作,资源操作的最后必须关闭
if __name__ == "__main__": # 判断程序执行名称
main() # 调用主函数
17、
# coding:UTF-8
import traceback, pymysql # pip install pymysql
SQL_A = "INSERT INTO user(name, ,note) VALUES ('小李', 'www.yootk.com')"
SQL_B = "INSERT INTO user(name, ,note) VALUES ('Mr'Yootk', 'www.yootk.com')"
SQL_C = "INSERT INTO user(name, ,note) VALUES ('小沐', 'www.yootk.com')"
def main(): # 主函数
try:
conn = pymysql.connect(host="localhost", port=3306, charset="UTF8", user="root",
passwd="mysqladmin", database="yootk", autocommit=False) # 数据库连接
conn.autocommit(False) # 重复设置
cmd = conn.cursor() # 获得一个数据库的操作对象
cmd.execute(query=SQL_A)
cmd.execute(query=SQL_B)
cmd.execute(query=SQL_C)
conn.commit() # 代码执行正确事务提交
except Exception:
conn.rollback() # 事务回滚
print(traceback.format_exc())
finally:
conn.close() # 数据库为资源操作,资源操作的最后必须关闭
if __name__ == "__main__": # 判断程序执行名称
main() # 调用主函数
18、
# coding:UTF-8
import traceback, pymysql, DBUtils.PooledDB # pip install DBUtils
SQL = "INSERT INTO user(name, note) VALUES ('小李老师', 'www.yootk.com')"
def main(): # 主函数
try:
pool = DBUtils.PooledDB.PooledDB(
creator=pymysql, # 连接池管理的是pymysql的操作类型
mincached=2, # 空闲时维持两个连接
maxcached=5, # 空闲时不超过5个连接
maxconnections=20 , # 最大的连接数(测试)
blocking=True, # 引入阻塞队列
host="localhost", # 主机名称
port=3306, # 端口
user="root", # 用户名
passwd="mysqladmin", # 密码
database="yootk", # 数据库
charset="UTF8" # 字符编码
)
conn = pool.connection() # 获取数据库连接
cmd = conn.cursor() # 获得一个数据库的操作对象
cmd.execute(query=SQL)
conn.commit() # 代码执行正确事务提交
except Exception:
conn.rollback() # 事务回滚
print(traceback.format_exc())
finally:
conn.close() # 数据库为资源操作,资源操作的最后必须关闭
if __name__ == "__main__": # 判断程序执行名称
main() # 调用主函数