首页 > 数据库 >使用python操作数据库

使用python操作数据库

时间:2022-12-16 01:55:05浏览次数:44  
标签:execute python 数据库 db cursor close sqlite3 操作 conn

import sqlite3
conn=sqlite3.connect('mrsoft.db')
cursor=conn.cursor()
cursor.execute('create table user (id int(10) primary key name varchar(20))')
cursor.close()
conn.close()


import sqlite3
conn=sqlite3.connect('mrsoft.db')
cursor=conn.cursor()
cursor.execute('insert into user (id, name) values (1,"MRSOFT")')
cursor.execute('insert into user (id, name) values (2,"Andy")')
cursor.execute('insert into user (id, name) values (3,"明日科技小助手")')
cursor.close()
conn.commit()
conn.close()

import sqlite3
conn=sqlite3.connect('mrsoft.db')
cursor=conn.cursor()
cursor.execute('select * from user')
result1=cursor.fetchone()
print(result1)
cursor.close()
conn.close()

import sqlite3
conn=sqlite3.connect('mrsoft.db')
cursor=conn.cursor()
cursor.execute('select * from user')
result2=cursor.fetchmany(2)
print(result2)
cursor.close()
conn.close()

import sqlite3
conn=sqlite3.connect('mrsoft.db')
cursor=conn.cursor()
cursor.execute('select * from user')
result3=cursor.fetchall()
print(result3)
cursor.close()
conn.close()

import sqlite3
conn=sqlite3.connect('mrsoft.db')
cursor=conn.cursor()
cursor.execute('update user set name = ? where id = ?',('MR',1))
cursor.execute('select * from user')
result=cursor.fetchall()
print(result)
cursor.close()
conn.commit()
conn.close()

import sqlite3
conn=sqlite3.connect('mrsoft.db')
cursor=conn.cursor()
cursor.execute('delete from user where id =?',(1,))
cursor.execute('select * from user')
result=cursor.fetchall()
print(result)
cursor.close()
conn.commit()
conn.close()

import pymysql
db=pymysql.connect(host='localhost',user='root',password='crz13714817086',database='aaa')
cursor=db.cursor()
cursor.execute('SELECT VERSION()')
data=cursor.fetchone()
print('Database version : %s' %data)
db.close

import pymysql
db=pymysql.connect(host='localhost',user='root',password='crz13714817086',database='aaa')
cursor=db.cursor()
cursor.execute('DROP TABLE IF EXISTS books')
sql='''
CREATE TABLE book (
    id int(8) NOT NULL AUTO_INCREMENT,name varchar(50) NOT NULL,category varchar(50) NOT NULL,price decimal(10,2) DEFAULT NULL,publish_time date DEFAULT NULL,  PRIMARY KEY (id)) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
'''

cursor.execute(sql)
db.close()

import pymysql
db=pymysql.connect(host='localhost',user='root',password='crz13714817086',database='aaa',charset='utf8')
cursor=db.cursor()
data=[('零基础学Python','python','79.80','2018-5-20'),('python从入门到精通','python','69.80','2018-6-18'),('零基础学PHP','PHP','69.80','2017-5-21'),('PHP项目开发实战入门','PHP','79.80','2016-5-21'),('零基础学Java','Java','69.80','2017-5-21')]
try:
    cursor.executemany('insert into book(name, category, price, publish_time) values (%s,%s,%s,%s)',data)
    db.commit()
except:
    db.rollback()
        
db.close()

标签:execute,python,数据库,db,cursor,close,sqlite3,操作,conn
From: https://www.cnblogs.com/whc123/p/16986385.html

相关文章

  • 5.python-函数式编程
    函数式编程(1)定义:用一系列函数解决问题。--函数可以赋值给变量,赋值后变量绑定函数。--允许将函数作为参数传入另一个函数。(2)高阶函数:将函数作为参数或返回值的函数......
  • Python OpenCV
    OpenCV准备工作Python版本3.6OpenCV版本3.4.1.15condacreate-nOpenCV3.6python=3.6pipinstallopencv-python==3.4.1.15pipinstalldlib==19.6.1pytho......
  • python-函数
    python-函数函数的作用封装代码,提高代码的重用性函数的使用函数的使用方法:先定义在调用函数的定义把代码封装到函数的内部函数的调......
  • 4.python-定时任务框架
    定时任务框架APScheduler提供了基于间隔、周期及时间的定时任务在命令行安装#Linux操作系统:sudopipinstallapscheduler#Windows操作系统:pipinstallapscheduler......
  • python 日志 logging模块详解
    1、基本使用配置logging基本的设置,然后在控制台输出日志,importlogginglogging.basicConfig(level=logging.INFO,format='%(asctime)s-%(name)s-%(levelname)s-......
  • 学习python-flask04
    flask项目演示打开一个flask项目运行步骤1.pycharm打开项目2.安装依赖:虚拟环境用pipinstall-rreq.txt3.数据库建库:movie #root用户密码不是123需要改两个地......
  • uniapp本地文件读写操作
    说明主要是封装官方文档里面的官方文档地址:https://www.html5plus.org/doc/zh_cn/io.html创建或打开文件//fileName目录路径dirEntry之前打开过的目录(没有则不填写......
  • 计算机和操作系统基础知识
    计算机和操作系统基础知识  1、操作系统(OperatingSystem,简称OS),是管理计算机硬件与软件资源的计算机程序,同时也是计算机系统的内核与基石 操作系统需要处理如管理与配置......
  • 2.python-程序结构
    程序结构1模块Module1.1定义包含一系列数据、函数、类的文件,通常以.py结尾。1.2作用让一些相关的数据,函数,类有逻辑的组织在一起,使逻辑结构更加清晰。有利于多人合......
  • orm查询操作 以及多表的查询
    今日内容详细ORM执行SQL语句有时候ORM的操作效率可能偏低我们是可以自己编写SQL......