Create Database - Meraj/ArioSql GitHub Wiki

you can create a database with this library too !
and you can update your database structure in future !
ArioDatabase class handle work for creating database
at the first step ,lets define ArioDatabase Class

val database = ArioDatabase(context)

ok, now lets explain

🔵 dbName / set database name (required)

with this function we can set our database name that we are going to create use it like :

database.dbName("my new database name") // database name as String

🟢 version / set database version (optional)

if we want to upgrade our database in future (for example add new columns in next version or something like that) we need to define our database version :

database.version(1) // database version as Int

🟣 table / add a new table

we have our database now, and this database need some tables, for setting a new table do like :

database.table("my_table_name") // database table as String

⚪ columns / add new columns to our table

our table need some columns ,right ?
for adding unique id for each row in your table use like :

database.id()

its add a column named "id" to your table,that give your rows unique id for each
now let`s add other columns
for adding columns do like :

database.column("my_new_column",CreateDatabase.VARCHAR) // set column name And sql data type for it

you can use ArioSql to get database data types, without be worry about you write it correct or not ;)
there is 13 different data type that you can get like : CreateDatabase.VARCHAR or ..... (data types list)
if you want to include your customized data types with default value ,Null or Not Null you can write it your self ,like below :

database.column("new_buddy","VARCHAR (256) NOT NULL")

in ArioSql you can use timestamp feature (Like one that laravel have) if you want to implement it in your table use do like :

database.timestamp()

and booom !!!
automatically add 2 new columns to your table (created_at, updated_at)
when add a new row/index to your table created_at and updated_at columns will automatically fill with current timestamp in millisecond
when you update a row, updated_at column of that row will automatically update to current timestamp in millisecond \

🟦 upgrade your database

sometimes maybe you want to upgrade your app database and add new columns to created tables or ...
if in your next app version you want to upgrade your database you can use upgradeRaw() function to do it
when app launch ArioSql check current created database version and your selected database version if the number you choose for your database in greater will run upgrade database query that you want
for write your custom upgrade query write :

database.upgradeRaw("DROP TABLE IF EXISTS my_table_name")

if you want to delete the table and recreate it add :

database.inUpgradeReCreateIt()

and done ! in next version table will be deleted and reCreated

🟤 initialize

now it`s time to create our database with all tables and columns to initialize it do like :

database.init()

and done your database created

🟩 A Simple Example

val database = ArioDatabase(context)

database
     .dbName("database_name") // set database name
     .version(1) // set database version (its going to help us in next update for or database)
     .table("persons") // set table name
        .id() // this function automatically add unique id column
        .column("person_name",CreateDatabase.VARCHAR) // add a new column for "persons" table
        .column("phone_number",CreateDatabase.INT) // add a new column for "persons" table
        .timestamp() // add timestamp feature (auto add created_at and updated_at column and manage theme)
        .upgradeRaw("DROP TABLE IF EXISTS persons") // this sql query will run when you update your database version
      .table("person_jobs") // set another table name
         .id() // add unique id column for or table
         .column("person_id",CreateDatabase.INT) // add a new column for "person_jobs" table
         .column("job_id",CreateDatabase.INT) // add a new column for "person_jobs" table
         .timestamp() // again enable timestamp feature
         .inUpgradeReCreateIt()
      .table("job_list") // set new table name
         .id() // add unique id column for or table
         .column("job_title",CreateDatabase.VARCHAR) // add a new column for "person_jobs" table
      .init() // initialize database

for table "persons" :

  • unique id column will add auto
  • timestamp feature is enabled
  • a custom query will be run in database version version

for table "person_jobs" :

  • unique id column will add auto
  • timestamp feature is enabled
  • in next database version table will be deleted and reCreate

for table "job_list" :

  • unique id column will add auto
  • timestamp feature is DISABLED
  • in next database version NOTHING will happen !