2010/05/12

[Android Dev.] SQLite Database 사용하기

Android 에서 SQLite Database 를 사용하려면 SQLiteOpenHelper, SQLiteDatabase 클래스를 상속받아 구현을 해야 히지만 복잡하고 귀찮아서 그냥 Activity 안에 구현해 보았다.





Activity 의 Layout 은 버튼하나와 수행 결과를 보여 줄 TextView 만 배치하였다.

<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/BtnTestSQLite"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Test SQLite"
/>
<TextView
android:id="@+id/TxtResult"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>



버튼이 눌려지만 Database 를 테스트하는 명령이 수행되고 그 결과를 TextView에 출력 하였다.

@Override
public void onClick(View v)
{
// 데이터베이스 열기 (생성)
m_db = openOrCreateDatabase( "test.db", Context.MODE_PRIVATE, null );

if ( !m_db.isOpen() )
{
Log.e( "SQLite", "openOrCreateDatabase ... Fail" );
return;
}

Log.i( "SQLite", "openOrCreateDatabase ... OK" );

String strSQL;

try
{
// member 테이블이 존재하면 삭제
strSQL = "DROP TABLE IF EXISTS member;";
m_db.execSQL( strSQL );

// member 테이블이 존재하지 않으면 생성
strSQL =
"CREATE TABLE IF NOT EXISTS member " +
"(" +
" c_name TEXT" +
" , c_alias TEXT" +
" , c_age INTEGER" +
");";
m_db.execSQL( strSQL );
Log.i( "SQLite", "Create Table ... OK" );

// Insert 구문을 이용한 Row 삽입
for ( int i = 0; i < 3; ++i )
{
strSQL =
"INSERT INTO member ( c_name, c_alias, c_age )" +
" VALUES ( 'code_" + Integer.toString( i ) + "'," +
" 'test', " + Integer.toString( i + 2 ) +
" );";
m_db.execSQL( strSQL );

Log.i( "SQLite", "Insert data " + i + " ... OK" );
}

// ContentValues 를 이용한 데이터 삽입
ContentValues cvInsert = new ContentValues();
cvInsert.put( "c_name", "neo" );
cvInsert.put( "c_alias", "dreamer" );
cvInsert.put( "c_age", "20" );
m_db.insert( "member", null, cvInsert );

cvInsert.put( "c_name", "neo2" );
cvInsert.put( "c_alias", "dreamer2" );
cvInsert.put( "c_age", "40" );
m_db.insert( "member", null, cvInsert );

// rawQuery 함수를 이용한 데이터 질의
m_cursor = m_db.rawQuery( "SELECT * FROM member", null );
if ( m_cursor != null )
{
if ( m_cursor.moveToFirst() )
{
String strRow = "--------------------------------\n";
for(int i = 0 ; i < m_cursor.getColumnCount() ; i++ )
{
strRow += m_cursor.getColumnName(i) + " | ";
}
strRow += "\n";
txtResult.setText( strRow );

do
{
strRow = "";
for(int i = 0 ; i < m_cursor.getColumnCount() ; i++ )
{
strRow += m_cursor.getString(i) + " | ";
}
strRow += "\n";
txtResult.setText( txtResult.getText() + strRow );
} while ( m_cursor.moveToNext() );
}
}
m_cursor.close(); // 커서 닫기

// rawQuery 함수에 parameter 를 이용한 데이터 질의
String strParam[] = { "neo" };
m_cursor = m_db.rawQuery( "SELECT * FROM member WHERE c_name = ?", strParam );
if ( m_cursor != null )
{
if ( m_cursor.moveToFirst() )
{
String strRow = "--------------------------------\n";
for(int i = 0 ; i < m_cursor.getColumnCount() ; i++ )
{
strRow += m_cursor.getColumnName(i) + " | ";
}
strRow += "\n";
txtResult.setText( txtResult.getText() + strRow );

do
{
strRow = "";
for(int i = 0 ; i < m_cursor.getColumnCount() ; i++ )
{
strRow += m_cursor.getString(i) + " | ";
}
strRow += "\n";
txtResult.setText( txtResult.getText() + strRow );
} while ( m_cursor.moveToNext() );
}
}
m_cursor.close(); // 커서 닫기

// query 함수를 이용할 데이터 질의
String strColumn[] = { "c_name", "c_age" };
String strSelection = "c_name like 'neo%'";
m_cursor = m_db.query( "member", strColumn, strSelection,
null, null, null, null );
if ( m_cursor != null )
{
if ( m_cursor.moveToFirst() )
{
String strRow = "--------------------------------\n";
for(int i = 0 ; i < m_cursor.getColumnCount() ; i++ )
{
strRow += m_cursor.getColumnName(i) + " | ";
}
strRow += "\n";
txtResult.setText( txtResult.getText() + strRow );

do
{
strRow = "";
for(int i = 0 ; i < m_cursor.getColumnCount() ; i++ )
{
strRow += m_cursor.getString(i) + " | ";
}
strRow += "\n";
txtResult.setText( txtResult.getText() + strRow );
} while ( m_cursor.moveToNext() );
}
}
m_cursor.close(); // 커서 닫기
}
catch ( SQLException e )
{
// SQL 예외 처리
Log.e( "SQLException", e.getMessage() );
}
finally
{
// Database 닫기
m_db.close();
Log.i( "SQLite", "Database Close ... OK" );
}
}


Log를 통해 진행 상황을 모니터링 한 내용이다.

05-12 23:26:20.110: INFO/SQLite(213): openOrCreateDatabase ... OK
05-12 23:26:20.190: INFO/SQLite(213): Create Table ... OK
05-12 23:26:20.200: INFO/SQLite(213): Insert data 0 ... OK
05-12 23:26:20.210: INFO/SQLite(213): Insert data 1 ... OK
05-12 23:26:20.240: INFO/SQLite(213): Insert data 2 ... OK
05-12 23:26:20.400: INFO/SQLite(213): Database Close ... OK

<

Original Post : http://neodreamer-dev.tistory.com/437

No comments :

Post a Comment