Skip to content

Instantly share code, notes, and snippets.

@Akhu
Created December 23, 2019 21:48
Show Gist options
  • Save Akhu/2f050380605724b6459f68685b265798 to your computer and use it in GitHub Desktop.
Save Akhu/2f050380605724b6459f68685b265798 to your computer and use it in GitHub Desktop.
SQLite Hell on Android
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class UserTable {
public static final String USER_TABLE = "USER_TABLE";
private static final String TAG = "UserTable";
private static final String ID = "ID";
private static final String NAME = "NAME";
private static final String FIRSTNAME = "FIRSTNAME";
private static final String URL_PICTURE = "URL_PICTURE";
public static final String DATABASE_CREATE_USER_TABLE = "create table if not exists " + USER_TABLE +
" (" + ID + " INTEGER primary key AUTOINCREMENT NOT NULL, " +
UUID + " TEXT not null, " +
NAME + " TEXT, " +
FIRSTNAME + " TEXT, " +
URL_PICTURE + " TEXT);";
public static void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE_USER_TABLE);
Log.w(TAG, "onCreate: " + DATABASE_CREATE_USER_TABLE);
}
public static void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database " + USER_TABLE);
switch (oldVersion + 1) {
case 1:
case 2:
case 3:
case 4:
case 5:
database.execSQL(DATABASE_CREATE_USER_TABLE);
}
}
public static void onDrop(SQLiteDatabase database) {
database.execSQL("DROP TABLE IF EXISTS " + USER_TABLE);
database.execSQL(DATABASE_CREATE_USER_TABLE);
}
public static void insert(User user) {
SQLiteDatabase database = DatabaseHelper.getSQLiteDatabase();
if (getOne(user.getUuid()) != null) {
//Log.e(TAG, "UPDATE: " + user.toString());
update(user);
} else {
//Log.e(TAG, "insert: " + user.toString());
ContentValues newValue = new ContentValues();
newValue.put(UUID, user.getUuid());
newValue.put(NAME, user.getName());
newValue.put(FIRSTNAME, user.getFirstName());
newValue.put(URL_PICTURE, user.getUrlPicture());
database.insert(USER_TABLE, null, newValue);
}
}
public static int update(User user) {
SQLiteDatabase database = DatabaseHelper.getSQLiteDatabase();
ContentValues newValue = new ContentValues();
newValue.put(NAME, user.getName());
newValue.put(FIRSTNAME, user.getFirstName());
newValue.put(URL_PICTURE, user.getUrlPicture());
return database.update(USER_TABLE, newValue, UUID +" = \"" + user.getUuid() + "\"", null);
}
public static int updateName(String name, String uuid) {
SQLiteDatabase database = DatabaseHelper.getSQLiteDatabase();
ContentValues newValue = new ContentValues();
newValue.put(NAME, name);
return database.update(USER_TABLE, newValue, UUID + " = \"" + uuid + "\"", null);
}
public static boolean remove(String uuid) {
SQLiteDatabase database = DatabaseHelper.getSQLiteDatabase();
return database.delete(USER_TABLE, UUID + " = \"" + uuid + "\"", null) > 0;
}
public static User getOne(String uuid) {
SQLiteDatabase database = DatabaseHelper.getSQLiteDatabase();
Cursor reponse = database.query(USER_TABLE, new String[]{
UUID, NAME, FIRSTNAME, URL_PICTURE
}, UUID + " = \"" + uuid + "\"", null, null, null, null);
User user = null;
if (reponse.moveToFirst()) {
user = new User(reponse.getString(reponse.getColumnIndex(NAME)),
reponse.getString(reponse.getColumnIndex(FIRSTNAME))
);
user.setUuid(reponse.getString(reponse.getColumnIndex(UUID)));
user.setUrlPicture(reponse.getString(reponse.getColumnIndex(URL_PICTURE)));
}
reponse.close();
return user;
}
public static List<User> getAll() {
SQLiteDatabase database = DatabaseHelper.getSQLiteDatabase();
Cursor reponse = database.query(USER_TABLE, new String[]{
UUID, NAME, FIRSTNAME, URL_PICTURE
}, null, null, null, null, null);
List<User> userList = new ArrayList<>();
if (reponse.moveToFirst()) {
do {
User user = new User(reponse.getString(reponse.getColumnIndex(NAME)),
reponse.getString(reponse.getColumnIndex(FIRSTNAME))
);
user.setUuid(reponse.getString(reponse.getColumnIndex(UUID)));
user.setUrlPicture(reponse.getString(reponse.getColumnIndex(URL_PICTURE)));
userList.add(user);
} while (reponse.moveToNext());
}
reponse.close();
return userList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment