sqlite3常用命令以及django如何操作sqlite3數(shù)據(jù)庫-創(chuàng)新互聯(lián)

一、如何進(jìn)入sqlite3交互模式進(jìn)行命令操作?

成都創(chuàng)新互聯(lián)公司是一家以網(wǎng)站建設(shè)公司、網(wǎng)頁設(shè)計、品牌設(shè)計、軟件運(yùn)維、seo優(yōu)化、小程序App開發(fā)等移動開發(fā)為一體互聯(lián)網(wǎng)公司。已累計為人造霧等眾行業(yè)中小客戶提供優(yōu)質(zhì)的互聯(lián)網(wǎng)建站和軟件開發(fā)服務(wù)。

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

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

>>> import sqlite3 >>>

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

2、如何進(jìn)入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()--事務(wù)提交 rollback()--事務(wù)回滾 close()--關(guān)閉一個數(shù)據(jù)庫連接 cursor()--創(chuàng)建一個游標(biāo)

2、cursor = cx.cursor()

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

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

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

4、cursor.close()

關(guān)閉游標(biāo)

下面是操作數(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()方法

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

本文標(biāo)題:sqlite3常用命令以及django如何操作sqlite3數(shù)據(jù)庫-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://muchs.cn/article10/egcgo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)虛擬主機(jī)、搜索引擎優(yōu)化、網(wǎng)站策劃、手機(jī)網(wǎng)站建設(shè)定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)