├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── me │ │ └── shalvah │ │ └── dbtest │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── me │ │ │ └── shalvah │ │ │ └── dbtest │ │ │ ├── CourseListActivity.java │ │ │ ├── MainActivity.java │ │ │ ├── StudentListActivity.java │ │ │ └── TestSimpleContentProvider.java │ └── res │ │ ├── layout │ │ ├── activity_course_list.xml │ │ ├── activity_main.xml │ │ ├── content_main.xml │ │ └── course_list_item.xml │ │ ├── menu │ │ └── menu_list.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-v21 │ │ └── styles.xml │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── me │ └── shalvah │ └── dbtest │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── projectFilesBackup └── .idea │ └── workspace.xml ├── settings.gradle └── simple-db ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src ├── androidTest └── java │ └── me │ └── shalvah │ └── simpledb │ └── ExampleInstrumentedTest.java ├── main ├── AndroidManifest.xml ├── java │ └── me │ │ └── shalvah │ │ └── simpledb │ │ ├── Column.java │ │ ├── Schema.java │ │ ├── SimpleContentProvider.java │ │ └── Table.java └── res │ └── values │ └── strings.xml └── test └── java └── me └── shalvah └── simpledb └── ExampleUnitTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | dbtest -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | 47 | 48 | 49 | 50 | 1.8 51 | 52 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

SimpleDB

2 | 3 |

Download

4 | 5 | SimpleDB is an Android library that makes working with SQLite databases reaaaally simple. The aim is to get your app up and running with a functional SQLite database within minutes (true) without you worrying about the internals, so you can focus on building a great app. 6 | 7 | You don't need to craft complex SQL statements anymore. All you need to do now is define your schema, and SimpleDB handles the rest! 8 | 9 | # Setup 10 | * Step 0: Add the dependency to your app's `build.gradle` 11 | ``` 12 | compile 'me.shalvah.simpledb:simple-db:0.5.0' 13 | ``` 14 | 15 | * Step 1: Create a class extending the `SimpleContentProvider`. This is your app's 16 | ContentProvider. Define your provider, database, and database version in a static block: 17 | names if you wish. 18 | ``` 19 | public class MySimpleContentProvider extends SimpleContentProvider 20 | { 21 | 22 | static 23 | { 24 | DB_NAME = "mydb"; 25 | PROVIDER_NAME = "me.shalvah.myapp.provider"; 26 | DB_VERSION = 1; 27 | } 28 | 29 | } 30 | ``` 31 | 32 | * Step 2: Implement the `setup()` function in your SimpleContentProvider. This is where you define your database schema. 33 | 34 | ``` 35 | public void setup() 36 | { 37 | //create columns 38 | Column producerId = Column.id("_id"); 39 | Column producerName = Column.text("name"); 40 | Column producerAge = Column.integer("age"); 41 | 42 | //create table and add columns 43 | Table producers = new Table("producers", producerName, producerAge); 44 | 45 | Column itemName = Column.text("name").unique(); 46 | Column itemTypee = Column.text("type"); 47 | Column itemPrice = Column.integer("price").notNull(); 48 | Column itemProducedBy = Column.integer("produced_by").foreignKey("producers","id"); 49 | 50 | Table items = new Table("courses", itemName, itemType, itemPrice, itemProducedBy); 51 | 52 | } 53 | ``` 54 | Note that if a table has a column which is a foreign key to another, make sure you add the "independent" table first. 55 | 56 | * Step 3: You're good to go! Use the class you created as your ContentPovider. 57 | 58 | # Quickstart 59 | ## Creating columns 60 | ``` 61 | Column.integer(columnName); 62 | Column.text(columnName); 63 | Column.real(columnName); 64 | Column.null(columnName); 65 | Column.blob(columnName); 66 | ``` 67 | These methods all return Column objects, so you can chain the methods below to set additional properties 68 | 69 | ## Setting column properties 70 | If `col` is a `Column` object: 71 | ``` 72 | col.notNull(); 73 | col.primaryKey(); 74 | col.id(); 75 | col.unique(); 76 | col.autoIncrement(); 77 | col.foreignKey(referencesTableName, referencesColumnName); 78 | ``` 79 | Again, these methods all return Column objects, so you can chain them to set additional properties 80 | 81 | ## Creating a Table 82 | ``` 83 | Table t=new Table(tableName, col1, col2, col3, ...); 84 | 85 | Column[] cols=new Column[] {col1, col2, col3, ...}; 86 | Table t=new Table(tableName, cols);``` 87 | ``` 88 | 89 | ## CRUD 90 | ``` 91 | //insert a new item 92 | getContentResolver().insert(TestSimpleContentProvider.contentUri(tableName), values); 93 | 94 | //delete all items 95 | getContentResolver().delete(MySimpleContentProvider.contentUri("tableName"), null, null); 96 | 97 | //delete a particular item by id 98 | getContentResolver().delete(MySimpleContentProvider.contentUri 99 | (tableName + "/" + id), 100 | null, 101 | null); 102 | ``` 103 | 104 | ## Using with a CursorLoader 105 | Loading data: 106 | ```private void fillData() 107 | { 108 | getLoaderManager().initLoader(0, null, this); 109 | 110 | sca = new SimpleCursorAdapter(this, R.layout.item_list_item, null, 111 | new String[]{"id", "name", "type", "price"}, 112 | new int[] {R.id._id, R.id.name, R.id.type, R.id.price}, 0); 113 | mListView.setAdapter(sca); 114 | } 115 | ``` 116 | 117 | Callbacks: 118 | ``` 119 | @Override 120 | public Loader onCreateLoader(int id, Bundle args) 121 | { 122 | String[] projection = new String[]{"id", "name", "type", "price"}; 123 | return new CursorLoader(this, TestSimpleContentProvider.contentUri 124 | ("items"), projection, null, 125 | null, 126 | null); 127 | } 128 | ``` 129 | 130 | A simple test app using the library can be found [here] (https://github.com/Shalvah/dbtest). 131 | 132 | # Features 133 | * Foreign key support. Simpledb also automatically sets an fk column to be on-null if referencing an "_id" column; otherwise, you'll have to do that yourself or risk getting an SQL error 134 | * Content URIs: Simpledb automatically generates these. 135 | 136 | # Future features :) 137 | * Migrations: Currently, you have to manually increment the database version if you change your 138 | schema, and you lose all data (db is recreated) 139 | 140 | # Changelog (0.6.0) 141 | - Changed method namesto match Java convention 142 | - Removed auto adding of id column to prevent confusion 143 | - Removed requirement to call init() in setup() 144 | 145 | # Contributing 146 | All contributions are welcome! Starting with improvements to this doc! Correcting typos and feature additions equally welcome. You could also work on one of the "Future Features" above. 147 | If you discover an issue and don't have time to fix it, please add it in the issue tracker. Thanks! 148 | Please see the contribution guide 149 | 150 | Thanks for checking out Simpledb! I hope it does make your work simpler. 151 | Please star if you find it useful! Thanks! 152 | 153 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.3" 6 | 7 | defaultConfig { 8 | applicationId "me.shalvah.dbtest" 9 | minSdkVersion 17 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(include: ['*.jar'], dir: 'libs') 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:23.2.1' 26 | compile 'com.android.support:design:23.2.1' 27 | compile 'me.shalvah.simpledb:simple-db:0.5.0' 28 | } 29 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in C:\Android\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/me/shalvah/dbtest/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package me.shalvah.dbtest; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase 10 | { 11 | public ApplicationTest() 12 | { 13 | super(Application.class); 14 | } 15 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 26 | 27 | 28 | 32 | 33 | 34 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /app/src/main/java/me/shalvah/dbtest/CourseListActivity.java: -------------------------------------------------------------------------------- 1 | package me.shalvah.dbtest; 2 | 3 | import android.app.LoaderManager; 4 | import android.content.ContentValues; 5 | import android.content.CursorLoader; 6 | import android.content.Loader; 7 | import android.database.Cursor; 8 | import android.os.Bundle; 9 | import android.support.v7.app.ActionBar; 10 | import android.support.v7.app.AppCompatActivity; 11 | import android.support.v7.widget.Toolbar; 12 | import android.view.View; 13 | import android.widget.EditText; 14 | import android.widget.ListView; 15 | import android.widget.SimpleCursorAdapter; 16 | 17 | 18 | public class CourseListActivity extends AppCompatActivity implements LoaderManager 19 | .LoaderCallbacks 20 | { 21 | private ListView lv; 22 | private SimpleCursorAdapter sca; 23 | 24 | @Override 25 | protected void onCreate(Bundle savedInstanceState) 26 | { 27 | super.onCreate(savedInstanceState); 28 | setContentView(R.layout.activity_course_list); 29 | 30 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 31 | setSupportActionBar(toolbar); 32 | 33 | ActionBar actionBar = getSupportActionBar(); 34 | if (actionBar != null) 35 | { 36 | actionBar.setDisplayHomeAsUpEnabled(true); 37 | } 38 | lv = (ListView) findViewById(android.R.id.list); 39 | fillData(); 40 | } 41 | 42 | private void fillData() 43 | { 44 | getLoaderManager().initLoader(0, null, this); 45 | 46 | sca = new SimpleCursorAdapter(this, R.layout.course_list_item, null, 47 | new String[]{TestSimpleContentProvider.COLUMN_ID, 48 | TestSimpleContentProvider.COLUMN_COURSES_CODE, 49 | TestSimpleContentProvider.COLUMN_COURSES_TITLE, 50 | TestSimpleContentProvider.COLUMN_COURSES_UNITS, 51 | TestSimpleContentProvider.COLUMN_COURSES_DESCRIPTION, 52 | TestSimpleContentProvider.COLUMN_COURSES_TOP_STUDENT 53 | }, new int[] 54 | {R.id._id, R.id.code, R.id.title, R.id.units, R.id.description, R.id.studentId}, 0); 55 | lv.setAdapter(sca); 56 | 57 | } 58 | 59 | @Override 60 | public Loader onCreateLoader(int id, Bundle args) 61 | { 62 | String[] projection = new String[]{TestSimpleContentProvider.COLUMN_ID, 63 | TestSimpleContentProvider.COLUMN_COURSES_CODE, 64 | TestSimpleContentProvider.COLUMN_COURSES_TITLE, 65 | TestSimpleContentProvider.COLUMN_COURSES_UNITS, 66 | TestSimpleContentProvider.COLUMN_COURSES_DESCRIPTION, 67 | TestSimpleContentProvider.COLUMN_COURSES_TOP_STUDENT}; 68 | return new CursorLoader(this, TestSimpleContentProvider.contentUri 69 | (TestSimpleContentProvider.TABLE_COURSES), projection, null, 70 | null, 71 | null); 72 | } 73 | 74 | @Override 75 | public void onLoadFinished(Loader loader, Cursor data) 76 | { 77 | sca.swapCursor(data); 78 | } 79 | 80 | @Override 81 | public void onLoaderReset(Loader loader) 82 | { 83 | sca.swapCursor(null); 84 | } 85 | 86 | public void add(View view) 87 | { 88 | String text = ((EditText) findViewById(R.id.inputCourseET)).getText().toString(); 89 | String[] data = text.split(","); 90 | ContentValues values = new ContentValues(); 91 | try 92 | { 93 | values.put(TestSimpleContentProvider.COLUMN_COURSES_CODE, data[0]); 94 | values.put(TestSimpleContentProvider.COLUMN_COURSES_TITLE, data[1]); 95 | values.put(TestSimpleContentProvider.COLUMN_COURSES_UNITS, data[2]); 96 | values.put(TestSimpleContentProvider.COLUMN_COURSES_DESCRIPTION, data[3]); 97 | values.put(TestSimpleContentProvider.COLUMN_COURSES_TOP_STUDENT, data[4]); 98 | } catch (Exception e) 99 | { 100 | e.printStackTrace(); 101 | } 102 | getContentResolver().insert(TestSimpleContentProvider.contentUri(TestSimpleContentProvider 103 | .TABLE_COURSES), values); 104 | ((EditText) findViewById(R.id.inputCourseET)).setText(""); 105 | } 106 | 107 | public void clear(View view) 108 | { 109 | String text = ((EditText) findViewById(R.id.inputCourseET)).getText().toString(); 110 | if (text.equalsIgnoreCase("")) 111 | { 112 | getContentResolver().delete(TestSimpleContentProvider.contentUri 113 | (TestSimpleContentProvider.TABLE_COURSES), null, null); 114 | } else 115 | { 116 | getContentResolver().delete(TestSimpleContentProvider.contentUri 117 | (TestSimpleContentProvider.TABLE_COURSES + "/" + text), 118 | null, 119 | null); 120 | ((EditText) findViewById(R.id.inputCourseET)).setText(""); 121 | } 122 | } 123 | } -------------------------------------------------------------------------------- /app/src/main/java/me/shalvah/dbtest/MainActivity.java: -------------------------------------------------------------------------------- 1 | package me.shalvah.dbtest; 2 | 3 | import android.content.Context; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.support.v7.widget.Toolbar; 8 | import android.view.View; 9 | 10 | public class MainActivity extends AppCompatActivity 11 | { 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) 15 | { 16 | super.onCreate(savedInstanceState); 17 | setContentView(R.layout.activity_main); 18 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 19 | setSupportActionBar(toolbar); 20 | 21 | } 22 | 23 | public void launch(View view) 24 | { 25 | Context context = view.getContext(); 26 | Intent intent; 27 | switch (view.getId()) 28 | { 29 | case R.id.studentsBtn: 30 | intent = new Intent(context, StudentListActivity.class); 31 | break; 32 | default: 33 | intent = new Intent(context, CourseListActivity.class); 34 | break; 35 | } 36 | context.startActivity(intent); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/java/me/shalvah/dbtest/StudentListActivity.java: -------------------------------------------------------------------------------- 1 | package me.shalvah.dbtest; 2 | 3 | import android.app.LoaderManager; 4 | import android.content.ContentValues; 5 | import android.content.CursorLoader; 6 | import android.content.Loader; 7 | import android.database.Cursor; 8 | import android.os.Bundle; 9 | import android.support.v7.app.ActionBar; 10 | import android.support.v7.app.AppCompatActivity; 11 | import android.support.v7.widget.Toolbar; 12 | import android.view.View; 13 | import android.widget.EditText; 14 | import android.widget.ListView; 15 | import android.widget.SimpleCursorAdapter; 16 | 17 | public class StudentListActivity extends AppCompatActivity implements LoaderManager 18 | .LoaderCallbacks 19 | { 20 | private ListView lv; 21 | private SimpleCursorAdapter sca; 22 | 23 | @Override 24 | protected void onCreate(Bundle savedInstanceState) 25 | { 26 | super.onCreate(savedInstanceState); 27 | setContentView(R.layout.activity_course_list); 28 | 29 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 30 | setSupportActionBar(toolbar); 31 | 32 | ActionBar actionBar = getSupportActionBar(); 33 | if (actionBar != null) 34 | { 35 | actionBar.setDisplayHomeAsUpEnabled(true); 36 | } 37 | lv = (ListView) findViewById(android.R.id.list); 38 | fillData(); 39 | } 40 | 41 | private void fillData() 42 | { 43 | getLoaderManager().initLoader(0, null, this); 44 | 45 | sca = new SimpleCursorAdapter(this, R.layout.course_list_item, null, 46 | new String[]{TestSimpleContentProvider.COLUMN_ID, 47 | TestSimpleContentProvider.COLUMN_STUDENTS_NAME, 48 | TestSimpleContentProvider.COLUMN_STUDENTS_AGE 49 | }, new int[] 50 | {R.id._id, R.id.code, R.id.title}, 0); 51 | lv.setAdapter(sca); 52 | 53 | } 54 | 55 | 56 | @Override 57 | public Loader onCreateLoader(int id, Bundle args) 58 | { 59 | String[] projection = new String[]{TestSimpleContentProvider.COLUMN_ID, 60 | TestSimpleContentProvider.COLUMN_STUDENTS_NAME, 61 | TestSimpleContentProvider.COLUMN_STUDENTS_AGE}; 62 | return new CursorLoader(this, TestSimpleContentProvider.contentUri 63 | (TestSimpleContentProvider.TABLE_STUDENTS), projection, null, 64 | null, 65 | null); 66 | } 67 | 68 | @Override 69 | public void onLoadFinished(Loader loader, Cursor data) 70 | { 71 | sca.swapCursor(data); 72 | } 73 | 74 | @Override 75 | public void onLoaderReset(Loader loader) 76 | { 77 | sca.swapCursor(null); 78 | } 79 | 80 | 81 | public void add(View view) 82 | { 83 | String text = ((EditText) findViewById(R.id.inputCourseET)).getText().toString(); 84 | String[] data = text.split(","); 85 | ContentValues values = new ContentValues(); 86 | try 87 | { 88 | values.put(TestSimpleContentProvider.COLUMN_STUDENTS_NAME, data[0]); 89 | values.put(TestSimpleContentProvider.COLUMN_STUDENTS_AGE, data[1]); 90 | 91 | } catch (Exception e) 92 | { 93 | e.printStackTrace(); 94 | } 95 | getContentResolver().insert(TestSimpleContentProvider.contentUri(TestSimpleContentProvider 96 | .TABLE_STUDENTS), values); 97 | ((EditText) findViewById(R.id.inputCourseET)).setText(""); 98 | } 99 | 100 | public void clear(View view) 101 | { 102 | String text = ((EditText) findViewById(R.id.inputCourseET)).getText().toString(); 103 | if (text.equalsIgnoreCase("")) 104 | { 105 | getContentResolver().delete(TestSimpleContentProvider.contentUri 106 | (TestSimpleContentProvider.TABLE_STUDENTS), null, null); 107 | } else 108 | { 109 | getContentResolver().delete(TestSimpleContentProvider.contentUri 110 | (TestSimpleContentProvider.TABLE_STUDENTS + "/" + text), 111 | null, 112 | null); 113 | ((EditText) findViewById(R.id.inputCourseET)).setText(""); 114 | } 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /app/src/main/java/me/shalvah/dbtest/TestSimpleContentProvider.java: -------------------------------------------------------------------------------- 1 | package me.shalvah.dbtest; 2 | 3 | import me.shalvah.simpledb.Column; 4 | import me.shalvah.simpledb.SimpleContentProvider; 5 | import me.shalvah.simpledb.Table; 6 | 7 | 8 | public class TestSimpleContentProvider extends SimpleContentProvider 9 | { 10 | 11 | public static final String DB_NAME = "dbtest"; 12 | 13 | public static final String PROVIDER_NAME = "me.shalvah.dbtest.dprovider"; 14 | 15 | public static final String TABLE_STUDENTS = "students"; 16 | public static final String COLUMN_STUDENTS_NAME = "name"; 17 | public static final String COLUMN_STUDENTS_AGE = "age"; 18 | 19 | public static final String TABLE_COURSES = "courses"; 20 | public static final String COLUMN_COURSES_CODE = "code"; 21 | public static final String COLUMN_COURSES_TITLE = "title"; 22 | public static final String COLUMN_COURSES_UNITS = "units"; 23 | public static final String COLUMN_COURSES_DESCRIPTION = "description"; 24 | public static final String COLUMN_COURSES_TOP_STUDENT = "top_student"; 25 | private static final int DB_VERSION = 2; 26 | 27 | public void setup() 28 | { 29 | //create columns 30 | Column studentName = Column.Text(COLUMN_STUDENTS_NAME); 31 | Column studentAge = Column.Integer(COLUMN_STUDENTS_AGE); 32 | 33 | //create table and add columns 34 | Table students = new Table(TABLE_STUDENTS, studentName, studentAge); 35 | 36 | Column courseCode = Column.Text(COLUMN_COURSES_CODE); 37 | Column courseTitle = Column.Text(COLUMN_COURSES_TITLE); 38 | Column courseUnits = Column.Integer(COLUMN_COURSES_UNITS); 39 | 40 | Table courses = new Table(TABLE_COURSES, courseTitle, courseCode, courseUnits); 41 | 42 | //you can set additional properties 43 | Column courseDesc = Column.Text(COLUMN_COURSES_DESCRIPTION) 44 | .notNull(); 45 | Column courseTopStudent = Column.Text(COLUMN_COURSES_TOP_STUDENT) 46 | .foreignKey(TABLE_STUDENTS, COLUMN_ID); 47 | 48 | //add a column to the table 49 | courses.add(courseDesc); 50 | courses.add(courseTopStudent); 51 | 52 | init(PROVIDER_NAME, DB_NAME, DB_VERSION, students, courses); 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_course_list.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 11 | 12 | 16 | 20 |