Created
November 30, 2016 09:44
-
-
Save ssaurel/000038c34fdd66ab39d3825df8893d51 to your computer and use it in GitHub Desktop.
SQLiteOpenHelper implementation for a SQLite Tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ssaurel.samples.sqlite; | |
import java.util.LinkedList; | |
import java.util.List; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
public class SQLiteDatabaseHandler extends SQLiteOpenHelper { | |
private static final int DATABASE_VERSION = 1; | |
private static final String DATABASE_NAME = "PlayersDB"; | |
private static final String TABLE_NAME = "Players"; | |
private static final String KEY_ID = "id"; | |
private static final String KEY_NAME = "name"; | |
private static final String KEY_POSITION = "position"; | |
private static final String KEY_HEIGHT = "height"; | |
private static final String[] COLUMNS = { KEY_ID, KEY_NAME, KEY_POSITION, | |
KEY_HEIGHT }; | |
public SQLiteDatabaseHandler(Context context) { | |
super(context, DATABASE_NAME, null, DATABASE_VERSION); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
String CREATION_TABLE = "CREATE TABLE Players ( " | |
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, " + "name TEXT, " | |
+ "position TEXT, " + "height INTEGER )"; | |
db.execSQL(CREATION_TABLE); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
// you can implement here migration process | |
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); | |
this.onCreate(db); | |
} | |
public void deleteOne(Player player) { | |
// Get reference to writable DB | |
SQLiteDatabase db = this.getWritableDatabase(); | |
db.delete(TABLE_NAME, "id = ?", new String[] { String.valueOf(player.getId()) }); | |
db.close(); | |
} | |
public Player getPlayer(int id) { | |
SQLiteDatabase db = this.getReadableDatabase(); | |
Cursor cursor = db.query(TABLE_NAME, // a. table | |
COLUMNS, // b. column names | |
" id = ?", // c. selections | |
new String[] { String.valueOf(id) }, // d. selections args | |
null, // e. group by | |
null, // f. having | |
null, // g. order by | |
null); // h. limit | |
if (cursor != null) | |
cursor.moveToFirst(); | |
Player player = new Player(); | |
player.setId(Integer.parseInt(cursor.getString(0))); | |
player.setName(cursor.getString(1)); | |
player.setPosition(cursor.getString(2)); | |
player.setHeight(Integer.parseInt(cursor.getString(3))); | |
return player; | |
} | |
public List<Player> allPlayers() { | |
List<Player> players = new LinkedList<Player>(); | |
String query = "SELECT * FROM " + TABLE_NAME; | |
SQLiteDatabase db = this.getWritableDatabase(); | |
Cursor cursor = db.rawQuery(query, null); | |
Player player = null; | |
if (cursor.moveToFirst()) { | |
do { | |
player = new Player(); | |
player.setId(Integer.parseInt(cursor.getString(0))); | |
player.setName(cursor.getString(1)); | |
player.setPosition(cursor.getString(2)); | |
player.setHeight(Integer.parseInt(cursor.getString(3))); | |
players.add(player); | |
} while (cursor.moveToNext()); | |
} | |
return players; | |
} | |
public void addPlayer(Player player) { | |
SQLiteDatabase db = this.getWritableDatabase(); | |
ContentValues values = new ContentValues(); | |
values.put(KEY_NAME, player.getName()); | |
values.put(KEY_POSITION, player.getPosition()); | |
values.put(KEY_HEIGHT, player.getHeight()); | |
// insert | |
db.insert(TABLE_NAME,null, values); | |
db.close(); | |
} | |
public int updatePlayer(Player player) { | |
SQLiteDatabase db = this.getWritableDatabase(); | |
ContentValues values = new ContentValues(); | |
values.put(KEY_NAME, player.getName()); | |
values.put(KEY_POSITION, player.getPosition()); | |
values.put(KEY_HEIGHT, player.getHeight()); | |
int i = db.update(TABLE_NAME, // table | |
values, // column/value | |
"id = ?", // selections | |
new String[] { String.valueOf(player.getId()) }); | |
db.close(); | |
return i; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment