一、如何進入sqlite3交互模式進行命令操作?
成都創(chuàng)新互聯(lián)公司專注于宿松企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站制作。宿松網(wǎng)站建設(shè)公司,為宿松等地區(qū)提供建站服務(wù)。全流程按需開發(fā),專業(yè)設(shè)計,全程項目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
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
您可以使用下列的點命令來格式化輸出為本教程下面所列出的格式:
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)建一個游標
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()方法
網(wǎng)站欄目:sqlite3常用命令以及django如何操作sqlite3數(shù)據(jù)庫
URL地址:http://bm7419.com/article34/jcehpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機網(wǎng)站建設(shè)、網(wǎng)站營銷、云服務(wù)器、App設(shè)計、服務(wù)器托管、品牌網(wǎng)站設(shè)計
聲明:本網(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)