SQLIte
Storage Description
Class
NULL It specifies that the value is a null value.
INTEGER It specifies the value is a signed integer, stored in 1, 2, 3,
4, 6, or 8 bytes depending on the magnitude of the value.
REAL It specifies the value is a floating point value, stored as an
8-byte IEEE floating point number.
text It specifies the value is a text string, stored using the
database encoding (utf-8, utf-16be or utf-16le)
BLOB It specifies the value is a blob of data, stored exactly as it
was input.
SQLite Create Database
In SQLite, the sqlite3 command is used to create a new database.
Syntax:
1. sqlite3 [Link]
SQLite Create Table
In SQLite, CREATE TABLE statement is used to create a new table. While
creating the table, we name that table and define its column and data
types of each column.
Syntax:
1. CREATE TABLE database_name.table_name(
2. column1 datatype PRIMARY KEY(one or more columns),
3. column2 datatype,
4. column3 datatype,
5. .....
6. columnN datatype,
7. );
o Add the downloaded jar file to your class path.
o You can now connect to the SQLite database using java.
import [Link];
import [Link];
import [Link];
public class Connect {
/**
* Connect to a sample database
*/
public static void connect() {
Connection conn = null;
try {
// db parameters
String url = "jdbc:sqlit[Link]/venkat/[Link]";
// create a connection to the database
conn = [Link](url);
[Link]("Connection to SQLite has been
established.");
} catch (SQLException e) {
[Link]([Link]());
} finally {
try {
if (conn != null) {
[Link]();
}
} catch (SQLException ex) {
[Link]([Link]());
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
connect();
}
}
Select records
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class SelectRecords {
private Connection connect() {
// SQLite connection string
String url = " jdbc:sqlit[Link]/venkat/[Link] ";
Connection conn = null;
try {
conn = [Link](url);
} catch (SQLException e) {
[Link]([Link]());
}
return conn;
}
public void selectAll(){
String sql = "SELECT * FROM employees";
try {
Connection conn = [Link]();
Statement stmt = [Link]();
ResultSet rs = [Link](sql);
// loop through the result set
while ([Link]()) {
[Link]([Link]("id") + "\t" +
[Link]("name") + "\t" +
[Link]("capacity"));
}
} catch (SQLException e) {
[Link]([Link]());
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SelectRecords app = new SelectRecords();
[Link]();
}
Insert Record in the table
After the creation of the table, use the following code to insert some
records in the table. Create a new class "InsertRecords", having the
following code:
import [Link];
import [Link];
import [Link];
import [Link];
public class InsertRecords {
private Connection connect() {
// SQLite connection string
String url = " jdbc:sqlit[Link]/venkat/[Link] ";
Connection conn = null;
try {
conn = [Link](url);
} catch (SQLException e) {
[Link]([Link]());
}
return conn;
}
public void insert(String name, double capacity) {
String sql = "INSERT INTO student(name, capacity)
VALUES(?,?)";
try{
Connection conn = [Link]();
PreparedStatement pstmt = [Link](sql);
[Link](1, name);
[Link](2, capacity);
[Link]();
} catch (SQLException e) {
[Link]([Link]());
}
}
public static void main(String[] args) {
InsertRecords app = new InsertRecords();
// insert three new rows
[Link](24999, "Aryan", 20, “West street”, 2000.00);
[Link](24998, "Bharathi", 20, “East street”, 2000.00);
[Link](24997, "Christina", 20, “North street”, 2000.00);
}