python+mysql基本操作

在使用python连接mysql之前,需要先安装pymysql:pip install pymysql

特别注意:python文件的命名最好不要与包名相同,否则在引用该包时,会报错

pycharm安装pymysq的方法:(其他包安装方法相同)

1、切换到对应project(项目),点击File→Settings

python+mysql基本操作_第1张图片

2、选择Project:PycharmProject→Project Interpreter,点击右侧的“+”,进入Available Packages界面

python+mysql基本操作_第2张图片

3、在Available Packages界面搜索框输入包名,点击Install Package等待安装完成即可

python+mysql基本操作_第3张图片

下面实例是日常对数据库的操作使用:

Note:为了避免输出的中文是 ?(问号) ,需要设置charset="utf8"。是utf8,不是utf-8

# -*- coding:utf-8 -*-
import pymysql

"""
在对数据进行修改(增、删、改/更新)后,都需要进行commit提交操作,否则操作失败
"""

# 连接数据库
print('连接mysql服务器......')
conn = pymysql.connect(host="localhost", user="root", passwd="123456", database="test", charset="utf8")
print('服务器连接成功!')
print('=============================')

# 创建cursor游标
cursor = conn.cursor()

"""插入"""
try:
    # 一次插入多条记录
    sql = "INSERT INTO newYear VALUES('谢广坤', 40, '男', '510811198801011122')," \
          "('谢大脚', 41, '女', '510811198801021133')," \
          "('赵四', 42, '男', '510811198801031144')," \
          "('刘能', 43, '男', '510811198801041155')," \
          "('王老七', 44, '男', '510811198801051166')"
    cursor.execute(sql)
    conn.commit()  # 提交更新的数据到数据库
    print('插入成功')
except Exception as error:  # 插入失败后,抛出异常
    print('插入数据失败')
    print(error)
# print('---------------------------------------')
#
# """查询"""
# sql = "SELECT * FROM tb_user"
# cursor.execute(sql)
# result = cursor.fetchall()
# for row in result:
#     id = row[0]
#     name = row[1]
#     print("user_id:%s, user_name:%s" % (id, name))
# print('---------------------------------------')
#
# """删除"""
# try:
#     sql = "DELETE FROM tb_user WHERE user_id=1005"
#     cursor.execute(sql)
#     conn.commit()  # 提交删除后的数据到数据库
#     print('删除成功')
# except Exception as error:
#     print("删除失败")
#     print(error)
# sql = "SELECT * FROM tb_user"
# cursor.execute(sql)
# result = cursor.fetchall()
# for row in result:
#     id = row[0]
#     name = row[1]
#     print("user_id:%s, user_name:%s" % (id, name))
# print('---------------------------------------')
#
# """更新update"""
# try:
#     sql = "UPDATE tb_user SET user_name='刘金凤' WHERE user_id=1004"
#     cursor.execute(sql)
#     conn.commit()  # 提交更新后的数据到数据库
#     print('更新成功')
# except Exception as error:
#     print(error)
#     print('更新失败')
# sql = "SELECT * FROM tb_user"
# cursor.execute(sql)
# result = cursor.fetchall()
# for row in result:
#     id = row[0]
#     name = row[1]
#     print("user_id:%s, user_name:%s" % (id, name))
# print('---------------------------------------')

# """新建表"""
# sql = "CREATE TABLE keydom(" \
#       "kid INT(10) NOT NULL, " \
#       "sname VARCHAR(20) NOT NULL, " \
#       "kage INT(3))" \
#       "DEFAULT CHARSET=utf8"
# try:
#     cursor.execute(sql)
#     conn.commit()
#     print('新表创建成功')
# except Exception as error:
#     print(error)
#     print('新表创建失败')

# """清空表"""
# try:
#     sql = "DELETE FROM newYear"
#     cursor.execute(sql)
#     conn.commit()
#     print("表清除成功")
# except Exception as error:
#     print(error)
#     print("表清除失败")

# """删除表"""
# try:
#     sql = "DROP TABLE newfile"
#     cursor.execute(sql)
#     conn.commit()
#     print("表删除成功")
# except Exception as error:
#     print(error)
#     print("表删除失败")

conn.close()
print('服务器关闭成功')
print('=============================')







你可能感兴趣的:(数据库,mysql)