久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python中查看.db文件中表格的名字及表格中的字段操作

瀏覽:129日期:2022-07-18 13:27:43

1.問(wèn)題描述: 我桌面上有一個(gè)“賬號(hào)密碼.db”文件,我現(xiàn)在想知道里面有幾張表格table、表格的名字、表頭結(jié)構(gòu)。

2.使用SQL語(yǔ)句'''select name from sqlite_master where type=’table’ order by name''',查找表格的名字。實(shí)例代碼如下:

# coding:utf-8import sqlite3conn = sqlite3.connect('C:UsersAdministratorDesktop密碼賬號(hào).db')cursor = conn.cursor()sql = '''select name from sqlite_master where type=’table’ order by name'''cursor.execute(sql)result = cursor.fetchall()print resultprint type(result)conn.close()

輸出結(jié)果為:

D:Python3python27python.exe D:/PyCharm/dytt_spider/mongo.py[(u’students’,)]<type ’list’> Process finished with exit code 0

可以看出,“密碼賬號(hào).db”文件中有1張表格,表格名字為“students”。

3.使用SQL語(yǔ)句'''PRAGMA table_info(students)''',查找“students”表格中的表頭結(jié)構(gòu)。

# coding:utf-8import sqlite3conn = sqlite3.connect('C:UsersAdministratorDesktop密碼賬號(hào).db')cursor = conn.cursor()sql = '''pragma table_info(students)'''cursor.execute(sql)result = cursor.fetchall()print resultprint type(result)conn.close()

輸出結(jié)果為:

D:Python3python27python.exe D:/PyCharm/dytt_spider/mongo.py[(0, u’name’, u’text’, 0, None, 0), (1, u’usename’, u’text’, 0, None, 0), (2, u’id’, u’int’, 0, None, 0)]<type ’list’> Process finished with exit code 0

可以看出“students”表中有“name”、“username”、id 三列。

補(bǔ)充知識(shí):python中sqlite3模塊查詢數(shù)據(jù)一條或多條

我就廢話不多說(shuō)了,大家還是直接看代碼吧~

#導(dǎo)入模塊import sqlite3#創(chuàng)建鏈接con = sqlite3.connect(’C:python_learnDBASQLite3demosqlite3demo.db’)#創(chuàng)建游標(biāo)對(duì)象cur = con.cursor()#編寫(xiě)sql語(yǔ)句sql = 'select * from t_person '#執(zhí)行語(yǔ)句try: cur.execute(sql) #獲取結(jié)果集 person_all = cur.fetchall() #獲取所有數(shù)據(jù) # person_all = cur.fetchone() #獲取一條數(shù)據(jù) for person in person_all: print(person) print('查詢數(shù)據(jù)成功')except Exception as e: print(e) print('查詢數(shù)據(jù)失敗')finally: cur.close() con.close()

以上這篇python中查看.db文件中表格的名字及表格中的字段操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 盐山县| 永寿县| 沙田区| 满洲里市| 绥德县| 齐齐哈尔市| 晋中市| 建昌县| 梨树县| 松江区| 纳雍县| 黄山市| 正安县| 建昌县| 江川县| 繁峙县| 崇义县| 旬邑县| 澎湖县| 呼和浩特市| 银川市| 景东| 嫩江县| 浦城县| 石门县| 儋州市| 韶关市| 孝感市| 衡阳市| 青阳县| 鹰潭市| 密云县| 彭阳县| 项城市| 玉山县| 米林县| 建始县| 杭锦后旗| 牟定县| 枞阳县| 淳化县|