Download Python sqlite3

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Commitment ordering wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Microsoft Access wikipedia , lookup

IMDb wikipedia , lookup

Serializability wikipedia , lookup

Oracle Database wikipedia , lookup

Functional Database Model wikipedia , lookup

Extensible Storage Engine wikipedia , lookup

Ingres (database) wikipedia , lookup

Database wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Concurrency control wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Open Database Connectivity wikipedia , lookup

SQL wikipedia , lookup

Versant Object Database wikipedia , lookup

Database model wikipedia , lookup

Clusterpoint wikipedia , lookup

ContactPoint wikipedia , lookup

Relational model wikipedia , lookup

PL/SQL wikipedia , lookup

Transcript
Python sqlite3
sqlite3.connect(database [,timeout ,other optional arguments])
This API opens a connection to the SQLite database file database. You can use ":memory:" to open a
database connection to a database that resides in RAM instead of on disk. If database is opened successfully,
it returns a connection object.
import sqlite3
conn = sqlite3.connect('LOVA.db')
connection.execute(sql [, optional parameters])
This routine creates an intermediate cursor object by calling the cursor method, then calls the cursor's
execute method with the parameters given.
conn.execute('''CREATE TABLE TVRTKA
(ID
INT PRIMARY KEY
NOT NULL,
PREZIME
TEXT
NOT NULL,
DOB
INT
NOT NULL,
ADRESA
CHAR(50),
PRIHOD
REAL
);''')
conn.execute("INSERT INTO TVRTKA ID,PREZIME,DOB,ADRESA,PRIHOD)\
VALUES (1, 'Pajo', 32, 'Istra', 120000.00 )");
cursor = conn.execute("SELECT ID,PREZIME,DOB,ADRESA,PRIHOD from TVRTKA")
for row in cursor:
print "ID = ", row[0]
print "PREZIME = ", row[1]
print "DOB = ",
row[2]
print "ADRESA = ", row[3]
print "PRIHOD = ", row[4], "\n"
connection.commit()
This method commits the current transaction. If you don't call this method, anything you did since the last
call to commit() is not visible from other database connections.
connection.close()
This method closes the database connection. Note that this does not automatically call commit(). If you just
close your database connection without calling commit() first, your changes will be lost!
conn.commit()
conn.close()
CURSOR
connection.cursor([cursorClass])
This routine creates a cursor which will be used throughout of your database programming with Python.
This method accepts a single optional parameter cursorClass.
conn = sqlite3.connect('example.db')
c = conn.cursor()
cursor.execute(sql [, optional parameters])
This routine executes an SQL statement. The SQL statement may be parameterized (i. e. placeholders
instead of SQL literals). The sqlite3 module supports two kinds of placeholders: question marks and
named placeholders (named style).
For example:cursor.execute("insert into people values (?, ?)", (who, age))
cursor.fetchone()
This method fetches the next row of a query result set, returning a single sequence, or None when no more
data is available
t = ('ML1742',)
c.execute('SELECT * FROM skladiste WHERE symbol=?', t)
print c.fetchone()
cursor.fetchmany([size=cursor.arraysize])
This routine fetches the next set of rows of a query result, returning a list. An empty list is returned when no
more rows are available. The method tries to fetch as many rows as indicated by the size parameter.
cursor.fetchall()
This routine fetches all (remaining) rows of a query result, returning a list. An empty list is returned when no
rows are available.
cursor.executemany(sql, seq_of_parameters)
This routine executes an SQL command against all parameter sequences or mappings found in the sequence
sql.
# Larger example that inserts many records at a time
purchases = [('2006-03-28', 'BUY', 'IBM', 1000, 45.00),
('2006-04-05', 'BUY', 'MSFT', 1000, 72.00),
('2006-04-06', 'SELL', 'IBM', 500, 53.00),
]
c.executemany('INSERT INTO stocks VALUES (?,?,?,?,?)', purchases)
for row in c.execute('SELECT * FROM stocks ORDER BY price'):
print row
connection.rollback()
This method rolls back any changes to the database since the last call to commit().
conn.execute("UPDATE COMPANY set SALARY = 25000.00 where ID=1")
conn.commit
conn.execute("DELETE from COMPANY where ID=2;")
conn.commit
connection.total_changes()
This routine returns the total number of database rows that have been modified, inserted, or deleted since the
database connection was opened.
conn.execute("DELETE from COMPANY where ID=2;")
conn.commit
print "Total number of rows deleted :", conn.total_changes