티스토리 뷰


아래는 걍 예제 소스. DB를 쓴다음에 읽어와서 화면에 뿌린다.

 @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" );
	}
}


자세한건 아래 사이트 참조
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함