17.Sqlite 数据查询
Posted KidsCode
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了17.Sqlite 数据查询相关的知识,希望对你有一定的参考价值。
本节目的:
学会Sqlite数据库中的数据查询方法。
步骤:
步骤1: 采用用SqliteStudio查询数据:
A.通过数据界面 B.使用Sql语句查询数据
步骤2: 启动SublimeText,编写Py文件如下:
三段SelectDemo 如下:
--------------------------------
import sqlite3
db_filename = 'htxx.db'
conn=sqlite3.connect(db_filename)
cursor = conn.cursor()
rows=cursor.execute('''select avg(score) from class6 where name = 'ma' ''')
for row in rows.fetchone():
print(row)
cursor.close()
conn.close()
# with sqlite3.connect(db_filename) as conn:
# cursor = conn.cursor()
# rows=cursor.execute('''select avg(score) from class6 where name = 'ma' ''')
# for row in rows.fetchone():
# print(row)
-----------------------
import sqlite3
db_filename = 'htxx.db'
with sqlite3.connect(db_filename) as conn:
cursor = conn.cursor()
cursor.execute('''select id,name,course,score from class6 where name = 'ma' ''')
for row in cursor.fetchall():
id,name,course,score=row
print('id=%d name=%s course=%s score=%f'%(id,name,course,score))
--------------------------------
import sqlite3
db_filename = 'htxx.db'
course_count=0
avgscore=0
with sqlite3.connect(db_filename) as conn:
cursor = conn.cursor()
cursor.execute('''select id,name,course,score from class6 where name = 'ma' ''')
for row in cursor.fetchall():
(id, name,course,score)=row
print('id=%d name=%s course=%s score=%f'%(id,name,course,score))
avgscore +=score
course_count+=1
avgscore=avgscore/course_count
print('average=%f'%avgscore)
--------------------
点击左下方 阅读原文 可查看博客原文
以上是关于17.Sqlite 数据查询的主要内容,如果未能解决你的问题,请参考以下文章