sqlite3常用命令以及django如何操作sqlite3數(shù)據(jù)庫

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

1、確認sqlite3是否已經(jīng)安裝

進去python命令行,執(zhí)行

>>> import sqlite3
>>>

沒有報錯,說明sqlite3已經(jīng)成功安裝了

2、如何進入sqlite3命令行

sqlite3 /path/to/dbname

直接執(zhí)行sqlite3 加數(shù)據(jù)庫名即可

 ~ 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、查詢表中總的數(shù)據(jù)條目數(shù)

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、執(zhí)行多條查詢語句

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如何執(zhí)行sqlite查詢命令

python執(zhí)行sqlite命令的流程:

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

創(chuàng)建或打開數(shù)據(jù)庫文件,如果數(shù)據(jù)庫文件不存在,則創(chuàng)建,存在,則打開該文件。cx為數(shù)據(jù)庫連接對象,它可以有以下操作: commit()--事務提交 rollback()--事務回滾 close()--關(guān)閉一個數(shù)據(jù)庫連接 cursor()--創(chuàng)建一個游標

2、cursor = cx.cursor()

定義了一個游標。游標對象有以下的操作: execute()--執(zhí)行sql語句 executemany--執(zhí)行多條sql語句 close()--關(guān)閉游標 fetchone()--從結(jié)果中取一條記錄 fetchmany()--從結(jié)果中取多條記錄 fetchall()--從結(jié)果中取出多條記錄 scroll()--游標滾動 關(guān)于對象的方法可以去 Python 主頁上查看DB API的詳細文檔

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

cursor.execute(sql語句)是執(zhí)行sql語句

4、cursor.close()

關(guān)閉游標

下面是操作數(shù)據(jù)庫的過程

>>> 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腳本,來執(zhí)行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()方法

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容