pymysql增刪改查

#!/usr/bin/env python
# encoding: utf-8  
# Date: 2018/6/24

# 一、增刪改
import pymysql

conn = pymysql.connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='123',
    db='stu_system',
    charset='utf8'
)
# 拿到遊標
cursor = conn.cursor()
sql = 'insert into stu_teacher(tname) values (%s)'

# 插入一條
# rows = cursor.execute(sql, ('wxx'))
# print(rows)

# 插入多條
rows = cursor.executemany(sql, [('wxx',), ('egonxx',)])
print(rows)

# 查看當前自增id數值
print(cursor.lastrowid)

conn.commit()
cursor.close()
conn.close()


# # 二、查詢
# import pymysql
#
# conn = pymysql.connect(
#     host='127.0.0.1',
#     port=3306,
#     user='root',
#     password='123',
#     db='stu_system',
#     charset='utf8'
# )
# # 拿到遊標
# # cursor = conn.cursor()  # 元組形式的遊標
# cursor = conn.cursor(pymysql.cursors.DictCursor)  # 字典形式的遊標
# # 查詢
# rows = cursor.execute('select * from stu_teacher;')
#
# # 取一行
# r1 = cursor.fetchone()  # 元組(1, '李必勝')
# print(r1)
#
# # 取多行
# r2 = cursor.fetchmany(2)  # 2行
# print(r2)
#
# # 取全部
# rall = cursor.fetchall()
# print(rall)
#
# # cursor 設置光標位置
# # cursor.scroll(3, mode='relative')  # 相對位置移動
# # cursor.scroll(3, mode='absolute')  # 絕對位置移動
#
# cursor.close()
# conn.close()


python

相關文章
相關標籤/搜索