Created
May 27, 2015 03:48
-
-
Save cbedoy/c4894dd5878f3b754ddb to your computer and use it in GitHub Desktop.
ORMProvider
This file contains hidden or 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
UserORMProvider userORMProvider = new UserORMProvider(getApplicationContext(), "DB", null, 1); | |
//Add User | |
User userToAdd = new User(); | |
userToAdd.setId(1); | |
userToAdd.setName("Bedoy"); | |
userToAdd.setAge(24); | |
userORMProvider.addUser(userToAdd); | |
//Remove User | |
//Primero debe existir :D | |
User userToRemove = new User(); | |
userToRemove.setId(1); | |
userToRemove.setName("Bedoy"); | |
userToRemove.setAge(24); | |
userORMProvider.removeUser(userToRemove); | |
//User to update | |
//Primero debe existir si no lo creo | |
User userToUpdate = new User(); | |
userToUpdate.setId(1); | |
userToUpdate.setName("Bedoy"); | |
userToUpdate.setAge(1); | |
userORMProvider.updateUser(userToUpdate); | |
//List All Users | |
String TAG = MainActivity.class.getCanonicalName(); | |
List<User> userList = userORMProvider.getAllUsers(); | |
for(User user : userList){ | |
Log.i(TAG, String.valueOf(user.getId())); | |
Log.i(TAG, String.valueOf(user.getName())); | |
Log.i(TAG, String.valueOf(user.getName())); | |
} | |
This file contains hidden or 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 self.cbedoy.sqliteexample; | |
/** | |
* Created by Carlos Bedoy on 26/05/2015. | |
* <p/> | |
* Mobile App Developer | |
* SQLiteExample | |
* <p/> | |
* E-mail: [email protected] | |
* Facebook: https://www.facebook.com/carlos.bedoy | |
* Github: https://github.com/cbedoy | |
*/ | |
public class User | |
{ | |
private int id; | |
private String name; | |
private int age; | |
public void setAge(int age) { | |
this.age = age; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public int getId() { | |
return id; | |
} | |
public String getName() { | |
return name; | |
} | |
} |
This file contains hidden or 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 self.cbedoy.sqliteexample; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.DatabaseErrorHandler; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteOpenHelper; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* Created by Carlos Bedoy on 26/05/2015. | |
* <p/> | |
* Mobile App Developer | |
* SQLiteExample | |
* <p/> | |
* E-mail: [email protected] | |
* Facebook: https://www.facebook.com/carlos.bedoy | |
* Github: https://github.com/cbedoy | |
*/ | |
public class UserORMProvider extends SQLiteOpenHelper { | |
String sqlCreate = "CREATE TABLE User (code INTEGER, name TEXT, age INTEGER)"; | |
public UserORMProvider(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { | |
super(context, name, factory, version); | |
} | |
public UserORMProvider(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) { | |
super(context, name, factory, version, errorHandler); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase sqLiteDatabase) { | |
sqLiteDatabase.execSQL(sqlCreate); | |
} | |
@Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {} | |
public boolean addUser(User user){ | |
SQLiteDatabase writableDatabase = getWritableDatabase(); | |
ContentValues contentValues = new ContentValues(); | |
contentValues.put("code", user.getId()); | |
contentValues.put("name", user.getName()); | |
contentValues.put("age", user.getAge()); | |
long transactionResult = writableDatabase.insert("User", null, contentValues); | |
return transactionResult != -1; | |
} | |
public boolean updateUser(User user){ | |
SQLiteDatabase writableDatabase = getWritableDatabase(); | |
ContentValues contentValues = new ContentValues(); | |
contentValues.put("name", user.getName()); | |
contentValues.put("age", user.getAge()); | |
long transactionResult = writableDatabase.update("User", contentValues, "codigo=" + user.getId(), null); | |
return transactionResult != -1; | |
} | |
public boolean removeUser(User user){ | |
SQLiteDatabase writableDatabase = getWritableDatabase(); | |
long transactionResult = writableDatabase.delete("User", "codigo=" + user.getId(), null); | |
return transactionResult != -1; | |
} | |
public List<User> getAllUsers(){ | |
List<User> users = new ArrayList<>(); | |
SQLiteDatabase readableDatabase = getReadableDatabase(); | |
Cursor cursor = readableDatabase.rawQuery("Select * from User;", null); | |
if (cursor.moveToFirst()) | |
{ | |
do | |
{ | |
User user = new User(); | |
user.setId(cursor.getInt(0)); | |
user.setName(cursor.getString(1)); | |
user.setAge(cursor.getInt(2)); | |
users.add(user); | |
} | |
while (cursor.moveToNext()); | |
} | |
return users; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://satyan.github.io/sugar/getting-started.html