Download presentation
Presentation is loading. Please wait.
Published byIwan Kusumo Modified over 6 years ago
1
Mobile Computing With Android ACST 4550 Android Database Storage Part 2
2
Creating a Database You use the SQLiteOpenHelper class to create or open a database. You do this by creating a Java class that extends the SQLiteOpenHelper class. Then in that classes constructor you pass the name to the parent constructor: super(context, “DbName”, null, DB_VERSION); This creates the database if it does not exist already. Then in the onCreate() method, you create the database table like so: db.execSQL("CREATE TABLE " + TableName + " (“ + columNames + “)”); The above text is a standard SQL statement for creating the table and identifying the features of each of the column/field.
3
Creating a Database Next, you call getWritableDatabase() for a database you can modify, or getReadableDatabase() for a read-only database. Both of these calls returns an SQLiteDatabase object. SQLiteDatabase db = getWritableDatabase(); This opens the database, and to close it you just call the close() method.
4
Working with Databases
The methods for modifying your SQLiteDatabase table are as follow: db.insert() <= inserts a new record into a table db.update() <= updates an existing record in a table db.delete() <= deletes an existing record in a table db.query() <= queries the database and returns a list of records The first argument to each of these methods is a string which is the name of the table, like “AddressBook” If you need to provide one or more column name/value pairs, you use the ContentValues class, giving a column name and value using put(): ContentValues values = new ContentValues(); values.put(“ColumnName1”, val1); values.put(“ColumnName2”, val2);
5
Working with Databases
db.insert db.insert(“TableName”,nullableColumn,ContentValues); The first argument is the name of the table. The second argument is a parameter that provides the name of a nullable column name to explicitly insert a NULL into if a null ContentValue name/value pairs is given. The last argument is the set of name/value pairs in a ContentValues object. For example the following inserts a row into the Addressbook table where ID is 456 and the LastName is “Smith”: ContentValues values = new ContentValues(); values.put(“ID”, 456); values.put(“LastName”, “Smith”); db.insert(“AddressBook”, null, values);
6
Working with Databases
db.update db.update(“TableName”,ContentValues,whereClause,whereargs); The first argument is the name of the table. The second argument is the set of name/value pairs in a ContentValues object. The third argument is a Where clause. The fourth argument is an arguments string array for the Where clause. For example the following finds records where ID is 455 or 457 and sets the LastNames of those records to “Smith”: ContentValues values = new ContentValues(); values.put(“LastName”, “Smith”); String wherestr = “ID=? OR ID=?”; String[] argstrs = { “455”, “457” }; db.update(“AddressBook”, values, wherestr, argstrs);
7
Working with Databases
db.delete: db.delete(“TableName”,whereClause,whereArgs); The first argument is the name of the table. The second argument is a Where clause. The third argument is an arguments string array for the Where clause. For example the following deletes records where ID is 455 or 457: String wherestr = “ID=? OR ID=?”; String[] argstrs = { “455”, “457” }; db.delete(“AddressBook”, wherestr, argstrs);
8
Working with Databases
db.query: Cursor c = db.query(“TableName”, ColumnNames, whereClause, whereArgs, groupBy, having, orderBy); The first argument is the table name. The second argument is a string array of the Column Name of the values to return. The third is a Where clause. The fourth is an arguments string array for the Where clause. The fifth is a GroupBy clause. The sixth is a Having clause. The seventh is a OrderBy clause. For example the following gets all IDs and LastNames where ID is 455 or 457, grouped by LastName, as long as no more than 5 LastNames in a group, and ordered by ID: String [] colnames = { “ID”, “LastName” }; String wherestr = “ID=? OR ID=?”; String [] argstrs = { “455”, “457” }; String havingstr = “count(ID) <= 5”; Cursor c = db.query(“AddressBook”, colnames, wherestr, argstrs, “LastName”, havingstr, “ID”);
9
Working with Databases
db.query: Cursor c = db.query(“TableName”, ColumnNames, whereClause, whereArgs, groupBy, having, orderBy); The Cursor is the set of results returned by the query. You can get the amount of results with c.getCount(). Each row returned may have multiple columns of values. You get each by first getting a column index using he column name like so: c.getColumnIndex(String columnName). Then you use the index to get a value of a certain type like c.getLong(int columnIndex) if the value for that column is a long, or if it’s a string you’d use c.getString(int columnIndex). If you don’t have a Where clause, a GroupBy clause, a Having clause, or a OrderBy clause, you can pass nulls for any or all of those values. The following would just get all the IDs and LastNames for all the records: String [] colnames = { “ID”, “LastName” }; Cursor c = db.query(“AddressBook”, colnames, null, null, null, null, null);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.