package com.hannam.project.sqlite;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
public class ActivityAndroidSQLite3 extends Activity {
private EditText title = null;
private EditText roomNum = null;
private EditText showMe = null;
private Spinner time_from = null;
private Spinner time_to = null;
private final String db_file_name = "data.db";
private final int db_version = 1;
private DBHelper dbHelper = null;
private SQLiteDatabase sqliteDB = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
title = (EditText) findViewById(R.id.title);
roomNum = (EditText) findViewById(R.id.room);
time_from = (Spinner) findViewById(R.id.time_from);
time_to = (Spinner) findViewById(R.id.time_to);
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this,
R.array.times, android.R.layout.simple_spinner_item);
adapter1
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
time_from.setAdapter(adapter1);
ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this,
R.array.times, android.R.layout.simple_spinner_item);
adapter2
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
time_to.setAdapter(adapter2);
Button saveDB = (Button) findViewById(R.id.Button_saveDB);
saveDB.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
writeDB(title.getText().toString(), roomNum.getText()
.toString(), time_from.getAdapter().toString(),
time_to.getAdapter().toString());
} catch (Exception e) {
e.printStackTrace();
}
}
});
showMe = (EditText) findViewById(R.id.et_queryprint);
Button readDB = (Button) findViewById(R.id.Button_readDB);
readDB.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String str = "";
Cursor cursor = null;
try {
cursor = readDB(title.getText().toString(), roomNum
.getText().toString(), time_from.getAdapter()
.toString(), time_to.getAdapter().toString());
cursor.moveToFirst();
for (int i = 0; i < cursor.getColumnCount(); i++) {
str += cursor.getColumnName(i) + " : "
+ cursor.getString(i) + " ";
}
showMe.setText(str);
} catch (Exception e) {
// TODO: handle exception
} finally {
cursor.close();
}
}
});
this.dbHelper = new DBHelper(this);
this.sqliteDB = this.dbHelper.getWritableDatabase();
}
private Cursor readDB(String title, String roomNum, String time_from, String time_to) {
Cursor cursor = this.sqliteDB.query("MONDAY", new String[] { "title",
"room" , "time_from", "time_to"}, "title = ? and room = ? and time_from = ? and time_to = ?",
new String[] { title, roomNum, time_from,time_to }, null, null, null, null);
return cursor;
}
private void writeDB(String title, String roomNum, String time_from,
String time_to) throws Exception {
ContentValues cv = new ContentValues();
cv.put("title", title);
cv.put("roomNum", roomNum);
cv.put("time_from", time_from);
cv.put("time_to", time_to);
long colNum = this.sqliteDB.insert("MONDAY", "", cv);
}
class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, db_file_name, null, db_version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table if not exists MONDAY (title VARCHAR(20) , room CARCHAR(20), time_from VARCHAR(10) , time_to VARCHAR(10))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists MONDAY");
onCreate(db);;
}
@Override
public synchronized void close() {
super.close();
}
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
db.execSQL("create table if not exists MONDAY (title VARCHAR(20) , room CARCHAR(20), time_from VARCHAR(10) , time_to VARCHAR(10))");
}
}
}
---------------------------------------------------------- main ---------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="과 목 명 : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15px">
</TextView>
<EditText
android:id="@+id/title"
android:bufferType="editable"
android:textColor="#707070"
android:textSize="20px"
android:layout_width="200px"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="강 의 실 : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15px">
</TextView>
<EditText
android:id="@+id/room"
android:bufferType="editable"
android:textColor="#707070"
android:textSize="10px"
android:layout_width="200px"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="시 간"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15px">
</TextView>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="from : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15px">
</TextView>
<Spinner
android:id="@+id/time_from"
android:drawSelectorOnTop="false"
android:layout_width="200px"
android:layout_height="wrap_content">
</Spinner>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="to : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15px">
</TextView>
<Spinner
android:id="@+id/time_to"
android:drawSelectorOnTop="false"
android:layout_width="200px"
android:layout_height="wrap_content">
</Spinner>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:text="저장"
android:id="@+id/Button_saveDB"
android:textSize="15px"
android:gravity="fill"
android:layout_width="82px"
android:layout_height="wrap_content">
</Button>
<Button
android:text="읽기"
android:id="@+id/Button_readDB"
android:textSize="15px"
android:gravity="fill"
android:layout_width="82px"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="@+id/et_queryprint"
android:bufferType="editable"
android:textColor="#707070"
android:textSize="10px"
android:layout_width="200px"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
</LinearLayout>