笔记:Python操作sql
python操作mysql步骤:
创建connect连接
conn = connect(host='127.0.0.1', port=3306, user='root', password='123456', database='jing_dong', charset='utf8')
获得cursor对象
cursor = conn.cursor()
执行SQL语句
cursor.execute('select * from xxx')
关闭corser和conn连接
cursor.close()
conn.close()
from pymysql import connect
class JD(object):
def __init__(self):
# 创建connect连接
self.conn = connect(host='127.0.0.1', port=3306, user='root',\
password='123456', database='jing_dong', charset='utf8')
# 获得cursor对象
self.cursor = self.conn.cursor()
def __del__(self):
# 关闭corser对象
self.cursor.close()
self.conn.close()
def execute_sql(self, sql):
self.cursor.execute(sql)
for temp in self.cursor.fetchall():
print(temp)
def show_all_item(self):
"""显示所有商品"""
sql = 'SELECT * FROM goods'
self.execute_sql(sql)
def show_cates(self):
"""显示所有商品"""
sql = 'SELECT name FROM goods_cates'
self.execute_sql(sql)
def show_brand(self):
"""显示所有的商品的品牌分类"""
sql = 'SELECT name FROM goods_brand'
self.execute_sql(sql)
def add_brand(self):
"""添加一个商品分类"""
item_name = input('输入新商品分类的名称: ')
sql = """INSERT INTO goods_cates(name) VALUES ('%s')""" % item_name
self.cursor.execute(sql)
self.conn.commit()
@staticmethod
def print_menu():
print('-----京东-----')
print('1.所有的商品')
print('2.所有的商品的分类')
print('3.所有的商品的品牌分类')
print('4.添加一个商品分类')
return input('请输入功能对应的序号: ')
def run(self):
while True:
op = self.print_menu()
if op == '1':
# 查询所有商品
self.show_all_item()
elif op == '2':
# 查询所有的商品的分类
self.show_cates()
elif op == '3':
# 查询所有的商品的品牌分类
self.show_brand()
elif op == '4':
# 添加一个商品分类
self.add_brand()
else:
print('输入有误,请重新输入...')
def main():
# 1.创建一个JD对象
jd = JD()
# 2.调用JD对象的run方法
jd.run()
if __name__ == '__main__':
main()
操作SQL修改数据库时
commit()
方法进行提交,如果在执行commit()
方法前反悔,可以执行rollback()
方法进行回退。
笔记:Python操作sql的更多相关文章
- 笔记-python操作mysql
笔记-python操作mysql 1. 开始 1.1. 环境准备-mysql create database db_python; use db_python; create tabl ...
- MongoDB学习笔记:Python 操作MongoDB
MongoDB学习笔记:Python 操作MongoDB Pymongo 安装 安装pymongopip install pymongoPyMongo是驱动程序,使python程序能够使用Mong ...
- Python 学习笔记:Python 操作 SQL Server 数据库
最近要将数据写到数据库里,学习了一下如何用 Python 来操作 SQL Server 数据库. 一.连接数据库: 首先,我们要连接 SQL Server 数据库,需要安装 pymssql 这个第三方 ...
- C#学习笔记---C#操作SQL数据库
C#操作SQL数据库 Connection(连接)对象 连接字符串: 形式1.”server=;uid=;pwd=;database=” 形式2.”server=;Intergrated Securi ...
- python操作SQL
pymysql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同 一.下载安装 pip3 install pymysql 二.操作使用 1.执行SQL #!/usr ...
- Python 操作 SQL 数据库 (ORCAL)
MySQLdb.connect是python 连接MySQL数据库的方法,在Python中 import MySQLdb即可使用,至于connect中的参数很简单:host:MySQL服务器名user ...
- python操作sql server2008 pyodbc
使用Python通过PyODBC连接数据的注意事项 今天使者用PyODBC连接数据库,试了很久才出来,现把一些心得体会和大家分享! 一.PyODBC的下载地址: http://code.google. ...
- MongoDB 学习笔记(python操作)
转自: http://blog.csdn.net/daillo/article/details/7030910
- 潭州课堂25班:Ph201805201 python 操作数据库 第五课 (课堂笔记)
一 用 python 操作 mysql 1,导入 pymysql 2,检查配置文件, 3,端口转发 如果 python 在本机,数据库在远程,或虚拟机则需要 4用 python 连接 # -*- co ...
随机推荐
- c语言NULL和0区别及NULL详解
先看下面一段代码输出什么: #include<stdo.h> int main() { int *p=NULL; printf("%s",p); } 输出<n ...
- jvm监控命令-jstat
jstat 用于查看服务器上某个服务的GC情况. 一般使用方式jstat –gcpid或jstat –utilpid 时间间隔-每个一定时间(指定的时间间隔)输出一次进程pid的内存情况及gc情况. ...
- Sigils of Elohim
题目大意 见游戏链接https://store.steampowered.com/app/321480/. 分析 作为一个程序猿,我拒绝用人脑dfs. 代码如下 #include <bits/s ...
- sshpass批量分发ssh秘钥
首先安装sshpass: yum -y install sshpass 单条命令: sshpass -p“password” ssh-copy-id -i /root/.ssh/id_rsa.pub ...
- Substring UVA - 11468 AC自动机+概率DP
题意: 给出一些字符和各自对应的选择概率,随机选择L次后得到一个长度为L的随机字符串S. 给出K个模板串,计算S不包含任何一个模板串的概率 dp[i][j]表示走到AC自动机 i 这个节点 还需要走 ...
- Error: setup script specifies an absolute path
在安装sklearn的时候,出现: error: Error: setup script specifies an absolute path: /opt/xgboost-0.47/python-pa ...
- mysql 数据库基本命令
停止mysql服务:net stop mysql //管理员方式运行 启动mysql服务:net start mysql 进入数据库:mysql -u root -p 查看数据库:show ...
- BBS论坛 自定义form组件
二.自定义form组件 from django import forms from django.forms import widgets from app01 import models # 定制f ...
- (转)C++ 11 多线程--线程管理
说到多线程编程,那么就不得不提并行和并发,多线程是实现并发(并行)的一种手段.并行是指两个或多个独立的操作同时进行.注意这里是同时进行,区别于并发,在一个时间段内执行多个操作.在单核时代,多个线程是并 ...
- JS对象 神奇的Math对象,提供对数据的数学计算。注意:Math 对象是一个固有的对象,无需创建它,直接把 Math 作为对象使用就可以调用其所有属性和方法。这是它与Date,String对象的区别
Math对象 Math对象,提供对数据的数学计算. 使用 Math 的属性和方法,代码如下: <script type="text/javascript"> var m ...