Ben Chuanlong Du's Blog

It is never too late to learn.

Hands on the sqlite3-jdbc library in Scala

In [1]:
%classpath add mvn org.xerial sqlite-jdbc 3.27.2.1
In [2]:
import collection.JavaConverters._
Out[2]:
scala.collection.JavaConverters$@355c65bd
In [3]:
import java.sql.DriverManager

val connection = DriverManager.getConnection("jdbc:sqlite:sample.db")
Out[3]:
org.sqlite.jdbc4.JDBC4Connection@7fababf3
In [4]:
val statement = connection.createStatement()
Out[4]:
org.sqlite.jdbc4.JDBC4Statement@38fe3ed7
In [5]:
statement.setQueryTimeout(30)
In [6]:
statement.executeUpdate("drop table if exists person")
Out[6]:
0
In [7]:
statement.executeUpdate("create table person (id integer, name string)")
Out[7]:
0
In [8]:
statement.executeUpdate("insert into person values(1, 'leo')")
Out[8]:
1
In [9]:
statement.executeUpdate("insert into person values(2, 'yui')")
Out[9]:
1
In [15]:
val results = statement.executeQuery("select * from person")
Out[15]:
org.sqlite.jdbc4.JDBC4ResultSet@5ecf21c
In [16]:
while(results.next()){
    println("name = " + results.getString("name"))
    println("id = " + results.getInt("id"))
}
In [17]:
println(1)
In [ ]:

Comments