The following is another basic example that illustrates the use of a SQLite driver for Java. Python already enjoys of a package for SQLite called pysqlite but unfortunately is not callable from Jython. I am attaching the SQLite database alpha.db that is part of the example.


Add to classpath or /jre/lib/ext:

Sample Code: from java.sql import DriverManager from java.sql import SQLException from java.lang import Class import sys class SQLiteDB: def __init__(self): self.database = "alpha.db" driver = "org.sqlite.JDBC" self.url = "jdbc:sqlite:" + self.database Class.forName(driver); def open(self): try: self.db = DriverManager.getConnection(self.url); stat1 = self.db.createStatement() print self.db except SQLException, e: print e.message print "cannot create connection with current driver" print "revise url and try again. Will exit application ..." sys.exit() def view(self): try: stmt = self.db.createStatement(); rs = stmt.executeQuery("SELECT * FROM letters ORDER BY ID"); while ( id = rs.getString("ID"); # Column 1 letter = rs.getString("letter"); # Column 2 name = rs.getString("name"); # Column 3 cat = rs.getString("categoryID"); # Column 4 print "ID:", id, "Code:", letter, "Name:", name, "\tCategory:", cat except SQLException, e: print e.message print "cannot open table" def close(self): self.db.close(); if __name__ == '__main__': app = SQLiteDB() app.view() app.close() alpha.db

Alfonso Reyes

View this message in context: SQLite + JDBC + Jython. Example
Sent from the jython-users mailing list archive at