Package org.androidtown.database.query; import android.app.Activity; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase;

Slides:



Advertisements
Similar presentations
SQLite is a software library. It is: self-contained + Serverless + zero-configuration transactional = SQL database engine. Most widely deployed. The source.
Advertisements

Android – CoNTENT PRoViders
Android course Database dr Milan Vidaković Chair of Informatics Faculty of Technical Sciences University of Novi Sad.
Cosc 5/4730 Android and Blackberry SQLite. For the sql language syntax, please see SQlite documentation –
@2011 Mihail L. Sichitiu1 Android Introduction Hello World.
Presenting Lists of Data. Lists of Data Issues involved – unknown number of elements – allowing the user to scroll Data sources – most common ArrayList.
Android Application Development Tutorial. Topics Lecture 5 Overview Overview of Networking Programming Tutorial 2: Downloading from the Internet.
SQLite Database. SQLite Public domain database – Advantages Small (about 150 KB) – Used on devices with limited resources Each database contained within.
Data Storage: Part 3 (SQLite)
Social Media Apps Programming Min-Yuh Day, Ph.D. Assistant Professor Department of Information Management Tamkang University
1/29/ Android Programming: FrameLayout By Dr. Ramji M. Makwana Professor and Head, Computer Engineering Department A.D. Patel.
Android Dialog Boxes AlertDialog - Toast
1 Mobile Computing DataBase and Content Providers Copyright 2014 by Janson Industries EC AssgAssg.
SQLite Android Club SQLite About onCreate Insert Select Update Delete onUpdate.
Android - Broadcast Receivers
Address Book App 1. Define styles   Specify a background for a TextView – res/drawable/textview_border.xml.
Import import android.graphics.Bitmap; import android.widget.ImageView;
로봇 모니터링 1/2 UNIT 20 로봇 SW 콘텐츠 교육원 조용수. 학습 목표 Message Queue Handler 2.
SQLite (part deux) 1 CS440. Traditional Model View Controller (MVC) CS440 2.
Mobile Software Development ISCG 7424 Department of Computing UNITEC John Casey and Richard Rabeder SQLite and Permissions.
SQlite. SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation.
Android and s Ken Nguyen Clayton state University 2012.
Address Book App 1 Fall 2014 CS7020: Game Design and Development.
Android 基本 I/O. 基本 I/O 介面元件 在此節中主要介紹常見的 I/O 使用者介 面元件 – Button, TextView, 以及 EditText , 學習者可以學會: – Android 的視窗表單設計 res/layout/main.xml – Android SDK –
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
Making content providers
Lab7 – Appendix.
Introduction to android
Lab7 – Advanced.
Android Layouts 8 May 2018 S.RENUKADEVI/AP/SCD/ANDROID LAYOUTS 1.
UNIT 11 로봇 전화번호부 3/4 로봇 SW 콘텐츠 교육원 조용수.
GUI Programming Fundamentals
ListView: Part 2.
Reactive Android Development
CS499 – Mobile Application Development
Tashkent Weather ANDROID CLUB 2015.
Android Widgets 1 7 August 2018
Android – Read/Write to External Storage
Picasso Revisted.
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CMPE419 Mobile Application Development
BMI Android Application will take weight and height from the users to calculate Body Mass Index (BMI) with the information, whether user is underweight,
Mobile Computing With Android ACST 4550 Android Database Storage
CIS 470 Mobile App Development
滑動 建國科技大學 資管系 饒瑞佶.
CIS 493/EEC 492 Android Sensor Programming
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Android Developer Fundamentals V2
CIS 470 Mobile App Development
CIS 470 Mobile App Development
CIS 470 Mobile App Development
ListView A view that shows items in a vertically scrolling list. The items come from the ListAdapter associated with this view. ListAdapter is used to.
Adding Components to Activity
BLP 4216 MOBİL UYGULAMA GELİŞTİRME-2
CMPE419 Mobile Application Development
Android Sensor Programming
Android Sensor Programming
CIS 694/EEC 693 Android Sensor Programming
CIS 694/EEC 693 Android Sensor Programming
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

package org.androidtown.database.query; import android.app.Activity; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; /** * 데이터베이스를 조회하는 방법을 알 수 있습니다. * Mike * */ public class MainActivity extends Activity { private TextView status; public static final String TAG = "MainActivity"; private static String DATABASE_NAME = null; private static String TABLE_NAME = "employee"; private static int DATABASE_VERSION = 1; private DatabaseHelper dbHelper; private SQLiteDatabase db; package org.androidtown.database.query; import android.app.Activity; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; /** * 데이터베이스를 조회하는 방법을 알 수 있습니다. * Mike * */ public class MainActivity extends Activity { private TextView status; public static final String TAG = "MainActivity"; private static String DATABASE_NAME = null; private static String TABLE_NAME = "employee"; private static int DATABASE_VERSION = 1; private DatabaseHelper dbHelper; private SQLiteDatabase public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); status = (TextView) findViewById(R.id.status); final EditText input01 = (EditText) findViewById(R.id.input01); Button queryBtn = (Button) findViewById(R.id.queryBtn); queryBtn.setOnClickListener(new OnClickListener() { public void onClick(View v) { DATABASE_NAME = input01.getText().toString(); boolean isOpen = openDatabase(); if (isOpen) { executeRawQuery(); executeRawQueryParam(); } } }); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); status = (TextView) findViewById(R.id.status); final EditText input01 = (EditText) findViewById(R.id.input01); Button queryBtn = (Button) findViewById(R.id.queryBtn); queryBtn.setOnClickListener(new OnClickListener() { public void onClick(View v) { DATABASE_NAME = input01.getText().toString(); boolean isOpen = openDatabase(); if (isOpen) { executeRawQuery(); executeRawQueryParam(); } } }); } 데이터 조회하기 (MainActivity.java)

private void executeRawQueryParam() { println("\nexecuteRawQueryParam called.\n"); String SQL = "select name, age, phone " + " from " + TABLE_NAME + " where age > ?"; String[] args= {"30"}; Cursor c1 = db.rawQuery(SQL, args); int recordCount = c1.getCount(); println("cursor count : " + recordCount + "\n"); for (int i = 0; i < recordCount; i++) { c1.moveToNext(); String name = c1.getString(0); int age = c1.getInt(1); String phone = c1.getString(2); println("Record #" + i + " : " + name + ", " + age + ", " + phone); } c1.close(); } private void executeRawQueryParam() { println("\nexecuteRawQueryParam called.\n"); String SQL = "select name, age, phone " + " from " + TABLE_NAME + " where age > ?"; String[] args= {"30"}; Cursor c1 = db.rawQuery(SQL, args); int recordCount = c1.getCount(); println("cursor count : " + recordCount + "\n"); for (int i = 0; i < recordCount; i++) { c1.moveToNext(); String name = c1.getString(0); int age = c1.getInt(1); String phone = c1.getString(2); println("Record #" + i + " : " + name + ", " + age + ", " + phone); } c1.close(); } private boolean openDatabase() { println("opening database [" + DATABASE_NAME + "]."); dbHelper = new DatabaseHelper(this); db = dbHelper.getWritableDatabase(); return true; } private void executeRawQuery() { println("\n executeRawQuery called.\n"); Cursor c1 = db.rawQuery("select count(*) as Total from " + TABLE_NAME, null); println("cursor count : " + c1.getCount()); c1.moveToNext(); println("record count : " + c1.getInt(0)); c1.close(); } private boolean openDatabase() { println("opening database [" + DATABASE_NAME + "]."); dbHelper = new DatabaseHelper(this); db = dbHelper.getWritableDatabase(); return true; } private void executeRawQuery() { println("\n executeRawQuery called.\n"); Cursor c1 = db.rawQuery("select count(*) as Total from " + TABLE_NAME, null); println("cursor count : " + c1.getCount()); c1.moveToNext(); println("record count : " + c1.getInt(0)); c1.close(); }

private void executeRawQueryParam2() { println("\n executeRawQueryParam2 called.\n"); String[] columns = {"name", "age", "phone"}; String whereStr = "where age > ?"; String[] whereParams = {"30"}; Cursor c1 = db.query(TABLE_NAME, columns, whereStr, whereParams, null, null, null); int recordCount = c1.getCount(); println("cursor count : " + recordCount + "\n"); for (int i = 0; i < recordCount; i++) { c1.moveToNext(); String name = c1.getString(0); int age = c1.getInt(1); String phone = c1.getString(2); println("Record #" + i + " : " + name + ", " + age + ", " + phone); } c1.close(); } private void println(String msg) { Log.d(TAG, msg); status.append("\n" + msg); } groupby 절 having 절 orderby 절

<LinearLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" > <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint=“ 데이터베이스명 " android:text="customer.db" android:textSize="16dp" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=“ 질의수행 " android:textStyle="bold" > <LinearLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" > <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint=“ 데이터베이스명 " android:text="customer.db" android:textSize="16dp" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=“ 질의수행 " android:textStyle="bold" > <ScrollView android:layout_width="match_parent" android:layout_height="match_parent” > <TextView android:layout_width="match_parent“ android:layout_height="match_parent“ android:background="#ff99ccee" android:textColor="#ff0000ff" android:textSize="14dp" > </LinearLayout <ScrollView android:layout_width="match_parent" android:layout_height="match_parent” > <TextView android:layout_width="match_parent“ android:layout_height="match_parent“ android:background="#ff99ccee" android:textColor="#ff0000ff" android:textSize="14dp" > </LinearLayout 데이터 조회하기 (activity_main.xml)

package org.androidtown.database.cursor; import android.app.Activity; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.TextView; public class MainActivity extends Activity { public static final String TAG = "SampleCursorAdapter"; private static String DATABASE_NAME = "employeeDB"; private static String TABLE_NAME = "employee"; private static int DATABASE_VERSION = 1; private DatabaseHelper dbHelper; private SQLiteDatabase db; private TextView public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); status = (TextView) findViewById(R.id.status); ListView list = (ListView) findViewById(R.id.list); package org.androidtown.database.cursor; import android.app.Activity; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.TextView; public class MainActivity extends Activity { public static final String TAG = "SampleCursorAdapter"; private static String DATABASE_NAME = "employeeDB"; private static String TABLE_NAME = "employee"; private static int DATABASE_VERSION = 1; private DatabaseHelper dbHelper; private SQLiteDatabase db; private TextView public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); status = (TextView) findViewById(R.id.status); ListView list = (ListView) findViewById(R.id.list); boolean isOpen = openDatabase(); if (isOpen) { Cursor cursor = executeRawQueryParam(); startManagingCursor(cursor); String[] columns = new String[] {"name", "age", "phone"}; int[] to = new int[] { R.id.name_entry, R.id.age_entry, R.id.phone_entry }; SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.listitem, cursor, columns, to); list.setAdapter(mAdapter); } private boolean openDatabase() { println("opening database [" + DATABASE_NAME + "]."); dbHelper = new DatabaseHelper(this); db = dbHelper.getWritableDatabase(); return true; } boolean isOpen = openDatabase(); if (isOpen) { Cursor cursor = executeRawQueryParam(); startManagingCursor(cursor); String[] columns = new String[] {"name", "age", "phone"}; int[] to = new int[] { R.id.name_entry, R.id.age_entry, R.id.phone_entry }; SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.listitem, cursor, columns, to); list.setAdapter(mAdapter); } private boolean openDatabase() { println("opening database [" + DATABASE_NAME + "]."); dbHelper = new DatabaseHelper(this); db = dbHelper.getWritableDatabase(); return true; } 커서어댑터로 뷰에 보여주기 (MainActivity.java) 액티비티 수명주기에 따라 cursor 를 자동 반응하게 함 액티비티가 onPause() 상태 : 커서 비활성화 onResume() 상태 : 커서 활성화

private Cursor executeRawQueryParam() { println("\nexecuteRawQueryParam called.\n"); String SQL = "select _id, name, age, phone " + " from " + TABLE_NAME + " where age > ?"; String[] args= {"10"}; Cursor c1 = db.rawQuery(SQL, args); return c1; } private class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } private Cursor executeRawQueryParam() { println("\nexecuteRawQueryParam called.\n"); String SQL = "select _id, name, age, phone " + " from " + TABLE_NAME + " where age > ?"; String[] args= {"10"}; Cursor c1 = db.rawQuery(SQL, args); return c1; } private class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } public void onCreate(SQLiteDatabase db) { println("creating table [" + TABLE_NAME + "]."); try { String DROP_SQL = "drop table if exists " + TABLE_NAME; db.execSQL(DROP_SQL); } catch(Exception ex) { Log.e(TAG, "Exception in DROP_SQL", ex); } String CREATE_SQL = "create table " + TABLE_NAME + "(" + " _id integer PRIMARY KEY autoincrement, " + " name text, " + " age integer, " + " phone text)"; try { db.execSQL(CREATE_SQL); } catch(Exception ex) { Log.e(TAG, "Exception in CREATE_SQL", ex); } println("inserting records."); try { db.execSQL( "insert into " + TABLE_NAME + "(name, age, phone) values ('John', 20, ' ');" ); db.execSQL( "insert into " + TABLE_NAME + "(name, age, phone) values ('Mike', 35, ' ');" ); db.execSQL( "insert into " + TABLE_NAME + "(name, age, phone) values ('Sean', 26, ' ');" ); } catch(Exception ex) { Log.e(TAG, "Exception in insert SQL", ex); } public void onCreate(SQLiteDatabase db) { println("creating table [" + TABLE_NAME + "]."); try { String DROP_SQL = "drop table if exists " + TABLE_NAME; db.execSQL(DROP_SQL); } catch(Exception ex) { Log.e(TAG, "Exception in DROP_SQL", ex); } String CREATE_SQL = "create table " + TABLE_NAME + "(" + " _id integer PRIMARY KEY autoincrement, " + " name text, " + " age integer, " + " phone text)"; try { db.execSQL(CREATE_SQL); } catch(Exception ex) { Log.e(TAG, "Exception in CREATE_SQL", ex); } println("inserting records."); try { db.execSQL( "insert into " + TABLE_NAME + "(name, age, phone) values ('John', 20, ' ');" ); db.execSQL( "insert into " + TABLE_NAME + "(name, age, phone) values ('Mike', 35, ' ');" ); db.execSQL( "insert into " + TABLE_NAME + "(name, age, phone) values ('Sean', 26, ' ');" ); } catch(Exception ex) { Log.e(TAG, "Exception in insert SQL", ex); }

public void onOpen(SQLiteDatabase db) { println("opened database [" + DATABASE_NAME + "]."); } public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + "."); db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db); } private void println(String msg) { Log.d(TAG, msg); status.append("\n" + msg); public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }

<LinearLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:layout_width="match_parent" android:layout_height="wrap_content" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff99ccee" android:textColor="#ff0000ff" android:textSize="18dp" > 커서어댑터로 뷰에 보여주기 (activity_main.xml)

<LinearLayout xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" /> 커서어댑터로 뷰에 보여주기 (listitem.xml)