Open main menu

CDOT Wiki β

Changes

Teams Winter 2011/team1/BlackBerry/Use SQLite

2,956 bytes added, 23:10, 11 April 2011
11.6 Create SQLiteManager Class
'''11.6.4 Delete Student'''<br/>
Then add a method to delete student by id:
 
<source lang="java">
// DELETE STUDENT void deleteStudent(int id) { try { Statement statement = db.createStatement("DELETE FROM Student WHERE id = ?"); statement.prepare(); statement.bind(1, id); statement.execute(); statement.close(); } catch(DatabaseException dbe) { errorDialog(dbe.toString()); } }
</source>
'''11.6.5 Update Student'''<br/>
And a method to update Student:
<source lang="java">
//UPDATE STUDENT
void updateStudent(int id, String fName, String lName, String email, String address)
{
try
{
Statement statement = db.createStatement("UPDATE Student SET firstName = ?, lastName= ?, email = ?, address = ? WHERE id = ?");
statement.prepare();
statement.bind(1, fName);
statement.bind(2, lName);
statement.bind(3, email);
statement.bind(4, address);
statement.bind(5, id);
statement.execute();
statement.close();
}
catch(DatabaseException dbe)
{
errorDialog(dbe.toString());
}
}
</source>
'''11.6.6 Select All Students'''<br/>
gives us And obviously we will need a method that gets all the students from database and returns a vector of type Student, so that we can populate our student list of all Students:
<source lang="java">
Vector getStudents(){ Vector students= new Vector(); try { Statement statement = db.createStatement("SELECT * FROM Student"); statement.prepare(); Cursor cursor = statement.getCursor(); // Iterate through the the result set. For each row, add a // new DirectoryItem object to the vector. while(cursor.next()) { Row row = cursor.getRow(); int id = row.getInteger(0); String fName = row.getString(1); String lName = row.getString(2); String email = row.getString(3); String address = row.getString(4); Student student = new Student(id, fName, lName, email, address); students.addElement(student); } statement.close(); cursor.close(); } catch(DatabaseException dbe) { errorDialog(dbe.toString()); } catch(DataTypeException dte) { errorDialog(dte.toString()); } return students; }
</source>
1
edit