Skip to content

Operation

WangTingZheng edited this page Jul 10, 2020 · 6 revisions

In this page, you will learn how to execute some table operation with MyOrm

Database

Before you use this database, you must get this database's apt(annotation process tool) from Database define class:

DatabaseApt home = new DatabaseApt(Home.class)

In Home.class, you must define database, more detail please check this page.

Create

Use this statement to create a database in your database software, if this database has already existed, this statement will not be valid.

home.create();

Drop

Use this statement to delete this database in you database software. All data will be lost!!!!

home.delete();

Table

Before you use this table, you must define table and database, more detail please check this page. You can get table apt object from database apt object:

TableApt user = home.newInstance("user");

or you can use table class directly:

TableApt user = home.newInstance(User.class);

The statement above will get a tableApt object which stand for the table called "user".

Create

Use this statement to create a table in your database , if this table has already existed, this statement will not be valid.

home.create();

Drop

Use this statement to delete this table in your database. All data will be lost!!!!

home.delete();

Close

Use this statement to close the connection to database. All sql operation will be invalid until renew a instance.

home.close();

SQL

In this, you will learn how to execute sql operation in MyOrm.

Add

Before execute this operation, you need to new a table object.

User user_add = new User("wangtingzheng","iloveMyOrm"):

This stands for a record, It's username equals "wangtingzheng", password equals "iloveMyOrm"(Because I set the construction method to initialize username and password, more detail please check this page)

And then, you need to use this to add record to you table:

user.add(user_add);

If this operation be executed successfully, this method will return true, if not, return false. When the item not be initialize totally(such as you just initialize username, password is empty), MyOrm will add null record in empty item.

If If the code above be executed, the record which username equals "wangtingzheng", password equals "iloveMyOrm" will be add into table use:happy:.

Delete

Before execute this operation, you need to new a table object.

User user_delete = new User("wangtingzheng","iloveMyOrm"):

This stands for a record, It's username equals "wangtingzheng", password equals "iloveMyOrm". MyOrm will use this record to look for some records in you table, if find, delete it. When the item not be initialize totally(such as you just initialize username, password is empty), MyOrm will think you don't care about this item's value.

And then, you need to use this to delete record from your table:

user.delete(user_delete);

If this operation be executed successfully, this method will return true, if not, return false.

If the code above be executed, the record which username equals "wangtingzheng" and password equals "iloveMyOrm" will be deleted from table user.:pensive:

Update

Before execute this operation, you need to new two table objects.

User user_update_old = new User("wangtingzheng"):
User user_update_new = new User("wangtingzheng","ihateMyOrm"):

The old table object will as the select condition, as same as delete, when the items are not be initialize totally, MyOrm will think you don't care about this item's value.

The new table object will as the add operation parameter, be loaded with add method.

And then, you need to use this to delete record from your table:

user.update(user_update_old, user_update_new);

If this operation be executed successfully, this method will return true, if not, return false.

If the code above be executed, the password of the record which the username value equals "wangtingzheng" in table user will be changed to ihateMyOrm 🆕.

Select

Before execute this operation, you need to new a table object.

User user_select = new User("wangtingzheng","iloveMyOrm"):

MyOrm will use this record to look for some records in you table, if find, output it to ReslutSet. When the item not be initialize totally, MyOrm will think you don't care about this item's value.

And then, you need to use this to delete record from your table:

user.delete(user_delete);

If this operation be executed successfully, this method will return a ReslutSet 😆, if not, throw out a DatabaseExcuteNoResult exception 😭.

Check if existed

Before execute this operation, you need to new a table object.

User user_if_existed = new User("wangtingzheng","iloveMyOrm"):

MyOrm will use this record to look for some records in you table.

And then, you need to use this to check record if existed in your table:

user.delete(user_delete);

if find, return true 🉑 ,if not return false 😕 . When the item not be initialize totally, MyOrm will think you don't care about this item's value.

Clone this wiki locally