sqlite and groovy walkthrough
daniel Tue, 06/29/2010 - 8:58am
Prereqs: sqlite3, groovy installed
note about sqlite and ubuntu:
There are at least 2 versions of sqlite in the repo. The sqlite package will not be able to read the db below. You'll need to install the sqlite3 package instead. You'll get a message about the db being encrypted or unreadable if you use just sqlite$ sudo apt-get install sqlite3
If you are using gradle to manage your dependencies, then add this dependency to your build.gradle:
compile group: 'org.xerial', name: 'sqlite-jdbc', version: '3.6.16'
An example groovy class using sqlite:class PersistStatistic {
String dbLocation = "jdbc:sqlite:/pathToTestdb/testdb.db"
String dbDriver = "org.sqlite.JDBC"
def getDb(){
return groovy.sql.Sql.newInstance(dbLocation, dbDriver)
}
void insert(String url){
def sql = getDb()
sql.execute("insert into myTable(colname1, colname2) values(?, ?)", [url, new Date()])
}
void printContents(){
def sql = getDb()
sql.rows("select * from myTable order by date desc").each{
println(it)
}
}
}
To view the database:
use the printContents method above (change the sql statement appropriately), or
run sqlite at the command line:
* sqlite3 test.db
* run your sql statements to query the database
* .exit to exit sqlite3
Some links of use:
Sqlite home
Sqlite commands
Xerial sqlite jdbc driver
A nice groovy with sqlite walkthrough
- Log in to post comments