sqlite3常用命令以及django如何操作sqlite3数据库
一、如何进入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_permissionsucloud_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 = .lower() for _ in cursor.description]
>>> columns
['uhost', 'project', 'zone']
>>> for _ in cursor:
... print _
...
(147, 10, 11)
>>> results =
>>> 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
cursor = dbs.cursor()
cursor.execute(sql)
columns = .lower() for _ in cursor.description]
results =
cursor.close()
return results
这里脚本里面,用到了zip()方法和dict()方法
学习了
页:
[1]