sqlite3 Python (Geliştiriciler İçin Nasıl Çalışir)
sqlite3 modülü, Python'da SQLite veritabanları ile etkileşim kurmanın bir yolunu sunar. Python Standart Kutuphanesi'nin bir parçasıdır, bu nedenle kullanmak için ekstra bir şey yüklemenize gerek yoktur. Özelliklerini keşfedelim ve bazı kod örneklerini görelim. Bu makalede daha sonra Iron Software tarafından geliştirilen bir PDF oluşturma kutuphanesi olan IronPDF'i inceleyeceğiz.
SQLite, ayrı bir veritabanı sunucusu işlemi gerektirmeyen hafif, disk tabanlı bir veritabanıdır. sqlite3 modülü, mevcut bir veritabanı veya yeni oluşturulmuş bir veritabanı ile sorunsuz bir şekilde etkileşim kurmak için bir SQL arayüz uyumlu ortam sağlar. Modül, PEP 249 tarafından tanımlanan DB-API 2.0 spesifikasyonunu uygular.
Temel Kullanımı
İşte size sqlite3 ile başlamanız için basit bir örnek ve birden fazla SQL ifadesi.
Bir Veritabanına Bağlanma
İlk olarak, SQLite veritabanınıza bağlanmanız gerekecek. Veritabanı dosyası eksikse, oluşturulacaktır:
import sqlite3
# Connect to the database (or create it if it doesn't exist)
conn = sqlite3.connect('example.db')
# Create a cursor object
cur = conn.cursor()import sqlite3
# Connect to the database (or create it if it doesn't exist)
conn = sqlite3.connect('example.db')
# Create a cursor object
cur = conn.cursor()Bir Tablo Oluşturma
Yeni bir veri tablosu oluşturmak için CREATE TABLE SQL ifadesini kullanın:
# Create a table using SQL statements
cur.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER
)
''')# Create a table using SQL statements
cur.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER
)
''')Veri Ekleme
İşte bir veritabanı tablosuna nasıl veri ekleyeceğiniz:
# Insert data into the table
cur.execute('''
INSERT INTO users (name, age) VALUES (?, ?)
''', ('Alice', 30))
# Commit the transaction using the connection object
conn.commit()# Insert data into the table
cur.execute('''
INSERT INTO users (name, age) VALUES (?, ?)
''', ('Alice', 30))
# Commit the transaction using the connection object
conn.commit()Veri Sorgulama
SQL komutları çalıştırabilir ve veritabanı tablosundan sonuçlar alabilirsiniz:
# Query the database
cur.execute('SELECT * FROM users')
# Fetch all results
rows = cur.fetchall()
# Print the results
for row in rows:
print(row)# Query the database
cur.execute('SELECT * FROM users')
# Fetch all results
rows = cur.fetchall()
# Print the results
for row in rows:
print(row)Veri Güncelleme
Tablodaki mevcut verileri güncellemek için:
# Update data in the table
cur.execute('''
UPDATE users SET age = ? WHERE name = ?
''', (31, 'Alice'))
# Commit the transaction
conn.commit()# Update data in the table
cur.execute('''
UPDATE users SET age = ? WHERE name = ?
''', (31, 'Alice'))
# Commit the transaction
conn.commit()Veri Silme
Adı Alice olan veritabanı satırlarından veri silmek için:
# Delete data from the table
cur.execute('''
DELETE FROM users WHERE name = ?
''', ('Alice',))
# Commit the transaction
conn.commit()# Delete data from the table
cur.execute('''
DELETE FROM users WHERE name = ?
''', ('Alice',))
# Commit the transaction
conn.commit()Bağlantıyı Kapatma
İşiniz bittiğinde hem imleci hem de bağlantıyı kapatmayı unutmayın:
# Close the cursor and connection
cur.close()
conn.close()# Close the cursor and connection
cur.close()
conn.close()Gelişmiş Özellikler
Bağlam Yöneticileri Kullanma
Bağlantıyı otomatik olarak kapatma işlemini ele almak için bağlam yöneticilerini kullanabilirsiniz:
with sqlite3.connect('example.db') as conn:
cur = conn.cursor()
cur.execute('SELECT * FROM users')
rows = cur.fetchall()
for row in rows:
print(row)with sqlite3.connect('example.db') as conn:
cur = conn.cursor()
cur.execute('SELECT * FROM users')
rows = cur.fetchall()
for row in rows:
print(row)İşlemleri Yönetme
SQLite, işlemleri destekler ve bunları yönetmek için BEGIN, COMMIT ve ROLLBACK kullanabilirsiniz:
try:
conn.execute('BEGIN')
cur.execute('INSERT INTO users (name, age) VALUES (?, ?)', ('Bob', 25))
conn.commit()
except sqlite3.Error as e:
conn.rollback()
print(f"An error occurred: {e}")try:
conn.execute('BEGIN')
cur.execute('INSERT INTO users (name, age) VALUES (?, ?)', ('Bob', 25))
conn.commit()
except sqlite3.Error as e:
conn.rollback()
print(f"An error occurred: {e}")IronPDF'i Tanıtma

IronPDF, HTML, CSS, görüntüler ve JavaScript kullanarak PDF oluşturmak, düzenlemek ve imzalamak için tasarlanmış güçlü bir Python kütüphanesidir. Düşük bellek kullanimiyla ticari seviyede performans sunar. Ana özellikler şunlardır:
HTML'den PDF'ye Dönüşüm:
HTML dosyalarını, HTML metinlerini ve URL'leri PDF'ye dönüştürün. Örneğin, bir web sayfasını Chrome PDF cizime kullanarak PDF olarak render edin.
Platformlar Arası Destek:
.NET Core, .NET Standard ve .NET Framework dahil çeşitli .NET platformlariyla uyumludur. Windows, Linux ve macOS'u destekler.
Düzenleme ve İmzalama:
Özellikleri ayarlayin, sifreler ve izinlerle guvenlik ekleyin ve PDF'lerinize dijital imzalar uygulayın.
Sayfa Şablonları ve Ayarları:
Basliklar, alt basliklar, sayfa numaralari ve ayarlanabilir kenar bosluklari ile PDF'leri ozellestirin. Duyarlı yerleşimleri ve özel kağıt boyutlarını destekler.
Standart Uyum:
PDF/A ve PDF/UA gibi PDF standartlarına uyar, UTF-8 karakter kodlamasını destekler ve resimler, CSS ve yazı tipleri gibi varlıkları yönetir.
Python üzerinde IronPDF ve SQLite3 Kullanarak PDF Belgeleri Oluşturma
import sqlite3
from ironpdf import *
# Apply your license key
License.LicenseKey = "key"
# Connect to the sqlite database file (or create it if it doesn't exist)
conn = sqlite3.connect('example.db')
# Create a cursor object for database connection
cur = conn.cursor()
# Create a table SQL command
cur.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER
)
''')
# Insert data into the table
cur.execute('''
INSERT INTO users (name, age) VALUES (?, ?)
''', ('IronUser1', 30))
cur.execute('''
INSERT INTO users (name, age) VALUES (?, ?)
''', ('IronUser2', 31))
cur.execute('''
INSERT INTO users (name, age) VALUES (?, ?)
''', ('IronUser3', 25))
cur.execute('''
INSERT INTO users (name, age) VALUES (?, ?)
''', ('IronUser4', 28))
# Commit the transaction using the connection object
conn.commit()
# Query the database
cur.execute('SELECT * FROM users')
# Fetch all results
rows = cur.fetchall()
# Print the results
for row in rows:
print(row)
# Update data in the table
cur.execute('''
UPDATE users SET age = ? WHERE name = ?
''', (31, 'Alice'))
# Commit the transaction
conn.commit()
# Delete data from the table
cur.execute('''
DELETE FROM users WHERE name = ?
''', ('IronUser1',))
# Commit the transaction
conn.commit()
# Initialize PDF renderer
renderer = ChromePdfRenderer()
# Create a PDF from an HTML string using Python
content = "<h1>Awesome Iron PDF with Sqlite3</h1>"
content += "<p>table data</p>"
for row in rows:
content += "<p>" + str(row) + "</p>"
# Close the cursor and connection
cur.close()
conn.close()
# Render the HTML as a PDF
pdf = renderer.RenderHtmlAsPdf(content)
# Export to a file
pdf.SaveAs("DemoSqlite3.pdf")import sqlite3
from ironpdf import *
# Apply your license key
License.LicenseKey = "key"
# Connect to the sqlite database file (or create it if it doesn't exist)
conn = sqlite3.connect('example.db')
# Create a cursor object for database connection
cur = conn.cursor()
# Create a table SQL command
cur.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER
)
''')
# Insert data into the table
cur.execute('''
INSERT INTO users (name, age) VALUES (?, ?)
''', ('IronUser1', 30))
cur.execute('''
INSERT INTO users (name, age) VALUES (?, ?)
''', ('IronUser2', 31))
cur.execute('''
INSERT INTO users (name, age) VALUES (?, ?)
''', ('IronUser3', 25))
cur.execute('''
INSERT INTO users (name, age) VALUES (?, ?)
''', ('IronUser4', 28))
# Commit the transaction using the connection object
conn.commit()
# Query the database
cur.execute('SELECT * FROM users')
# Fetch all results
rows = cur.fetchall()
# Print the results
for row in rows:
print(row)
# Update data in the table
cur.execute('''
UPDATE users SET age = ? WHERE name = ?
''', (31, 'Alice'))
# Commit the transaction
conn.commit()
# Delete data from the table
cur.execute('''
DELETE FROM users WHERE name = ?
''', ('IronUser1',))
# Commit the transaction
conn.commit()
# Initialize PDF renderer
renderer = ChromePdfRenderer()
# Create a PDF from an HTML string using Python
content = "<h1>Awesome Iron PDF with Sqlite3</h1>"
content += "<p>table data</p>"
for row in rows:
content += "<p>" + str(row) + "</p>"
# Close the cursor and connection
cur.close()
conn.close()
# Render the HTML as a PDF
pdf = renderer.RenderHtmlAsPdf(content)
# Export to a file
pdf.SaveAs("DemoSqlite3.pdf")Kod Açıklaması
Bu Python programı, bir veritabanı oluşturmak, veri eklemek, sorgular yapmak, kayıtları güncellemek, kayıtları silmek ve sonunda IronPDF kullanarak bir PDF belgesi oluşturmak için SQLite kutuphanesinin nasıl kullanılacağını gösterir.
Kütüphanelerin İthal Edilmesi:
sqlite3: SQLite veri tabanlari ile calismak icin Python'un yerlesik modulu.ironpdf: PDF oluşturma izni veren IronPDF'den bileşenler ithal etmek.
Veritabanına Bağlanma:
example.dbadiyla bir SQLite veri tabanina baglanti kurar.
Bir Tablo Oluşturma:
usersadiyla bir SQLite tablosu kolanlarla birlikteid(INTEGER, PRIMARY KEY),name(TEXT, NOT NULL) veage(INTEGER) tanimlar.
Veri Ekleme:
userstablosuna coklu veri satiri ekler.
İşlemleri Onaylama:
- Yaptığı değişiklikleri kalıcı hale getirmek için Veritabanına kaydeder.
Veritabanını Sorgulama:
userstablosundan tüm satirlari elde etmek icin bir SELECT ifadesi calistirir.
Veri Güncelleme:
- 'Alice' adli kullanicinin
agebilgisini günceller.
- 'Alice' adli kullanicinin
Veri Silme:
- 'IronUser1' adli kullaniciyi
userstablosundan siler.
- 'IronUser1' adli kullaniciyi
PDF Oluşturma:
- IronPDF (
ChromePdfRenderer) kullanarak HTML iceriginden bir PDF dokümani oluşturur. - HTML içeriğinde başlık ve tablo verilerini (veritabanından alınan) birleştirir.
- PDF dokümanini
DemoSqlite3.pdfolarak kaydeder.
- IronPDF (
- Bağlantıları Kapatma:
- Tuşlayici (
cur) ve baglantiyi (conn) kapanir, kaynaklari serbest birakir.
- Tuşlayici (
Bu betik, Python'un SQLite3 ve IronPDF kutuphanelerini kullanarak veritabanı kurulumu, veriyi manipüle etme ve PDF oluşturma ile ilgili eksiksiz bir iş akışı gösterir.
Çıktı


IronPDF Lisansı
IronPDF bir lisans anahtarı üzerinde çalışır. IronPDF for Python, kullanıcıların kapsamlı özelliklerini satın almadan önce test etmelerine olanak tanıyan ücretsiz bir deneme lisans anahtarı sunar.
Lisans Anahtarını buraya yerleştirin:
import {IronPdfGlobalConfig, PdfDocument} from "@ironsoftware/ironpdf";
// Apply your IronPDF license key
IronPdfGlobalConfig.getConfig().licenseKey = "Add Your key here";import {IronPdfGlobalConfig, PdfDocument} from "@ironsoftware/ironpdf";
// Apply your IronPDF license key
IronPdfGlobalConfig.getConfig().licenseKey = "Add Your key here";Sonuç

sqlite3 modülü, Python'da SQLite veritabanlarıyla çalışmak için güçlü ve kullanımı kolay bir araçtır. Python Standart Kutuphanesi'ne entegrasyonu, basit ve karmaşık veritabanı işlemlerini uygun hale getirir. IronPDF bir deneme lisansı sunar. Daha sonra, lisans $799'da başlar ve yukari dogru cikar.










