sqlite如何重新排序id号_使用Python和SQLite的SQL(二)

4f06b255304dd446d0c8161d495ee366.png

数据库提供了许多功能,通过这些功能,人们可以通过web轻松管理大量信息,并通过文本文件等典型文件输入和输出大量数据。SQL是一种查询语言,在数据库中非常流行。许多网站使用MySQL。SQLite是一个“轻量级”版本,其语法与SQL非常相似。

SQLite是一个自包含、高可靠性、嵌入式、全功能、公共域的SQL数据库引擎。它是互联网中使用最多的数据库引擎。

Python有一个用于访问SQLite数据库的库,称为sqlite3,用于与该数据库一起使用,该库自2.5版以来已包含在Python软件包中。

在本文中,我们将讨论如何使用诸如Update和Delete之类的命令查询数据库,以及如何通过图形显示数据。

更新和删除操作

# code for update operation import sqlite3   # database name to be passed as parameter conn = sqlite3.connect('mydatabase.db')   # update the student record conn.execute("UPDATE Student SET name = 'Sam' where unix='B113059'") conn.commit()   print "Total number of rows updated :", conn.total_changes   cursor = conn.execute("SELECT * FROM Student") for row in cursor:    print row,   conn.close() 

输出:

Total number of rows updated : 1(u'B113053', u'Geek', u'2017-01-11 13:53:39', 21.0), (u'B113058', u'Saan', u'2017-01-11 13:53:39', 21.0), (u'B113059', u'Sam', u'2017-01-11 13:53:39', 22.0)
# code for delete operation import sqlite3   # database name to be passed as parameter conn = sqlite3.connect('mydatabase.db')   # delete student record from database conn.execute("DELETE from Student where unix='B113058'") conn.commit() print "Total number of rows deleted :", conn.total_changes   cursor = conn.execute("SELECT * FROM Student") for row in cursor:    print row,   conn.close() 

输出:

Total number of rows deleted : 1(u'B113053', u'Geek', u'2017-01-11 13:53:39', 21.0), (u'B113059', u'Sam', u'2017-01-11 13:53:39', 22.0)

用户输入的数据

# code for executing query using input data import sqlite3   # creates a database in RAM con = sqlite3.connect(":memory:") cur = con.cursor() cur.execute("create table person (name, age, id)")   print ("Enter 5 students names:") who = [raw_input() for i in range(5)] print ("Enter their ages respectively:") age = [int(raw_input()) for i in range(5)] print ("Enter their ids respectively:") p_id = [int(raw_input()) for i in range(5)] n = len(who)   for i in range(n):       # This is the q-mark style:     cur.execute("insert into person values (?, ?, ?)", (who[i], age[i], p_id[i]))       # And this is the named style:     cur.execute("select * from person")       # Fetches all entries from table     print cur.fetchall() 

输出:

(u'Navin', 34, 113053)(u'Basu', 42, 113058)(u'Firoz', 65, 113059)(u'Tim', 47, 113060)(u'Varun', 54, 113061)

用SQLite作图

# graph visualization using matplotlib library import matplotlib.pyplot as plt   def graph_data(p_id,age):       # plotting the points         plt.plot(p_id, age, color='yellow', linestyle='dashed', linewidth = 3,     marker='*', markerfacecolor='blue', markersize=12)       # naming the x axis     plt.xlabel('Persons Id')       # naming the y axis     plt.ylabel('Ages')       # plt.plot(p_id,age)     plt.show()   print ("Enter 5 students names:") who = [raw_input() for i in range(5)] print ("Enter their ages respectively:") age = [int(raw_input()) for i in range(5)] print ("Enter their ids respectively:") p_id = [int(raw_input()) for i in range(5)]   # calling graph function graph_data(p_id,age) 

通过这种方式,我们可以使用SQL查询来执行此类操作,以与数据库进行通信并显着绘制Graph以绘制其特征。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
高校宿舍管理系统需要设计数据库模型,包括宿舍楼信息、房间信息、学生信息等。以下是一个简单的数据库模型设计: - 宿舍楼表 dormitory_building:id、name、address、manager - 房间表 room:id、building_id、room_number、number_of_beds、available_beds - 学生表 student:id、name、gender、birthday、major、room_id 其中,dormitory_building 的 id 为主键,room 的 id 为主键,student 的 id 为主键,room 的 building_id 为外键,student 的 room_id 为外键。 接下来,我们可以使用 SQL 语句创建上述表格,并插入测试数据。 ```sql -- 创建宿舍楼表 CREATE TABLE dormitory_building ( id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, address VARCHAR(50) NOT NULL, manager VARCHAR(50) NOT NULL ); -- 创建房间表 CREATE TABLE room ( id INT PRIMARY KEY, building_id INT NOT NULL, room_number INT NOT NULL, number_of_beds INT NOT NULL, available_beds INT NOT NULL, FOREIGN KEY (building_id) REFERENCES dormitory_building(id) ); -- 创建学生表 CREATE TABLE student ( id INT PRIMARY KEY, name VARCHAR(50) NOT NULL, gender VARCHAR(10) NOT NULL, birthday DATE NOT NULL, major VARCHAR(50) NOT NULL, room_id INT NOT NULL, FOREIGN KEY (room_id) REFERENCES room(id) ); -- 插入测试数据 INSERT INTO dormitory_building VALUES (1, '1宿舍楼', 'XX路XX', '张三'); INSERT INTO dormitory_building VALUES (2, '2宿舍楼', 'XX路XX', '李四'); INSERT INTO room VALUES (1, 1, 101, 4, 4); INSERT INTO room VALUES (2, 1, 102, 4, 3); INSERT INTO room VALUES (3, 2, 201, 4, 4); INSERT INTO student VALUES (1, '小明', '男', '1999-01-01', '计算机科学', 1); INSERT INTO student VALUES (2, '小红', '女', '1999-02-02', '数学', 1); INSERT INTO student VALUES (3, '小张', '男', '1999-03-03', '物理', 2); INSERT INTO student VALUES (4, '小李', '女', '1999-04-04', '化学', 3); ``` 接下来,我们可以使用 Python 语句连接数据库,实现宿舍管理系统的基本功能,例如查询宿舍楼、查询房间、查询学生、分配房间等。 ```python import sqlite3 # 连接数据库 conn = sqlite3.connect('dormitory.db') # 查询宿舍楼 def get_dormitory_building(): cursor = conn.execute('SELECT * FROM dormitory_building;') for row in cursor: print(row) # 查询某个宿舍楼的房间 def get_room(building_id): cursor = conn.execute('SELECT * FROM room WHERE building_id=?;', (building_id,)) for row in cursor: print(row) # 查询某个房间的学生 def get_student(room_id): cursor = conn.execute('SELECT * FROM student WHERE room_id=?;', (room_id,)) for row in cursor: print(row) # 分配房间 def assign_room(student_id, room_id): cursor = conn.execute('UPDATE student SET room_id=? WHERE id=?;', (room_id, student_id)) conn.commit() print('分配成功!') # 查询某个学生所在的房间 def get_student_room(student_id): cursor = conn.execute('SELECT room.* FROM student JOIN room ON student.room_id=room.id WHERE student.id=?;', (student_id,)) for row in cursor: print(row) # 测试代码 get_dormitory_building() get_room(1) get_student(1) assign_room(4, 1) get_student_room(4) # 关闭数据库连接 conn.close() ``` 以上代码实现了查询宿舍楼、查询房间、查询学生、分配房间、查询学生所在的房间等功能,可以根据实际需求进行扩展和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值