खोज…


Sqlite3 - अलग सर्वर प्रक्रिया की आवश्यकता नहीं है।

Sqlite3 मॉड्यूल को गेरहार्ड हैरिंग ने लिखा था। मॉड्यूल का उपयोग करने के लिए, आपको पहले एक कनेक्शन ऑब्जेक्ट बनाना होगा जो डेटाबेस का प्रतिनिधित्व करता है। यहां डेटा example.db फ़ाइल में संग्रहीत किया जाएगा:

import sqlite3
conn = sqlite3.connect('example.db')

आप RAM में डेटाबेस बनाने के लिए विशेष नाम: मेमोरी: की भी आपूर्ति कर सकते हैं। एक बार आपके पास कनेक्शन होने पर, आप एक कर्सर ऑब्जेक्ट बना सकते हैं और एसक्यूएल कमांड्स को निष्पादित करने के लिए इसके निष्पादन () विधि को कॉल कर सकते हैं:

c = conn.cursor()

# Create table
c.execute('''CREATE TABLE stocks
         (date text, trans text, symbol text, qty real, price real)''')

# Insert a row of data
c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")

# Save (commit) the changes
conn.commit()

# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
conn.close()

डेटाबेस और एरर हैंडलिंग से मान प्राप्त करना

SQLite3 डेटाबेस से मान प्राप्त करना।

प्रिंट क्वेरी मान चुनिंदा क्वेरी द्वारा लौटाया गया

import sqlite3
conn = sqlite3.connect('example.db')
c = conn.cursor()
c.execute("SELECT * from table_name where id=cust_id")
for row in c:
    print row # will be a list

एकल मिलान लाने के लिए भ्रूण () विधि

print c.fetchone()

कई पंक्तियों के लिए fetchall () विधि का उपयोग करें

a=c.fetchall() #which is similar to list(cursor) method used previously
for row in a:
    print row

फ़ंक्शन में निर्मित sqlite3.Error का उपयोग करके त्रुटि से निपटने के लिए किया जा सकता है

try:
    #SQL Code
except sqlite3.Error as e:
    print "An error occurred:", e.args[0]


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow