마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

SQLite 삽입 삭제 기능이 안됩니다.. 왜이런건가요?

0 추천
package com.example.sqlite;

import java.util.ArrayList;
import java.util.HashMap;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SimpleAdapter;

public class MainActivity extends ListActivity {
 
 private SQLiteDatabase mDb;
 Cursor mCursor;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  
  mDb = openOrCreateDatabase("mydb1.db",MODE_PRIVATE,null);
  mDb.execSQL("drop table if exists my_table");
  mDb.execSQL("create table my_table(_id integer primary key autoincrement, "+"studentId int not null, score int not null)");
  
  mDb.execSQL("insert into my_table(studentId,score) values (60092418, 100);");
  mDb.execSQL("insert into my_table(studentId,score) values (60092419, 90);");
  mDb.execSQL("insert into my_table(studentId,score) values (60092420, 80);");
  
  mCursor = mDb.query(
    "my_table",
    new String[] {"studentId", "score"},
    null,
    null,
    null,
    null,
    "_id",
    "5");
  ArrayList<HashMap<String,String>> mList = new ArrayList<HashMap<String,String>>();
  if(mCursor != null){
   if(mCursor.moveToFirst()){
    do{
     HashMap<String,String> item = new HashMap<String,String>();
     for (int j=0; j<mCursor.getColumnCount(); j++){
      item.put(mCursor.getColumnName(j), mCursor.getString(j));
     }
     mList.add(item);
    }while(mCursor.moveToNext());
   }
  }
  
  SimpleAdapter adapter = new SimpleAdapter(this,mList,android.R.layout.simple_list_item_2,
    new String[] {"studentId","score"}, new int[]{android.R.id.text1, android.R.id.text2});
  setListAdapter(adapter);
  setContentView(R.layout.activity_main);
  
  Button insert = (Button)findViewById(R.id.insert);
  Button delete = (Button)findViewById(R.id.delete);
  insert.setOnClickListener(new OnClickListener(){
   public void onClick(View v){
    if(v.getId()==R.id.insert){
     mDb.execSQL("insert into my_table(studentId,score) values (0, 0);");
     
    }
   }
  });
  delete.setOnClickListener(new OnClickListener(){
   public void onClick(View v){
    if(v.getId()==R.id.delete){
     mDb.execSQL("delete from my_table where _id=1;");
    }
   }
  });
  
  
  
  
  
  
 }


 @Override
 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;
 }

}

엊그제 SQLite에 대해서 간단하게 배웠습니다.

배운걸 이용해서 간단하게 첫번째 데이터를 지우는 기능과

데이터를 입력하는 기능을 가진 버튼 두개를 넣어 보았는데, 제대로 작동하지를 않네요..

처음 앱 실행했을때 기본 테이블이 제대로 뜨기는하는데... 왜이런건가요?

Elaha (150 포인트) 님이 2013년 10월 30일 질문

1개의 답변

0 추천
mDb.getWritableDatabase();
mDb.getReadableDatabase();
 
위 속성을 먼저 주고 실행해보세요... 
인연 (31,880 포인트) 님이 2013년 11월 13일 답변
...