python使用pymysql模块操作MySQL

目录
  • 实例一:插入数据
  • 实例二:获取某个表全部数据
  • 实例三:根据cName模糊搜索

实例一:插入数据

import pymysql
import tkinter as tk

conn = pymysql.connect(host='localhost', user='root', passwd='root', db='okzl', charset='utf8')

master = tk.Tk()
master.title("插入供应商信息")
master.geometry('350x300')
tk.Label(master, text='cName').place(x=30,y=10)
tk.Label(master, text='address').place(x=30,y=40)
tk.Label(master, text='linkman').place(x=30,y=70)
tk.Label(master, text='linkPhone').place(x=30,y=100)
tk.Label(master, text='credit').place(x=30,y=130)
tk.Label(master, text='remark').place(x=30,y=160)
in1=tk.Entry(master, width=30).place(x=100,y=10)
in2=tk.Entry(master, width=30).place(x=100,y=40)
in3=tk.Entry(master, width=30).place(x=100,y=70)
in3=tk.Entry(master, width=30).place(x=100,y=100)
in3=tk.Entry(master, width=30).place(x=100,y=130)
in3=tk.Entry(master, width=30).place(x=100,y=160)

def insert():
    cur = conn.cursor()  # 伸出手
    sql1 = "insert into pro(cName,address,linkman,linkPhone,credit,remark) values(%s,%s,%s,%s,%s,%s)"
    temp2 = ( )
    cur.execute(sql1, temp2)
    conn.commit()
    cur.close()

tk.Button(master,text='插入',width=8,command=insert).place(x=140,y=220)

master.mainloop()
conn.close()

成功插入数据

实例二:获取某个表全部数据

import pymysql

conn = pymysql.connect(host='localhost', user='root', passwd='root', db='okzl', charset='utf8')
cur = conn.cursor()

cur.execute('select * from pro')
data = cur.fetchall()

cur.close()
print(data)
conn.close()

实例三:根据cName模糊搜索

import pymysql
import tkinter as tk

conn = pymysql.connect(host='localhost', user='root', passwd='root', db='okzl', charset='utf8')  # 连接数据库

master = tk.Tk()
master.title("搜索某客户信息")
master.geometry('350x300')

e = tk.Entry(master)
e.pack(padx=20, pady=20)

def tosearch():
    cur = conn.cursor()
    temp2 = (e.get(), "%" + e.get() + "%")
    cur.execute("select * from pro where cName like %s or cName like %s ", temp2)
    data = cur.fetchall()
    cur.close()
    print(data)

tk.Button(master, text='搜索', width=8, command=tosearch).pack(padx=20, pady=50)

master.mainloop()

conn.close()

实例四:修改数据

根据数据库自动给数据生成的id来确认目标和修改数据

import pymysql
import tkinter as tk

conn = pymysql.connect(host='localhost', user='root', passwd='root', db='okzl', charset='utf8')

master = tk.Tk()
master.title("修改供应商信息")
master.geometry('350x300')
tk.Label(master, text='cName').place(x=30,y=10)
tk.Label(master, text='address').place(x=30,y=40)
tk.Label(master, text='linkman').place(x=30,y=70)
tk.Label(master, text='linkPhone').place(x=30,y=100)
tk.Label(master, text='credit').place(x=30,y=130)
tk.Label(master, text='remark').place(x=30,y=160)
tk.Label(master, text='目标id').place(x=30,y=190)
in1=tk.Entry(master, width=30)
in1.place(x=100,y=10)
in2=tk.Entry(master, width=30)
in2.place(x=100,y=40)
in3=tk.Entry(master, width=30)
in3.place(x=100,y=70)
in4=tk.Entry(master, width=30)
in4.place(x=100,y=100)
in5=tk.Entry(master, width=30)
in5.place(x=100,y=130)
in6=tk.Entry(master, width=30)
in6.place(x=100,y=160)
in7=tk.Entry(master, width=30)
in7.place(x=100,y=190)

def update():
    cur = conn.cursor()  # 伸出手
    sql1 = "update pro set cName=%s, address=%s,linkman=%s,linkPhone=%s,credit=%s,remark=%s where id=%s"
    temp2 = (in1.get(),in2.get(),in3.get(),in4.get(),in5.get(),in6.get(),in7.get())
    cur.execute(sql1, temp2)
    conn.commit()
    cur.close()

tk.Button(master,text='确认修改',width=8,command=update).place(x=140,y=220)

master.mainloop()
conn.close()

实例五:删除数据

这里是根据id删除

sql1 = "delete from pro where id=%s"
temp1 = str(n)
cur.execute(sql1, temp1)
conn.commit()
cur.close()

上述实例均为基础实现操作举例,实际操作中可根据需求更改程序和sql语句实现目标效果

以上就是python使用pymysql模块操作MySQL的详细内容,更多关于python 用pymysql操作MySQL的资料请关注我们其它相关文章!

(0)

相关推荐

  • python数据库操作mysql:pymysql、sqlalchemy常见用法详解

    本文实例讲述了python数据库操作mysql:pymysql.sqlalchemy常见用法.分享给大家供大家参考,具体如下: 相关内容: 使用pymysql直接操作mysql 创建表 查看表 修改表 删除表 插入数据 查看数据 修改数据 删除数据 使用sqlmary操作mysql 创建表 查看表 修改表 删除表 插入数据 查看数据 修改数据 删除数据 首发时间:2018-02-24 23:59 修改: 2018-06-15,发现自己关于pymysql写了对于数据的操作示例,但没有写表结构的示例

  • pymysql实现增删改查的操作指南(python)

    1.安装pymysql:pip install pymysql (在命令行窗口中执行) 2.卸载pymysql:pip uninstall pymysql (在命令行窗口中执行) 数据库的连接 需要注意的是port是不用引号括起来 charset是utf8不是utf-8 # 获取数据库连接对象 connection = pymysql.connect(host='localhost', port=3306, user='root', passwd='2732195202', db='book',

  • python 基于PYMYSQL使用MYSQL数据库

    在做测试的时候都会用到数据库,今天写一篇通过python连接MYSQL数据库 什么是MYSQL数据库 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件之一. 什么是PYMYSQL PyMySQL 是在 Python3.x 版本中用于

  • python pymysql链接数据库查询结果转为Dataframe实例

    我就废话不多说了,大家还是直接看代码吧! import pymysql import pandas as pd def con_sql(db,sql): # 创建连接 db = pymysql.connect(host='127.0.0.1', port=3308, user='name', passwd='password', db=db, charset='utf8') # 创建游标 cursor = db.cursor() cursor.execute(sql) result = curs

  • Python pymysql模块安装并操作过程解析

    pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同.但目前pymysql支持python3.x而后者不支持3.x版本. 本文环境 python3.6.1 Mysql 5.7.18 1.安装模块 pip3 install pymysql 2.python操作 1) 获取查询数据 #!/usr/bin/env python # -*- coding:utf-8 -*- import pymysql # 创建连接 conn = pymysql.connect(host

  • Python使用pycharm导入pymysql教程

    file->setting->project->project interperter 双击右侧出现的pip,弹出安装包,搜索pymysql->选择第一个->Install Package 出现底部绿色字体说明导入成功 补充知识:Python 3.6.X导入pymysql模块出错:No module named 'pymysql'问题 可能原因是因为使用的是pycharm在新建项目的时候没有正确的选择导致的 1.成功安装mysql,3.0版本的执行命令:pip3 instal

  • Python 解析pymysql模块操作数据库的方法

    pymysql 是 python 用来操作MySQL的第三方库,下面具体介绍和使用该库的基本方法. 1.建立数据库连接 通过 connect 函数中 parameter 参数 建立连接,连接成功返回Connection对象 import pymysql #建立数据库连接 connection = pymysql.connect(host = 'localhost', user = 'root', password = '123456', database = 'mydb', charset =

  • 在python中使用pymysql往mysql数据库中插入(insert)数据实例

    咱还是直接看代码吧! from pymysql import * def main(): # 创建connection连接 conn = connect(host='', port=3306, database='', user='', password='', charset='utf8') # 获取cursor对象 cs1 = conn.cursor() # 执行sql语句 query = 'insert into 表名(列名1, 列名2, 列名3, 列名4, 列名5, 列名6) value

  • python pymysql库的常用操作

    批量插入 import pymysql def insert_to_mysql(to_db_list): mysql_db = pymysql.connect(host="HOST_IP", port=3306, user="username", password="password", database="db", charset="utf8") cursor = mysql_db.cursor() sq

  • Python使用pymysql模块操作mysql增删改查实例分析

    本文实例讲述了Python使用pymysql模块操作mysql增删改查.分享给大家供大家参考,具体如下: # -*- coding:utf-8 -*- import pymysql user = input('请输入用户名:') pwd = input('请输入密码:') # 1.连接 conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='123', db='t1', charset='utf8')

随机推荐