调试环境python3.6,调试python操作mysql数据库

python3.6下代码如下

#coding:utf-8
#python3.6使用pymysql操作mysql
print("=====================mysql数据库=====================")


import pymysql.cursors

# 连接数据库
connect = pymysql.Connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    passwd='19910101a',
    db='note',
    charset='utf8'
)

# 获取游标
cursor = connect.cursor()

#删除表
sql = 'DROP TABLE IF EXISTS student'
cursor.execute(sql)
connect.commit()
print('如果存在表就删除表格')

#创建表格
sql = "CREATE TABLE student(id INTEGER PRIMARY KEY,name TEXT)"
try:
    cursor.execute(sql)
    connect.commit()
except:
    print("表已存在")
print('成功创建表格')

# 插入数据
sql = "INSERT INTO student VALUES(%d,'%s')"
data = (1, 'student1')
cursor.execute(sql % data)
connect.commit()
print('成功插入', cursor.rowcount, '条数据')

# 修改数据
sql = "UPDATE student SET name = '%s' WHERE id = %d "
data = ('student2', 1)
cursor.execute(sql % data)
connect.commit()
print('成功修改', cursor.rowcount, '条数据')

# 查询数据
sql = "SELECT * FROM student WHERE id=%d"
data = (1,)
cursor.execute(sql % data)
for row in cursor.fetchall():
    print("%s" % str(row))
print('共查找出', cursor.rowcount, '条数据')

# 删除数据
sql = "DELETE FROM student WHERE id = %d LIMIT %d"
data = (1, 1)
cursor.execute(sql % data)
connect.commit()
print('成功删除', cursor.rowcount, '条数据')


# 事务处理
sql_1 = "UPDATE student SET name = name + '1' WHERE id = 1 "


try:
    cursor.execute(sql_1)
except Exception as e:
    connect.rollback()  # 事务回滚
    print('事务处理失败', e)
else:
    connect.commit()  # 事务提交
    print('事务处理成功', cursor.rowcount)

# 关闭连接
cursor.close()
connect.close()

#pymysql.Connect()参数说明
#host(str):      MySQL服务器地址
#port(int):      MySQL服务器端口号
#user(str):      用户名
#passwd(str):    密码
#db(str):        数据库名称
#charset(str):   连接编码
#
#connection对象支持的方法
#cursor()        使用该连接创建并返回游标
#commit()        提交当前事务
#rollback()      回滚当前事务
#close()         关闭连接
#
#cursor对象支持的方法
#execute(op)     执行一个数据库的查询命令
#fetchone()      取得结果集的下一行
#fetchmany(size) 获取结果集的下几行
#fetchall()      获取结果集中的所有行
#rowcount()      返回数据条数或影响行数
#close()         关闭游标对象


python2.7下代码如下

如果连接失败会报错。

#coding:utf-8
#python2.7使用MySQLdb操作mysql
import MySQLdb
print("=====================mysql数据库=====================")

getRC = lambda cur: cur.rowcount if hasattr(cur, 'rowcount') else -1                    #获取游标所指向是数据的行数

HOST = "127.0.0.1"  #数据库主机
USER = 'root'  #用户名
PASSWORD = '111111'  #密码
DBNAME = 'note'  #数据名称
try:
    conn = MySQLdb.connect(HOST,USER,PASSWORD,DBNAME)                   #连接mysql数据库,参数:主机号、用户名、密码、数据库
    curs=conn.cursor()                                                                 # 获取游标
    curs.execute('CREATE TABLE student(id INTEGER PRIMARY KEY,name TEXT)')             # 执行代码,创建表和字段
    curs.execute("INSERT INTO student VALUES(1,'student1')");                           # 添加记录 
    curs.execute("INSERT INTO student VALUES(%d, '%s')" % (2, 'student2'))                #添加记录
    conn.commit()                                                                     # 每次执行完后都应该保存
except Exception:print("数据表和记录已经添加")
finally:
    curs.execute("UPDATE student SET name='student3' WHERE id=2")            #更新记录
    curs.execute("SELECT * FROM student")                                # 查询记录
    for row in curs.fetchall():
        print(row[0],row[1])
    curs.execute('DELETE FROM student WHERE id=%d' % 1)                                 #删除记录
    curs.execute('DROP TABLE student')                                                  #删除表
    curs.close()                                                                       #关闭游标
    conn.close()                                                                       #关闭连接

 


原文:https://blog.csdn.net/luanpeng825485697/article/details/77816801 
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值