parent
ef85815469
commit
0724d1ae3b
@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.hmkcode.android"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0" >
|
||||
|
||||
<uses-sdk
|
||||
android:minSdkVersion="8"
|
||||
android:targetSdkVersion="17" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@drawable/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:theme="@style/AppTheme" >
|
||||
<activity
|
||||
android:name="com.hmkcode.android.MainActivity"
|
||||
android:label="@string/app_name" >
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
|
||||
</manifest>
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 50 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 7.5 KiB |
@ -0,0 +1,16 @@
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:paddingBottom="@dimen/activity_vertical_margin"
|
||||
android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||
android:paddingTop="@dimen/activity_vertical_margin"
|
||||
tools:context=".MainActivity" >
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/hello_world" />
|
||||
|
||||
</RelativeLayout>
|
||||
@ -0,0 +1,9 @@
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
|
||||
|
||||
<item
|
||||
android:id="@+id/action_settings"
|
||||
android:orderInCategory="100"
|
||||
android:showAsAction="never"
|
||||
android:title="@string/action_settings"/>
|
||||
|
||||
</menu>
|
||||
@ -0,0 +1,7 @@
|
||||
<resources>
|
||||
|
||||
<!-- Default screen margins, per the Android Design guidelines. -->
|
||||
<dimen name="activity_horizontal_margin">16dp</dimen>
|
||||
<dimen name="activity_vertical_margin">16dp</dimen>
|
||||
|
||||
</resources>
|
||||
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
|
||||
<string name="app_name">SQLite</string>
|
||||
<string name="action_settings">Settings</string>
|
||||
<string name="hello_world">Hello world!</string>
|
||||
|
||||
</resources>
|
||||
@ -0,0 +1,20 @@
|
||||
<resources>
|
||||
|
||||
<!--
|
||||
Base application theme, dependent on API level. This theme is replaced
|
||||
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
|
||||
-->
|
||||
<style name="AppBaseTheme" parent="android:Theme.Light">
|
||||
<!--
|
||||
Theme customizations available in newer API levels can go in
|
||||
res/values-vXX/styles.xml, while customizations related to
|
||||
backward-compatibility can go here.
|
||||
-->
|
||||
</style>
|
||||
|
||||
<!-- Application theme. -->
|
||||
<style name="AppTheme" parent="AppBaseTheme">
|
||||
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
|
||||
</style>
|
||||
|
||||
</resources>
|
||||
@ -0,0 +1,42 @@
|
||||
package com.hmkcode.android;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.hmkcode.android.model.Book;
|
||||
import com.hmkcode.android.sqlite.MySQLiteHelper;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.app.Activity;
|
||||
|
||||
|
||||
public class MainActivity extends Activity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
|
||||
MySQLiteHelper db = new MySQLiteHelper(this);
|
||||
|
||||
/**
|
||||
* CRUD Operations
|
||||
* */
|
||||
// add Books
|
||||
db.addBook(new Book("Android Application Development Cookbook", "Wei Meng Lee"));
|
||||
db.addBook(new Book("Android Programming: The Big Nerd Ranch Guide", "Bill Phillips and Brian Hardy"));
|
||||
db.addBook(new Book("Learn Android App Development", "Wallace Jackson"));
|
||||
|
||||
// get all books
|
||||
List<Book> list = db.getAllBooks();
|
||||
|
||||
// delete one book
|
||||
db.deleteBook(list.get(0));
|
||||
|
||||
// get all books
|
||||
db.getAllBooks();
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,46 @@
|
||||
package com.hmkcode.android.model;
|
||||
|
||||
|
||||
|
||||
public class Book {
|
||||
|
||||
private int id;
|
||||
private String title;
|
||||
private String author;
|
||||
|
||||
public Book(){}
|
||||
|
||||
public Book(String title, String author) {
|
||||
super();
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Book [id=" + id + ", title=" + title + ", author=" + author
|
||||
+ "]";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,185 @@
|
||||
package com.hmkcode.android.sqlite;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import com.hmkcode.android.model.Book;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.database.sqlite.SQLiteOpenHelper;
|
||||
import android.util.Log;
|
||||
|
||||
public class MySQLiteHelper extends SQLiteOpenHelper {
|
||||
|
||||
// Database Version
|
||||
private static final int DATABASE_VERSION = 1;
|
||||
// Database Name
|
||||
private static final String DATABASE_NAME = "BookDB";
|
||||
|
||||
public MySQLiteHelper(Context context) {
|
||||
super(context, DATABASE_NAME, null, DATABASE_VERSION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(SQLiteDatabase db) {
|
||||
// SQL statement to create book table
|
||||
String CREATE_BOOK_TABLE = "CREATE TABLE books ( " +
|
||||
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
|
||||
"title TEXT, "+
|
||||
"author TEXT )";
|
||||
|
||||
// create books table
|
||||
db.execSQL(CREATE_BOOK_TABLE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||
// Drop older books table if existed
|
||||
db.execSQL("DROP TABLE IF EXISTS books");
|
||||
|
||||
// create fresh books table
|
||||
this.onCreate(db);
|
||||
}
|
||||
//---------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* CRUD operations (create "add", read "get", update, delete) book + get all books + delete all books
|
||||
*/
|
||||
|
||||
// Books table name
|
||||
private static final String TABLE_BOOKS = "books";
|
||||
|
||||
// Books Table Columns names
|
||||
private static final String KEY_ID = "id";
|
||||
private static final String KEY_TITLE = "title";
|
||||
private static final String KEY_AUTHOR = "author";
|
||||
|
||||
private static final String[] COLUMNS = {KEY_ID,KEY_TITLE,KEY_AUTHOR};
|
||||
|
||||
public void addBook(Book book){
|
||||
Log.d("addBook", book.toString());
|
||||
// 1. get reference to writable DB
|
||||
SQLiteDatabase db = this.getWritableDatabase();
|
||||
|
||||
// 2. create ContentValues to add key "column"/value
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(KEY_TITLE, book.getTitle()); // get title
|
||||
values.put(KEY_AUTHOR, book.getAuthor()); // get author
|
||||
|
||||
// 3. insert
|
||||
db.insert(TABLE_BOOKS, // table
|
||||
null, //nullColumnHack
|
||||
values); // key/value -> keys = column names/ values = column values
|
||||
|
||||
// 4. close
|
||||
db.close();
|
||||
}
|
||||
|
||||
public Book getBook(int id){
|
||||
|
||||
// 1. get reference to readable DB
|
||||
SQLiteDatabase db = this.getReadableDatabase();
|
||||
|
||||
// 2. build query
|
||||
Cursor cursor =
|
||||
db.query(TABLE_BOOKS, // a. table
|
||||
COLUMNS, // b. column names
|
||||
" id = ?", // c. selections
|
||||
new String[] { String.valueOf(id) }, // d. selections args
|
||||
null, // e. group by
|
||||
null, // f. having
|
||||
null, // g. order by
|
||||
null); // h. limit
|
||||
|
||||
// 3. if we got results get the first one
|
||||
if (cursor != null)
|
||||
cursor.moveToFirst();
|
||||
|
||||
// 4. build book object
|
||||
Book book = new Book();
|
||||
book.setId(Integer.parseInt(cursor.getString(0)));
|
||||
book.setTitle(cursor.getString(1));
|
||||
book.setAuthor(cursor.getString(2));
|
||||
|
||||
Log.d("getBook("+id+")", book.toString());
|
||||
|
||||
// 5. return book
|
||||
return book;
|
||||
}
|
||||
|
||||
// Get All Books
|
||||
public List<Book> getAllBooks() {
|
||||
List<Book> books = new LinkedList<Book>();
|
||||
|
||||
// 1. build the query
|
||||
String query = "SELECT * FROM " + TABLE_BOOKS;
|
||||
|
||||
// 2. get reference to writable DB
|
||||
SQLiteDatabase db = this.getWritableDatabase();
|
||||
Cursor cursor = db.rawQuery(query, null);
|
||||
|
||||
// 3. go over each row, build book and add it to list
|
||||
Book book = null;
|
||||
if (cursor.moveToFirst()) {
|
||||
do {
|
||||
book = new Book();
|
||||
book.setId(Integer.parseInt(cursor.getString(0)));
|
||||
book.setTitle(cursor.getString(1));
|
||||
book.setAuthor(cursor.getString(2));
|
||||
|
||||
// Add book to books
|
||||
books.add(book);
|
||||
} while (cursor.moveToNext());
|
||||
}
|
||||
|
||||
Log.d("getAllBooks()", books.toString());
|
||||
|
||||
// return books
|
||||
return books;
|
||||
}
|
||||
|
||||
// Updating single book
|
||||
public int updateBook(Book book) {
|
||||
|
||||
// 1. get reference to writable DB
|
||||
SQLiteDatabase db = this.getWritableDatabase();
|
||||
|
||||
// 2. create ContentValues to add key "column"/value
|
||||
ContentValues values = new ContentValues();
|
||||
values.put("title", book.getTitle()); // get title
|
||||
values.put("author", book.getAuthor()); // get author
|
||||
|
||||
// 3. updating row
|
||||
int i = db.update(TABLE_BOOKS, //table
|
||||
values, // column/value
|
||||
KEY_ID+" = ?", // selections
|
||||
new String[] { String.valueOf(book.getId()) }); //selection args
|
||||
|
||||
// 4. close
|
||||
db.close();
|
||||
|
||||
return i;
|
||||
|
||||
}
|
||||
|
||||
// Deleting single book
|
||||
public void deleteBook(Book book) {
|
||||
|
||||
// 1. get reference to writable DB
|
||||
SQLiteDatabase db = this.getWritableDatabase();
|
||||
|
||||
// 2. delete
|
||||
db.delete(TABLE_BOOKS,
|
||||
KEY_ID+" = ?",
|
||||
new String[] { String.valueOf(book.getId()) });
|
||||
|
||||
// 3. close
|
||||
db.close();
|
||||
|
||||
Log.d("deleteBook", book.toString());
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue