Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
swdev:android:database_sqlite [2018/04/29 13:37] smayr created |
swdev:android:database_sqlite [2018/05/02 20:30] (current) smayr [Database Utils] |
||
---|---|---|---|
Line 1: | Line 1: | ||
= Database: SQLite = | = Database: SQLite = | ||
==== DB Connector ==== | ==== DB Connector ==== | ||
- | Download | + | When using the built-in '' |
- | * [[https:// | + | |
- | * Extract JAR file to some folder. | + | |
- | * Add JAR file as module: '' | + | |
- | * JAR package should be now listed under the project Gradle Scripts: '' | + | |
- | * If there is an error including the JAR package, try the following: | + | |
- | * In '' | + | |
- | include ': | + | |
- | include ': | + | |
- | </ | + | |
- | * Sync project (File > Sync Project with Gradle Files). | + | |
- | * Add '' | + | |
- | * Right-click on the '' | + | |
- | * In the top left navigation pane of the " | + | |
- | * Click on the green plus (+), and select ' | + | |
- | * Click OK, and close all the opened windows. A synchronization process should take place once more. | + | |
- | * If there is an error including the JAR package, try the following: | + | |
- | * In '' | + | |
- | dependencies { | + | |
- | //... | + | |
- | compile project(': | + | |
- | } | + | |
- | </ | + | |
- | * If succesfull, under the '' | + | |
- | * Include the classes required in your code. | + | |
Line 76: | Line 52: | ||
public class DbHelper extends SQLiteOpenHelper | public class DbHelper extends SQLiteOpenHelper | ||
{ | { | ||
- | // | ||
- | // Properties | ||
// | // | ||
// If you change the database schema, you must increment the database version. | // If you change the database schema, you must increment the database version. | ||
- | | + | |
+ | // Properties | ||
public static final String DATABASE_NAME = " | public static final String DATABASE_NAME = " | ||
+ | public static final int DATABASE_VERSION = 1; | ||
private static final String SQL_CREATE_ENTRIES = | private static final String SQL_CREATE_ENTRIES = | ||
- | | + | |
- | DbContract.TeamEntry._ID + " INTEGER PRIMARY KEY," + | + | DbContract.TeamEntry._ID + " INTEGER PRIMARY KEY," + |
- | DbContract.TeamEntry.COL_NAME + " TEXT," + | + | DbContract.TeamEntry.COL_NAME + " TEXT," + |
- | DbContract.TeamEntry.COL_NUMBER + " TEXT)"; | + | DbContract.TeamEntry.COL_NUMBER |
+ | DbContract.TeamEntry.COL_RANKING | ||
private static final String SQL_DELETE_ENTRIES = | private static final String SQL_DELETE_ENTRIES = | ||
- | | + | |
Line 96: | Line 73: | ||
// Methods | // Methods | ||
// | // | ||
- | public DbHelper(Context context) { | + | public DbHelper(Context context) |
+ | | ||
super(context, | super(context, | ||
+ | Boolean createDatabase = false; | ||
+ | if(createDatabase) { | ||
+ | SQLiteDatabase db = this.getWritableDatabase(); | ||
+ | } | ||
} | } | ||
- | public void onCreate(SQLiteDatabase db) { | + | public void onCreate(SQLiteDatabase db) |
+ | | ||
db.execSQL(SQL_CREATE_ENTRIES); | db.execSQL(SQL_CREATE_ENTRIES); | ||
+ | |||
+ | // Add content to table | ||
+ | for(int i=1; i <= 20; i++) { | ||
+ | ContentValues values = new ContentValues(); | ||
+ | values.put(DbContract.TeamEntry.COL_NAME, | ||
+ | values.put(DbContract.TeamEntry.COL_NUMBER, | ||
+ | |||
+ | // Insert the new row, returning the primary key value of the new row | ||
+ | db.insert(DbContract.TeamEntry.TABLE_NAME, | ||
+ | } | ||
} | } | ||
- | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | + | public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) |
+ | | ||
// This database is only a cache for online data, so its upgrade policy is | // This database is only a cache for online data, so its upgrade policy is | ||
// to simply to discard the data and start over | // to simply to discard the data and start over | ||
Line 111: | Line 105: | ||
} | } | ||
- | public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { | + | public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) |
+ | | ||
onUpgrade(db, | onUpgrade(db, | ||
} | } | ||
Line 147: | Line 142: | ||
{ | { | ||
private DbHelper dbHelper; | private DbHelper dbHelper; | ||
- | SQLiteDatabase db; | + | |
public DbHelper getConnection(Context cx) | public DbHelper getConnection(Context cx) | ||
Line 155: | Line 150: | ||
} | } | ||
- | public long addTeamEntry() | + | public long addTeamRecord() |
{ | { | ||
// Gets the data repository in write mode | // Gets the data repository in write mode | ||
Line 171: | Line 166: | ||
} | } | ||
- | public List readTeamEntry() | + | public List readTeamRecords() |
{ | { | ||
db = dbHelper.getReadableDatabase(); | db = dbHelper.getReadableDatabase(); | ||
Line 180: | Line 175: | ||
BaseColumns._ID, | BaseColumns._ID, | ||
DbContract.TeamEntry.COL_NAME, | DbContract.TeamEntry.COL_NAME, | ||
- | DbContract.TeamEntry.COL_NUMBER | + | DbContract.TeamEntry.COL_NUMBER, |
+ | DbContract.TeamEntry.COL_RANKING | ||
}; | }; | ||
Line 188: | Line 184: | ||
// How you want the results sorted in the resulting Cursor | // How you want the results sorted in the resulting Cursor | ||
- | String sortOrder = | + | String sortOrder = DbContract.TeamEntry.COL_NUMBER + " DESC"; |
- | | + | |
Cursor cursor = db.query( | Cursor cursor = db.query( | ||
Line 203: | Line 198: | ||
List itemIds = new ArrayList<> | List itemIds = new ArrayList<> | ||
while(cursor.moveToNext()) { | while(cursor.moveToNext()) { | ||
- | long itemId = cursor.getLong( | + | long itemId = cursor.getLong( cursor.getColumnIndexOrThrow(DbContract.TeamEntry._ID) ); |
- | | + | |
itemIds.add(itemId); | itemIds.add(itemId); | ||
} | } | ||
Line 212: | Line 206: | ||
} | } | ||
- | public void deleteInfo() | + | |
+ | { | ||
+ | db = dbHelper.getReadableDatabase(); | ||
+ | |||
+ | // Define a projection that specifies which columns from the database | ||
+ | // you will actually use after this query. | ||
+ | String[] projection = { | ||
+ | BaseColumns._ID, | ||
+ | DbContract.TeamEntry.COL_NAME, | ||
+ | DbContract.TeamEntry.COL_NUMBER, | ||
+ | DbContract.TeamEntry.COL_RANKING | ||
+ | }; | ||
+ | |||
+ | // Filter results WHERE " | ||
+ | //String selection = DbContract.TeamEntry.COL_NAME + " = ?"; | ||
+ | //String[] selectionArgs = { "My Team" }; | ||
+ | String selection = null; // no filters | ||
+ | String[] selectionArgs = null; // no filters | ||
+ | |||
+ | // How you want the results sorted in the resulting Cursor | ||
+ | //String sortOrder = DbContract.TeamEntry.COL_NUMBER + " ASC"; | ||
+ | String sortOrder = DbContract.TeamEntry.COL_NAME + " ASC"; | ||
+ | |||
+ | Cursor cursor = db.query( | ||
+ | DbContract.TeamEntry.TABLE_NAME, | ||
+ | projection, | ||
+ | selection, | ||
+ | selectionArgs, | ||
+ | null, // don't group the rows | ||
+ | null, // don't filter by row groups | ||
+ | sortOrder | ||
+ | ); | ||
+ | |||
+ | Map< | ||
+ | while(cursor.moveToNext()) { | ||
+ | String name = cursor.getString( cursor.getColumnIndexOrThrow(DbContract.TeamEntry.COL_NAME) ); | ||
+ | String number = cursor.getString( cursor.getColumnIndexOrThrow(DbContract.TeamEntry.COL_NUMBER) ); | ||
+ | mapTeam.put(name, | ||
+ | } | ||
+ | cursor.close(); | ||
+ | |||
+ | return mapTeam; | ||
+ | } | ||
+ | |||
+ | public List< | ||
+ | { | ||
+ | try { | ||
+ | db = dbHelper.getReadableDatabase(); | ||
+ | } catch (Exception ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } | ||
+ | |||
+ | // Define a projection that specifies which columns from the database | ||
+ | // you will actually use after this query. | ||
+ | String[] projection = { | ||
+ | BaseColumns._ID, | ||
+ | DbContract.TeamEntry.COL_NAME, | ||
+ | DbContract.TeamEntry.COL_NUMBER, | ||
+ | DbContract.TeamEntry.COL_RANKING | ||
+ | }; | ||
+ | |||
+ | // Filter results WHERE " | ||
+ | //String selection = DbContract.TeamEntry.COL_NAME + " = ?"; | ||
+ | //String[] selectionArgs = { "My Team" }; | ||
+ | String selection = null; // no filters | ||
+ | String[] selectionArgs = null; // no filters | ||
+ | |||
+ | // How you want the results sorted in the resulting Cursor | ||
+ | //String sortOrder = DbContract.TeamEntry.COL_NUMBER + " ASC"; | ||
+ | String sortOrder = DbContract.TeamEntry.COL_NAME + " ASC"; | ||
+ | |||
+ | Cursor cursor = db.query( | ||
+ | DbContract.TeamEntry.TABLE_NAME, | ||
+ | projection, | ||
+ | // | ||
+ | // | ||
+ | null, // don't group the rows | ||
+ | null, // don't filter by row groups | ||
+ | sortOrder | ||
+ | ); | ||
+ | |||
+ | List< | ||
+ | while(cursor.moveToNext()) { | ||
+ | String name = cursor.getString( cursor.getColumnIndexOrThrow(DbContract.TeamEntry.COL_NAME) ); | ||
+ | arrNames.add(name); | ||
+ | } | ||
+ | cursor.close(); | ||
+ | |||
+ | return arrNames; | ||
+ | } | ||
+ | |||
+ | | ||
{ | { | ||
// Define selection filter | // Define selection filter | ||
Line 222: | Line 307: | ||
} | } | ||
- | public int updateDatabase() | + | public int updateTeamRecords() |
{ | { | ||
SQLiteDatabase db = dbHelper.getWritableDatabase(); | SQLiteDatabase db = dbHelper.getWritableDatabase(); | ||
Line 239: | Line 324: | ||
values, | values, | ||
selection, | selection, | ||
- | selectionArgs); | + | selectionArgs |
+ | | ||
return count; | return count; | ||
Line 250: | Line 336: | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | === MainActivity === | ||
+ | <code java> | ||
+ | public class MainActivity extends AppCompatActivity { | ||
+ | |||
+ | // Const | ||
+ | private static final String TAG = " | ||
+ | |||
+ | // Properties | ||
+ | ListView lstTeams; | ||
+ | String[] arrTeams; | ||
+ | List< | ||
+ | |||
+ | @Override | ||
+ | protected void onCreate(Bundle savedInstanceState) | ||
+ | { | ||
+ | super.onCreate(savedInstanceState); | ||
+ | setContentView(R.layout.activity_main); | ||
+ | |||
+ | // Connect to resources | ||
+ | Resources res = getResources(); | ||
+ | lstTeams = (ListView) findViewById(R.id.lstTeams); | ||
+ | //arrTeams = res.getStringArray(R.array.teams); | ||
+ | |||
+ | try { | ||
+ | DbUtils dbutil = new DbUtils(); | ||
+ | dbutil.getConnection(this); | ||
+ | teamNames = dbutil.findTeamNamesAll(); | ||
+ | } catch (Exception ex) { | ||
+ | ex.printStackTrace(); | ||
+ | } | ||
+ | // Using custom ItemAdapter with custom layout | ||
+ | TeamItemAdapter itemAdapter = new TeamItemAdapter( | ||
+ | this, // context | ||
+ | // | ||
+ | teamNames | ||
+ | ); | ||
+ | lstTeams.setAdapter(itemAdapter); | ||
+ | |||
+ | lstTeams.setOnItemClickListener(new AdapterView.OnItemClickListener() { | ||
+ | @Override | ||
+ | public void onItemClick(AdapterView<?> | ||
+ | Log.d(TAG, " | ||
+ | Toast.makeText(getApplicationContext(), | ||
+ | |||
+ | Intent showRubricActivity = new Intent(getApplicationContext(), | ||
+ | showRubricActivity.putExtra(" | ||
+ | showRubricActivity.putExtra(" | ||
+ | startActivity(showRubricActivity); | ||
+ | } | ||
+ | }); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | activiy_main.xml Layout: | ||
+ | <code xml> | ||
+ | <?xml version=" | ||
+ | < | ||
+ | xmlns: | ||
+ | xmlns: | ||
+ | android: | ||
+ | android: | ||
+ | tools: | ||
+ | |||
+ | < | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | android: | ||
+ | app: | ||
+ | app: | ||
+ | app: | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | === ItemAdapter === | ||
+ | <code java> | ||
+ | package com.acme.myapp; | ||
+ | |||
+ | import android.content.Context; | ||
+ | import android.view.LayoutInflater; | ||
+ | import android.view.View; | ||
+ | import android.view.ViewGroup; | ||
+ | import android.widget.BaseAdapter; | ||
+ | import android.widget.TextView; | ||
+ | |||
+ | import java.util.List; | ||
+ | |||
+ | public class TeamItemAdapter extends BaseAdapter { | ||
+ | LayoutInflater mInflater; | ||
+ | |||
+ | //String[] arrTeams; | ||
+ | List< | ||
+ | |||
+ | //public TeamItemAdapter(Context cx, String[] teams) | ||
+ | public TeamItemAdapter(Context cx, List< | ||
+ | { | ||
+ | arrTeams = teams; | ||
+ | |||
+ | mInflater = (LayoutInflater) cx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | //public int getCount() | ||
+ | //{ | ||
+ | // return arrTeams.length; | ||
+ | //} | ||
+ | public int getCount() | ||
+ | { | ||
+ | return arrTeams.size(); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | //public Object getItem(int idx) | ||
+ | //{ | ||
+ | // return arrTeams[idx]; | ||
+ | //} | ||
+ | public Object getItem(int idx) | ||
+ | { | ||
+ | return arrTeams.get(idx); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public long getItemId(int idx) | ||
+ | { | ||
+ | return idx; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public View getView(int idx, View view, ViewGroup viewGroup) | ||
+ | { | ||
+ | // Use layout inflater. | ||
+ | |||
+ | View vw = mInflater.inflate(R.layout.listview_detail_team, | ||
+ | TextView lblItemNumber = (TextView) vw.findViewById(R.id.lblItemNumber); | ||
+ | TextView lblTeamName = (TextView) vw.findViewById(R.id.lblTeamName); | ||
+ | |||
+ | String strItemNumber = (idx+1) + ""; | ||
+ | //String strTeam = arrTeams[idx]; | ||
+ | String strTeam = arrTeams.get(idx); | ||
+ | |||
+ | lblItemNumber.setText(strItemNumber); | ||
+ | lblTeamName.setText(strTeam); | ||
+ | |||
+ | return vw; | ||
+ | } | ||
} | } | ||
</ | </ | ||
Line 258: | Line 495: | ||
* [[https:// | * [[https:// | ||
* [[https:// | * [[https:// | ||
+ | * [[https:// |