1.0.9
SQLite Connection for Android. ORM tool for Android devices. First ORM with Geometric-Spatial data support for Android operating systems.
There is relational table support. OneToOne and OneToMany are fully supported. OHQL support for simple inquiries.
v1.0.8 -> Solved single quotes problem.
v1.0.9 -> Folder created automatically.
To use the library, first include it your project using Gradle
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
and:
dependencies {
compile 'com.github.10uroi:OHibernate:1.0.9'
}
public class MainActivity extends AppCompatActivity {
//I set the database path and the name
public static final String DATABASE_EXTERNAL = Environment.getExternalStorageDirectory().getPath(); //external
public static final String DATABASE_SUB = "/folderName/";
public static final String DATABASE_DB_PATH = DATABASE_EXTERNAL + DATABASE_SUB;
public static final String DATABASE_DATA_NAME = "databaseName.sqlite";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OHibernateConfig.DB_PATH = DATABASE_DB_PATH; // DATABASE PATH
OHibernateConfig.DB_NAME = DATABASE_DATA_NAME; // DATABASE NAME
}
}
@Entity(TABLE_NAME = "example", TABLE_OPERATION = Entity.TABLE_OPERATION_TYPE.DROP_AND_CREATE)
public class ExampleBean {
@Id(PRIMARY_KEY_AUTOINCREMENT = true)
private Integer id;
private String name;
private String surname;
private int age;
@Column(NAME = "active") //column customization
private boolean status;
@Column(DATETIME = true) //column customization
private String datetime;
//GETTER - SETTER
}
public class ExampleBeanDAO {
//Automatic Transactions
OHibernate<ExampleBean> oHibernate = new OHibernate<>(ExampleBean.class);
public void insert(ExampleBean exampleBean){
try {
oHibernate.insert(exampleBean); // Returns the id of the object
} catch (Exception e) {
e.printStackTrace();
Log.e("Error",e.getMessage());
}
}
public void update(ExampleBean exampleBean){
try {
oHibernate.update(exampleBean);
} catch (Exception e) {
e.printStackTrace();
Log.e("Error",e.getMessage());
}
}
public void delete(ExampleBean exampleBean){
try {
oHibernate.delete(exampleBean);
} catch (Exception e) {
e.printStackTrace();
Log.e("Error",e.getMessage());
}
}
public ExampleBean select(Integer id){
try {
return (ExampleBean) oHibernate.where("id",id).select();
} catch (Exception e) {
e.printStackTrace();
Log.e("Error",e.getMessage());
}
return null;
}
public ArrayList<ExampleBean> selectAll(){
try {
return (ArrayList<ExampleBean>) oHibernate.selectAll();
} catch (Exception e) {
e.printStackTrace();
Log.e("Error",e.getMessage());
}
return null;
}
public ArrayList<ExampleBean> selectAll(String surname){
try {
return (ArrayList<ExampleBean>) oHibernate.where("surname",surname).limit(5).selectAll(); // custom
} catch (Exception e) {
e.printStackTrace();
Log.e("Error",e.getMessage());
}
return null;
}
public ExampleBean selectCustom(String name,String surname){
try {
return (ExampleBean) oHibernate.where("name",name).and().where("surname",surname, LIKE_TYPE.BOTH).select(); // custom
} catch (Exception e) {
e.printStackTrace();
Log.e("Error",e.getMessage());
}
return null;
}
}
User | Address |
---|---|
|
|
users | addresses | ||||||
---|---|---|---|---|---|---|---|
id | firstname | lastname | id | county | phone_number | user_id | |
1 | Onur | Ciner | ┬► | 1 | Ankara | 05554443322 | 1 |
└► | 2 | İstanbul | 05554443311 | 1 |
User user = (User) new OQuery()
.addEntity(User.class) //=>Returns a String if entity is not added
.Select("*")
.From("users") //=> "users"->table name
.Where("id",2)
.getSingleResult(); //=> Fetch user with id 2 in the users table
ArrayList<User> users = new OQuery()
.addEntity(User.class) //=>Returns a String if entity is not added
.Select("*")
.From("users") //=> "users"->table name
.list(); //=> Brings all users in the users table
new OQuery()
.SetParameter("firstName","Onur")
.SetParameter("lastName","Ciner")
.Insert("users"); //=> "users"->table name
Users user = new Users(); //=>The object is created
user.setFirstName("Onur");
user.setLastName("Ciner");
new OQuery().InsertEntity("users",user); //=> "users"->table name
new OQuery()
.SetParameter("firstName", "Selçuk")
.SetParameter("lastName", "Uzunsoy")
.Where("id", 15) //=> User with id 15 will be updated
.Update("users"); //=> "users"->table name
Users user = OQuery().Select...; //=> Object brought
user.setFirstName("Onur");
user.setLastName("Ciner");
new OQuery().UpdateEntity("users",user); //=> "users"->table name
new OQuery()
.Where("id",15) //=> User with id 15 will be deleted
.Delete("users"); //=> "users"->table name
new OQuery()
.DeleteAll("users"); //=> All users in the users table will be deleted