Reputation: 1483
I' m developing an Android app wich requires me to use a Database. After reading a while, i' ve decided to use an SQLite database, i started reading how do i should work with it, but every book/tutorial/etc... develop an Updatable database by allowing the user to add information to it. (like a diary or a running time record app style). I just need to create (i mean, like hardcoding) my database and query it. My database will not be updatable by user side. I' m pretty sure it should be an easy/dummy way to do it but i'm not finding it yet. So, if anyone can help me, it would be awesome. ;)
Thanks. Amet.
Upvotes: 1
Views: 2657
Reputation: 2020
Please check the following link: SQLite database tutorial with DAO and Content Provider.
You need only 2 classes:
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_COMMENTS = "comments";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_COMMENT = "comment";
private static final String DATABASE_NAME = "commments.db";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_COMMENTS + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_COMMENT
+ " text not null);";
// Database creation sql statement
private static final String TABLE_CREATE = "CREATE TABLE tablename....."
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
database.execSQL(TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// You don't need this. If you don't change the DATABASE_VERSION value then this method will not be called.
}
}
public class CommentsDataSource {
// Database fields
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_COMMENT };
public CommentsDataSource(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public List<Comment> getAllComments() {
List<Comment> comments = new ArrayList<Comment>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Comment comment = cursorToComment(cursor);
comments.add(comment);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return comments;
}
private Comment cursorToComment(Cursor cursor) {
Comment comment = new Comment();
comment.setId(cursor.getLong(0));
comment.setComment(cursor.getString(1));
return comment;
}
}
Upvotes: 1
Reputation: 2409
You could use SQLiteOpenHelper's onCreate method to execute the sql needed to create your database. The sql could either be hard coded or read in from a file. Doing it this way would allow you initialize the database when the application is first ran.
Another option would be to use a tool such as SQLite Database Browser
This is a desktop application that you can use to build the sqlite database. Once you've created it you'll have to decide how to deploy it. I've never tried doing this but I imagine you could bundle it in your resource folder and then move it into the correct directory when the app first runs after being installed.
Upvotes: 0