## Modulok impontálása
import sqlite3
import os
## Minta adatbázis
DB_FILE = r"c:\Users\User\Downloads\Chinook_Sqlite.sqlite" # adatbázis neve
### forrás : https://github.com/lerocha/chinook-database/blob/master/ChinookDatabase/DataSources/Chinook_Sqlite.sqlite
try: ## hibakezelés fiygelés kezdet
con = sqlite3.connect(DB_FILE) ## kapcsolódás
cur = con.cursor() ## kurzor létrehozás
## paraméteres lekérdezés 1.
cur.execute(""" SELECT TrackId, Name, Composer, AlbumId, Bytes FROM Track WHERE
GenreId = ? --- sql : paraméter deffinicio
and Composer is not null """,
('23',)) ## paraméter érték direct megadása
print('---- Kiíratás (nyers, csak tartalom) ----\n')
for row in cur:
for mezo in row:
print(mezo, end=' ') # Mezők egy sorba
print() # kiíratás végén sorvégjel kitétele
print()
## lezárások, erőforrás felszabadítás
cur.close()
con.close()
except sqlite3.Error as e: # hiba elkapása
print ("Hiba történt : ", e.args[0]) # hiba üzenet
---- Kiíratás (nyers, csak tartalom) ---- 3375 No Such Thing Chris Cornell 270 3691272 3376 Poison Eye Chris Cornell 270 3890037 3377 Arms Around Your Love Chris Cornell 270 3516224 3378 Safe and Sound Chris Cornell 270 4207769 3379 She'll Never Be Your Man Chris Cornell 270 3355715 3380 Ghosts Chris Cornell 270 3799745 3381 Killing Birds Chris Cornell 270 3588776 3382 Billie Jean Michael Jackson 270 4606408 3383 Scar On the Sky Chris Cornell 270 3616618 3384 Your Soul Today Chris Cornell 270 3385722 3385 Finally Forever Chris Cornell 270 3565098 3386 Silence the Voices Chris Cornell 270 4379597 3387 Disappearing Act Chris Cornell 270 4476203 3388 You Know My Name Chris Cornell 270 3940651
## Minta adatbázis
DB_FILE = r"c:\Users\User\Downloads\Chinook_Sqlite.sqlite" # adatbázis neve
### forrás : https://github.com/lerocha/chinook-database/blob/master/ChinookDatabase/DataSources/Chinook_Sqlite.sqlite
try: ## hibakezelés fiygelés kezdet
con = sqlite3.connect(DB_FILE) ## kapcsolódás
cur = con.cursor() ## kurzor létrehozás
## paraméteres lekérdezés 2.
cur.execute(""" SELECT TrackId, Name, Composer, AlbumId, Bytes FROM Track WHERE
GenreId = :GenreId --- sql : nevesített paraméter deffinicio
and Composer > :Composer """,
{"GenreId": "23", "Composer": "A"}) ## paraméter szótár érték direct megadása
print('---- Kiíratás turple (nyers, csak tartalom) ----\n')
i = cur.fetchone()
while(i):
print(i)
i = cur.fetchone()
print()
print('------------------ Második ---------------------')
print()
cur.execute(""" SELECT TrackId, Name, Composer, AlbumId, Bytes FROM Track WHERE
GenreId = :GenreId --- sql : nevesített paraméter deffinicio
and Composer > :Composer """,
{"GenreId": "24", "Composer": "T"}) ## paraméter szótár érték direct megadása
print(cur.fetchmany(2)) ## két sor kiírása
print()
print('------------------ Harmadik ---------------------')
print()
rows = cur.fetchall()
for row in rows:
print("ID = {} ".format(row[0]))
print("NEV = {} ".format(row[1]))
print("SZERZO = {}".format(row[2]))
print("BYTE = {}".format(row[4]))
print('-------------------------------')
## lezárások, erőforrás felszabadítás
cur.close()
con.close()
except sqlite3.Error as e: # hiba elkapása
print ("Hiba történt : ", e.args[0]) # hiba üzenet
---- Kiíratás turple (nyers, csak tartalom) ---- (3375, 'No Such Thing', 'Chris Cornell', 270, 3691272) (3376, 'Poison Eye', 'Chris Cornell', 270, 3890037) (3377, 'Arms Around Your Love', 'Chris Cornell', 270, 3516224) (3378, 'Safe and Sound', 'Chris Cornell', 270, 4207769) (3379, "She'll Never Be Your Man", 'Chris Cornell', 270, 3355715) (3380, 'Ghosts', 'Chris Cornell', 270, 3799745) (3381, 'Killing Birds', 'Chris Cornell', 270, 3588776) (3382, 'Billie Jean', 'Michael Jackson', 270, 4606408) (3383, 'Scar On the Sky', 'Chris Cornell', 270, 3616618) (3384, 'Your Soul Today', 'Chris Cornell', 270, 3385722) (3385, 'Finally Forever', 'Chris Cornell', 270, 3565098) (3386, 'Silence the Voices', 'Chris Cornell', 270, 4379597) (3387, 'Disappearing Act', 'Chris Cornell', 270, 4476203) (3388, 'You Know My Name', 'Chris Cornell', 270, 3940651) ------------------ Második --------------------- [(3412, '"Eine Kleine Nachtmusik" Serenade In G, K. 525: I. Allegro', 'Wolfgang Amadeus Mozart', 281, 5760129), (3413, 'Concerto for Clarinet in A Major, K. 622: II. Adagio', 'Wolfgang Amadeus Mozart', 282, 6474980)] ------------------ Harmadik --------------------- ID = 3448 NEV = Lamentations of Jeremiah, First Set \ Incipit Lamentatio SZERZO = Thomas Tallis BYTE = 1208080 ------------------------------- ID = 3454 NEV = Symphony No. 41 in C Major, K. 551, "Jupiter": IV. Molto allegro SZERZO = Wolfgang Amadeus Mozart BYTE = 6173269 ------------------------------- ID = 3492 NEV = Sing Joyfully SZERZO = William Byrd BYTE = 2256484 ------------------------------- ID = 3502 NEV = Quintet for Horn, Violin, 2 Violas, and Cello in E Flat Major, K. 407/386c: III. Allegro SZERZO = Wolfgang Amadeus Mozart BYTE = 3665114 -------------------------------
import xlsxwriter
import sqlite3
# Új excel file létrehozása
workbook = xlsxwriter.Workbook(r"c:\Users\User\Downloads\Chinook_file.xlsx")
# Új munkalap létrehozása
worksheet = workbook.add_worksheet()
## Kezdő pozició
row_ = 0
col_ = 0
DB_FILE = r"c:\Users\User\Downloads\Chinook_Sqlite.sqlite" # adatbázis neve
con = sqlite3.connect(DB_FILE) ## kapcsolódás
cur = con.cursor() ## kurzor létrehozás
FIELD_MAX_WIDTH = 30 ## mezők maximális hossz beállítása
cur.execute(""" SELECT t.TrackId, t.Name, t.Composer, g.name as Style
FROM Track t INNER JOIN Genre g ON t.GenreId= g.GenreId --- SQL megjegyzés: Join szabály
WHERE t.GenreId=23 and t.Composer is not null --- SQL megjegyzés: Where feltétel """)
# Fejléc
for fieldDesc in cur.description:
print(fieldDesc[0].ljust(FIELD_MAX_WIDTH), end=' ')
## Excelbe fejléc írása
worksheet.write(row_, col_, fieldDesc[0])
col_ += 1
print() # Fejléc vége
for fieldDesc in cur.description:
print('-' * 30, end=' ') # Alávonás
print() # elválasztó sor
# Adattőrzs
fieldIndices = range(len(cur.description))
for row in cur:
row_ += 1
col_ = 0
for fieldIndex in fieldIndices:
fieldValue = str(row[fieldIndex])
print(fieldValue.ljust(FIELD_MAX_WIDTH), end=' ') # Mezők egy sorba
## excelbe adatok írása
worksheet.write(row_, col_, row[fieldIndex])
col_ += 1
print() # kiíratás végén sorvégjel kitétele
## lezárások, erőforrás felszabadítás
cur.close()
con.close()
workbook.close()
## Hiányzik: kivétel kezelés, ...
TrackId Name Composer Style ------------------------------ ------------------------------ ------------------------------ ------------------------------ 3375 No Such Thing Chris Cornell Alternative 3376 Poison Eye Chris Cornell Alternative 3377 Arms Around Your Love Chris Cornell Alternative 3378 Safe and Sound Chris Cornell Alternative 3379 She'll Never Be Your Man Chris Cornell Alternative 3380 Ghosts Chris Cornell Alternative 3381 Killing Birds Chris Cornell Alternative 3382 Billie Jean Michael Jackson Alternative 3383 Scar On the Sky Chris Cornell Alternative 3384 Your Soul Today Chris Cornell Alternative 3385 Finally Forever Chris Cornell Alternative 3386 Silence the Voices Chris Cornell Alternative 3387 Disappearing Act Chris Cornell Alternative 3388 You Know My Name Chris Cornell Alternative