From: Alfonso R. <ms...@re...> - 2007-10-21 01:01:59
|
Thanks for the heads-up. I will try to format the code in a different way. By the way, is there a recommended practice to insert code at this forum?. Thanks. Alfonso Reyes wrote: > > 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.<br><br> > > References:<br> > sqliteJDBC: http://www.zentus.com/sqlitejdbc/ > http://www.zentus.com/sqlitejdbc/ <br> > Tutorial: http://www.pysquared.com/files/Java/JavaSQLiteExample/ > http://www.pysquared.com/files/Java/JavaSQLiteExample/ > <br><br> > Add to classpath or /jre/lib/ext:<br> > sqlitejdbc-v037-nested.jar > <br><br> > > Sample Code: > <xmp> > 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 (rs.next()): > 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.open() > app.view() > app.close() > </xmp> http://www.nabble.com/file/p13322270/alpha.db alpha.db > <br><br> > Cheers! > <br> > Alfonso Reyes > -- View this message in context: http://www.nabble.com/SQLite-%2B-JDBC-%2B-Jython.-Example-tf4663619.html#a13324954 Sent from the jython-users mailing list archive at Nabble.com. |