设为首页 收藏本站
查看: 2438|回复: 1

[经验分享] sqlite3常用命令以及django如何操作sqlite3数据库

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-3-10 16:32:36 | 显示全部楼层 |阅读模式
一、如何进入sqlite3交互模式进行命令操作?
1、确认sqlite3是否已经安装
进去python命令行,执行
1
2
>>> import sqlite3
>>>



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

2、如何进入sqlite3命令行
1
sqlite3 /path/to/dbname



直接执行sqlite3 加数据库名即可
1
2
3
4
~ 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 :查看所有表
1
2
3
4
5
6
7
8
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、查询表中总的数据条目数
1
select count() from TableName;



例如:
1
2
3
4
5
6
sqlite> select count() from ucloud_zone;
11
sqlite> select count() from ucloud_uhost;
147
sqlite> select count() from ucloud_project;
10




5、执行多条查询语句
1
2
3
4
5
6
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 onsqlite>.mode columnsqlite>.timer onsqlite>
更多命令查看:
http://www.runoob.com/sqlite/sqlite-commands.html



二、python如何执行sqlite查询命令
python执行sqlite命令的流程:
1
1、cx = sqlite3.connect("db.sqlite3)



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

1
2、cursor = cx.cursor()



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

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



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

1
4、cursor.close()



关闭游标

下面是操作数据库的过程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
>>> import sqlite3
>>> from django.db import connections
cx = sqlite3.connect("/Users/cengchengpeng/Downloads/django_test/cmdb/db.sqlite3")
cursor = cx.cursor()
>>> cursor
<sqlite3.Cursor object at 0x10b24cb20>
>>> 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
... """)
<sqlite3.Cursor object at 0x10b24cb20>
>>> 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语句
1
2
3
4
5
6
7
8
9
10
11
#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()方法


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-350662-1-1.html 上篇帖子: android sqlite3 show all tables 下篇帖子: PHP 使用PDO方法连接数据库实例 数据库 如何
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表