This page documents usage of my db library
Steps to create a new usage
1. Create a new class derived from metcarob.com.common.io.db.DBFile
Example:
- 
public class ServerDataFile extends DBFile {
 - 
public ServerDataFile(String string, boolean b) throws IOException, ClassNotFoundException, SQLException {
 - 
super(string, b);
 - 
}
 - 
 - 
@Override
 - 
"id integer not null PRIMARY KEY," +
 - 
"name TEXT not null unique," +
 - 
"path TEXT not null," +
 - 
"subdirs integer not null DEFAULT 1," +
 - 
"created DATETIME not null DEFAULT CURRENT_TIMESTAMP," +
 - 
"creation_note TEXT," +
 - 
"lastplayed DATETIME" +
 - 
")";
 - 
this.executeUpdate(query);
 - 
 - 
query = "create table pathReplacements (" +
 - 
"id integer not null PRIMARY KEY," +
 - 
"name TEXT not null unique," +
 - 
"replacewith TEXT not null" +
 - 
")";
 - 
this.executeUpdate(query);
 - 
}
 - 
 - 
@Override
 - 
protected int getCodeDBFileVersion() {
 - 
return 2;
 - 
}
 - 
 - 
 - 
@Override
 - 
 - 
 - 
if (p_fromVer==1) {
 - 
//Add picPlayLists fields created, creation_note, lastplayed
 - 
int r = this.executeUpdate(query);
 - 
query = "ALTER TABLE picPlayLists ADD COLUMN creation_note TEXT";
 - 
this.executeUpdate(query);
 - 
query = "ALTER TABLE picPlayLists ADD COLUMN lastplayed DATETIME";
 - 
this.executeUpdate(query);
 - 
query = "update picPlayLists set creation_note='From Previous File Version'";
 - 
this.executeUpdate(query);
 - 
 - 
p_fromVer=2;
 - 
}
 - 
}
 - 
 - 
}
 
2. Add Dependincies
You must add org.sqlite.JDBC or org.sqldroid.SqldroidDriver to your project
3. Add code to open and close the file in your class
Example (m_DB is declared as the DBFile override class:
- 
{
 - 
if (f.exists()) {
 - 
m_DB = new ServerDataFile(p_argDBFile,false);
 - 
} else {
 - 
m_DB = new ServerDataFile(p_argDBFile,true);
 - 
}
 - 
};
 - 
 - 
if (null==m_DB) {
 - 
};
 
Some where later
- 
m_DB.close();
 
4. Create a saveable object
Create a class that extends DBFileSaveable:
- 
public class Node extends DBFileSaveable {
 - 
 - 
super(i, dbFile);
 - 
Load(i,dbFile);
 - 
}
 - 
 - 
public Node() {
 - 
super();
 - 
}
 - 
 - 
 - 
@Override
 - 
DBFileException {
 - 
int ret = p_File.executeUpdate(query);
 - 
}
 - 
 - 
@Override
 - 
protected void SaveDerivedINSERT(DBFile p_File, int p_id)
 - 
String query;
 - 
int ret;
 - 
query = "insert into pathReplacements (id,name,replacewith) VALUES (";
 - 
query += p_id + ",";
 - 
query += sqliteString(m_Name) + ",";
 - 
query += sqliteString(m_ReplaceWith);
 - 
query +=")";
 - 
try {
 - 
ret = p_File.executeUpdate(query);
 - 
throw new DBFileException("Error Inserting Record query was: (" + query + ") error was " + e.toString());
 - 
}
 - 
if (ret!=1) {
 - 
throw new DBFileException("Trying to save but updated " + ret + " rows (should be 1) - INSERT");
 - 
};
 - 
}
 - 
 - 
@Override
 - 
protected void SaveDerivedUPDATE(DBFile p_File, int p_id)
 - 
String query;
 - 
int ret;
 - 
query = "update pathReplacements set ";
 - 
query += "name=" + sqliteString(m_Name);
 - 
query += ",replacewith=" + sqliteString(m_ReplaceWith);
 - 
query += " where id=" + p_id;
 - 
ret = p_File.executeUpdate(query);
 - 
if (1!=ret) {
 - 
throw new DBFileException("Trying to save but updated " + ret + " rows (should be 1) - UPDATE");
 - 
};
 - 
}
 - 
 - 
@Override
 - 
DBFileException {
 - 
int c=0;
 - 
while (rs.next()) {
 - 
c++;
 - 
m_Name = rs.getString("name");
 - 
m_ReplaceWith = rs.getString("replacewith");
 - 
};
 - 
rs.close();
 - 
rs = null;
 - 
if (c!=1) {
 - 
throw new DBFileException("ERROR: could not find file id " + p_id + " (returned " + c + " records");
 - 
};
 - 
}
 - 
}
 
5. Create object
You can now simply create nodes:
- 
Node a = new Node();
 - 
a.Save(mainfile);
 
6. Manager Object
Example Manager Object:
- 
public class NodeManager {
 - 
private MainNodeFile m_mainFile = null;
 - 
 - 
 - 
public NodeManager(MainNodeFile p_mainFile) {
 - 
m_mainFile = p_mainFile;
 - 
}
 - 
 - 
if (p_node==null) return;
 - 
m_nameNodeMap.put(p_node.getM_name(), p_node);
 - 
m_IDNodeMap.put(p_node.getDb_id(), p_node);
 - 
 - 
}
 - 
 - 
Node ret = m_nameNodeMap.get(p_name);
 - 
if (ret!=null) return ret;
 - 
 - 
//Not already loaded so load from database
 - 
 - 
int id = -1;
 - 
int c=0;
 - 
while (rs.next()) {
 - 
c++;
 - 
id = rs.getInt("id");
 - 
};
 - 
rs.close();
 - 
rs = null;
 - 
if (c!=1) {
 - 
return null;
 - 
};
 - 
ret = new Node(id, m_mainFile);
 - 
if (ret!=null) {
 - 
addNode(ret);
 - 
}
 - 
return ret;
 - 
}
 - 
 - 
}
 
RJM Article Type
              Work Notes