sqlite3常用命令以及django如何操作sqlite3数据库
admin
2023-05-27 10:01:09
0

一、如何进入sqlite3交互模式进行命令操作?

1、确认sqlite3是否已经安装

进去python命令行,执行

>>> import sqlite3
>>>

没有报错,说明sqlite3已经成功安装了


2、如何进入sqlite3命令行

sqlite3 /path/to/dbname

直接执行sqlite3 加数据库名即可

 ~ sqlite3 ~/Downloads/django_test/cmdb/db.sqlite3 
sqlite3SQLite version 3.14.0 2016-07-26 15:17:14
Enter ".help" for usage hints.
sqlite>


3、.tables :查看所有表

sqlite> .tables
auth_group                  django_content_type       
auth_group_permissions      django_migrations         
auth_permission             django_session            
auth_user                   ucloud_project            
auth_user_groups            ucloud_region             
auth_user_user_permissions  ucloud_uhost              
django_admin_log            ucloud_zone


4、查询表中总的数据条目数

select count() from TableName;

例如:

sqlite> select count() from ucloud_zone;
11
sqlite> select count() from ucloud_uhost;
147
sqlite> select count() from ucloud_project;
10


5、执行多条查询语句

sqlite> select
   ...> (select count(1) from ucloud_uhost) as uhost,
   ...> (select count(1) from ucloud_project) as project,
   ...> (select count(1) from ucloud_region) as region
   ...> ;
147|10|8


6、格式化输出

您可以使用下列的点命令来格式化输出为本教程下面所列出的格式:

sqlite>.header on
sqlite>.mode column
sqlite>.timer on
sqlite>


更多命令查看:

http://www.runoob.com/sqlite/sqlite-commands.html




二、python如何执行sqlite查询命令

python执行sqlite命令的流程:

1、cx = sqlite3.connect("db.sqlite3)

创建或打开数据库文件,如果数据库文件不存在,则创建,存在,则打开该文件。cx为数据库连接对象,它可以有以下操作: commit()--事务提交 rollback()--事务回滚 close()--关闭一个数据库连接 cursor()--创建一个游标


2、cursor = cx.cursor()

定义了一个游标。游标对象有以下的操作: execute()--执行sql语句 executemany--执行多条sql语句 close()--关闭游标 fetchone()--从结果中取一条记录 fetchmany()--从结果中取多条记录 fetchall()--从结果中取出多条记录 scroll()--游标滚动 关于对象的方法可以去 Python 主页上查看DB API的详细文档


3、 cursor.execute("""
... select
... (select count(1) from ucloud_uhost) as uhost
... """)

cursor.execute(sql语句)是执行sql语句


4、cursor.close()

关闭游标


下面是操作数据库的过程

>>> import sqlite3
>>> from django.db import connections
cx = sqlite3.connect("/Users/cengchengpeng/Downloads/django_test/cmdb/db.sqlite3")
cursor = cx.cursor()
>>> cursor

>>> cursor.execute("""
... select
... (select count(1) from ucloud_uhost) as uhost,
... (select count(1) from ucloud_project) as project,
... (select count(1) from ucloud_zone) as zone
... """)

>>> cursor.description
(('uhost', None, None, None, None, None, None), ('project', None, None, None, None, None, None), ('zone', None, None, None, None, None, None))
>>> columns = [_[0].lower() for _ in cursor.description]
>>> columns
['uhost', 'project', 'zone']
>>> for _ in cursor:
...     print _
... 
(147, 10, 11)
>>> results = [dict(zip(columns, _)) for _ in cursor]
>>> results
>>> results
[{'project': 10, 'zone': 11, 'uhost': 147}]
>>> cursor.close()




写python脚本,来执行sqlite语句

#coding:utf-8
from django.db import connections

def open_sql_dict(sql, connection_name='default'):
    dbs = connections[connection_name]
    cursor = dbs.cursor()
    cursor.execute(sql)
    columns = [_[0].lower() for _ in cursor.description]
    results = [dict(zip(columns, _)) for _ in cursor]
    cursor.close()
    return results


这里脚本里面,用到了zip()方法和dict()方法



相关内容

热门资讯

陕西柞水遭遇大暴雨,五千余户群... 8月5日7时至15时,陕西省商洛市柞水县出现大到暴雨、局地大暴雨,杏坪社区地质站最大降雨量135.4...
伊朗与阿曼会谈最新细节曝光 美国有线电视新闻网(CNN)8月5日报道,伊朗和阿曼将敲定一项关于通过霍尔木兹海峡进行商业航运的框架...
大科学装置建设全面提速 创新成... 央视网消息(新闻联播):重大科技基础设施是建设科技强国的利器。今年以来,我国大科学装置建设全面提速,...
贝塔创新科技观察算力公有化趋势... 从公有云到公有算力网络:AI基础设施进入弹性调度时代 过去几年,市场更关注大模型参数、算法能力和应...
活力中国调研行丨激活创新源 合... 新华社合肥8月5日电 题:激活创新源 合肥大健康科研成果加速产业化 新华社记者高亢、张紫赟 近日,“...
凤凰连线:为何伊朗称“即使与阿... 据凤凰卫视报道,伊朗知情人士当地时间周三透露称,伊朗与阿曼就霍尔木兹海峡船舶通行安排,可能达成协议,...
伊朗称与阿曼就霍尔木兹海峡航运... 据凤凰卫视报道,伊朗外交部发言人巴加埃5日表示,伊朗与阿曼已就霍尔木兹海峡拟定航道的地理坐标达成一致...
予非知识图谱大模型完成生成式人... 近日,山东省网信办正式公布生成式人工智能服务备案名录,潍坊华创信息技术有限公司自主研发的“予非知识图...
学习语丨人工智能应该是造福全人... 近日,习近平总书记向2025世界智能产业博览会致贺信,指出:“人工智能应该是造福全人类的国际公共产品...
2026年交易全链路可追溯的智... 2026-08-05 12:10:48 作者:狼叫兽 2026年被视作智能体支付规模化落地的关键之年...