PHarkinian
Lurker
I'm developing an Android application and I would like to keep my SQLite query statements outside of my Java classes.
I considered using a .properties file, in which to store all of my SQL statements. Sounds fine, each property in a .properties file holds a string - I can even store prepared statements and give them the needed parameters, for example:
One thing I thought of was creating a class which can't be instantiated in which to "construct" the queries I need. Something along the lines of...
This is still a Java class, but it'll be good to know that all of my SQL is there and not scattered around all over the place.
Is this a good idea? Are there any other alternatives? Thanks in advance, guys!
I considered using a .properties file, in which to store all of my SQL statements. Sounds fine, each property in a .properties file holds a string - I can even store prepared statements and give them the needed parameters, for example:
get.student.with.first.name=SELECT * FROM Students WHERE FirstName = ?;
However, I have also implemented Persistence Contracts for my database tables like so:
public final class StudentPersistenceContract {
private StudentPersistenceContract() {}
public static abstract class StudentEntry implements BaseColumns {
public static final String TABLE_NAME = "Student";
public static final String COLUMN_FIRST_NAME = "FirstName";
public static final String COLUMN_LAST_NAME = "LastName";
}
}
I wouldn't want to hardcode the table and column names into the entries of the .properties file, I would like to access them dynamically like so: StudentPersistenceContract.StudentEntry.TABLE_NAME, etc.private StudentPersistenceContract() {}
public static abstract class StudentEntry implements BaseColumns {
public static final String TABLE_NAME = "Student";
public static final String COLUMN_FIRST_NAME = "FirstName";
public static final String COLUMN_LAST_NAME = "LastName";
}
}
One thing I thought of was creating a class which can't be instantiated in which to "construct" the queries I need. Something along the lines of...
public final class SqlQueryConstructor {
private SqlQueryConstructor() {}
public static final String GET_STUDENT_WITH_FIRST_NAME = "SELECT * FROM " + StudentPersistenceContract.StudentEntry.TABLE_NAME + " WHERE " + StudentPersistenceContract.StudentEntry.COLUMN_FIRST_NAME + " = ?;";
}
This way I can get the desired SQL query by accessing: SqlQueryConstructor.GET_STUDENT_WITH_FIRST_NAMEprivate SqlQueryConstructor() {}
public static final String GET_STUDENT_WITH_FIRST_NAME = "SELECT * FROM " + StudentPersistenceContract.StudentEntry.TABLE_NAME + " WHERE " + StudentPersistenceContract.StudentEntry.COLUMN_FIRST_NAME + " = ?;";
}
This is still a Java class, but it'll be good to know that all of my SQL is there and not scattered around all over the place.
Is this a good idea? Are there any other alternatives? Thanks in advance, guys!