您的位置:首页 > 数据库 > SQL

sqlite3常用命令以及django如何操作sqlite3数据库

2017-03-09 11:41 471 查看
一、如何进入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
<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语句
#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()方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sqlite