日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

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

發(fā)布時(shí)間:2024/7/23 python 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python db文件_python中查看.db文件中表格的名字及表格中的字段操作 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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-8

import sqlite3

conn = sqlite3.connect("C:\\Users\\Administrator\\Desktop\\密碼賬號(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 result

print type(result)

conn.close()

輸出結(jié)果為:

D:\Python3\python27\python.exe D:/PyCharm/dytt_spider/mongo.py

[(u'students',)]

Process finished with exit code 0

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

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

# coding:utf-8

import sqlite3

conn = sqlite3.connect("C:\\Users\\Administrator\\Desktop\\密碼賬號(hào).db")

cursor = conn.cursor()

sql = """pragma table_info(students)"""

cursor.execute(sql)

result = cursor.fetchall()

print result

print type(result)

conn.close()

輸出結(jié)果為:

D:\Python3\python27\python.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)]

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_learn\DBA\SQLite3demo\sqlite3demo.db')

#創(chuàng)建游標(biāo)對(duì)象

cur = con.cursor()

#編寫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è)參考,也希望大家多多支持python博客。

總結(jié)

以上是生活随笔為你收集整理的python db文件_python中查看.db文件中表格的名字及表格中的字段操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。