Veri tabanındaki tablonun sütun sayısını öğrenebilir miyiz, nasıl?
Githubta Pieter Muller isimli birinin paylaştığı kodu kırpttım senin için genel olarak iki SQL komutu kullanılmış Count ve PRAGMA sorguları şu linkten ayrıntılı bilgi alabilirsin.Veritabanı dosyasının yolunu Describe() fonksiyonunda belirtmeyi unutma takıldığın yerde sorabilirsin.
# <author>Pieter Muller</author>
# <date>2012-11-14</date>
import sqlite3 as sqlite
tablesToIgnore = ["sqlite_sequence"]
def Describe(dbFile):
connection = sqlite.connect(dbFile)
cursor = connection.cursor()
totalTables = 0
totalColumns = 0
#Tablo sayısını Bul
tableListQuery = "SELECT name FROM sqlite_master WHERE type='table' ORDER BY Name"
cursor.execute(tableListQuery)
tables = map(lambda t: t[0], cursor.fetchall())
for table in tables:
if (table in tablesToIgnore):
continue
#Pragma ile tablo içindeki kolonları bulma
columnsQuery = "PRAGMA table_info(%s)" % table
cursor.execute(columnsQuery)
numberOfColumns = len(cursor.fetchall())
totalTables += 1
totalColumns += numberOfColumns
print( "Number of Tables:\t%d" % totalTables )
print( "Total Number of Columns:\t%d" % totalColumns )
cursor.close()
connection.close()
Describe("Alarms.db")
1 Beğeni
Veritabanina gore degisir.
SQL’de standart INFORMATION_SCHEMA schema’si var.
1 Beğeni