Tuesday 19 October 2010

How to start with SQLite?

How to start with SQLite?

SQLite is very easy to use, simple and very light. The database is saved in an only file.
SQLite works with MySQL syntax.

The easiest mode to create a database is using a client for SQLite. My recommendation is a plugin fo firefox called SQLite Manager.
You can get it here.

If you want connect your SQLite databse with Java, you will need the SQLite Connector:
You can get it here.
Below I explain the first steps to use it.






Initialize:


try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:"+DBpath);
}
catch(Exception e){
e.printStackTrace();
}

Execute query:

String select = "SELECT * FROM CONTENT WHERE ID=?";
PreparedStatement ps = conn.prepareStatement(select);
ps.setString(1, id);
ResultSet rs = ps.executeQuery();
rs.next();
if(rs.getString("CONTENT")!=null){
String path = rs.getString("CONTENT");
}
ps.close();
rs.close();

Execute Update:

ResultSet rs = null;
PreparedStatement ps = null;

ps = conn.prepareStatement("UPDATE CONTENT SET SIZE=?, TYPE=?, TIME=? WHERE ID=?");
ps.setInt(1, Integer.valueOf(size));
ps.setString(2, type);
ps.setDate(3, date);
ps.setString(4, id);
ps.executeUpdate();

ps.close();

Thanks for read me!

0 comments: