3

    科技2022-07-12  130

    文章目录

    操作 SQLite3数据库创建表插入数据插入一条数据插入多条数据 查询数据查询所有数据查询一条数据 删除数据删除数据库


    简单的sqllite 语法- 文档查询


    操作 SQLite3数据库

    导入相关库或模块(SQLite3)。使用connect()连接数据库并获取数据库连接对象。它提供了以下方法: .cursor() 方法来创建一个游标对象.commit() 方法来处理事务提交.rollback() 方法来处理事务回滚.close() 方法来关闭一个数据库连接 使用con.cursor()获取游标对象。使用游标对象的方法(execute()、executemany()、fetchall()等)来操作数据库,实现插入、修改和删除操作,并查询获取显示相关的记录。在Python程序中,连接函数sqlite3.connect()有如下两个常用参数。 database:表示要访问的数据库名。timeout:表示访问数据的超时设定。 使用close()关闭游标对象和数据库连接。数据库操作完成之后,必须及时调用其close()方法关闭数据库连接,这样做的目的是减轻数据库服务器的压力。

    创建表

    # 使用SQLite3创建表 # 使用sqlite3模块的connect方法来创建/打开数据库,需要指定数据库路径,不存在则创建一个新的数据库。 #导入sqllite3模块 import sqlite3 # 1.硬盘上创建连接 con = sqlite3.connect('D:/Python/StudyPython/Jupyter/数据库编程/sqlitedb/first.db') # 获取cursor对象 cur = con.cursor() # 执行sql创建表 sql = 'create table t_person(pno INTEGER PRIMARY KEY AUTOINCREMENT ,pname varchar(30) NOT NULL ,age INTEGER)' try: cur.execute(sql) except Exception as e: print(e) print('创建表失败') finally: # 关闭游标 cur.close() # 关闭连接 con.close()

    插入数据

    插入一条数据

    # 插入一条数据 #导入模块 import sqlite3 #创建连接 con=sqlite3.connect('D:/Python/StudyPython/Jupyter/数据库编程/sqlitedb/first.db') #创建游标对象 cur=con.cursor() #编写插入sql sql='insert into t_person(pname,age) values(?,?)' try: # 执行sql cur.execute(sql, ('张三', 24)) #提交事务 con.commit() print('插入数据成功') except Exception as e: print(e) con.rollback() # 回滚 print('插入数据失败') finally: #关闭游标连接 cur.close() #关闭数据库连接 con.close()

    插入多条数据

    # 插入多条数据 #导入模块 import sqlite3 #创建连接 con=sqlite3.connect('D:/Python/StudyPython/Jupyter/数据库编程/sqlitedb/first.db') #创建游标对象 cur=con.cursor() #编写插入sql sql='insert into t_person(pname,age) values(?,?)' try: # 执行插入多条数据的sql cur.executemany(sql,[('小李',23),('小花',34),('小明',28)]) #提交事务 con.commit() print('插入多条数据成功') except Exception as e: print(e) con.rollback() print('插入多条数据失败') finally: #关闭游标连接 cur.close() #关闭数据库连接 con.close()

    查询数据

    查询所有数据

    # 查询所有数据 #导入模块 import sqlite3 #创建连接 con=sqlite3.connect('D:/Python/StudyPython/Jupyter/数据库编程/sqlitedb/first.db') #创建游标对象 cur=con.cursor() #创建查询sql sql='select * from t_person' try: cur.execute(sql) #获取结果集 # fetchall 获取方法 person_all=cur.fetchall() # print(person_all) for person in person_all: print(person) except Exception as e: print(e) print('查询所有数据失败') finally: #关闭游标 cur.close() #关闭连接 con.close()

    查询一条数据

    # 查询一条数据 import sqlite3 #创建连接 con=sqlite3.connect('D:/Python/StudyPython/Jupyter/数据库编程/sqlitedb/first.db') #创建游标对象 cur=con.cursor() #创建查询sql sql='select * from t_person' try: cur.execute(sql) person_all=cur.fetchone() # print(person_all) for person in person_all: print(person) except Exception as e: print(e) print('查询所有数据失败') finally: #关闭游标 cur.close() #关闭连接 con.close()

    删除数据

    # sqlite3 数据库删除数据 #导入模块 import sqlite3 #创建连接 con=sqlite3.connect('D:/Python/StudyPython/Jupyter/数据库编程/sqlitedb/first.db') #创建游标对象 cur=con.cursor() #编写删除数据的SQL语句 sql='delete from t_person where pno=?' #执行sql try: cur.execute(sql, (2,)) #提交事务 con.commit() print('删除成功') except Exception as e: print(e) print('删除失败') con.rollback() finally: #关闭连接 con.close()

    删除数据库

    # sqlite3 数据库删除数据 #导入模块 import sqlite3 #创建连接 con=sqlite3.connect('D:/Python/StudyPython/Jupyter/数据库编程/sqlitedb/first.db') #创建游标对象 cur=con.cursor() #编写删除数据的SQL语句 sql='delete from t_person where pno=?' #执行sql try: cur.execute(sql, (2,)) #提交事务 con.commit() print('删除成功') except Exception as e: print(e) print('删除失败') con.rollback() finally: #关闭连接 con.close()
    Processed: 0.010, SQL: 8