2013-05-29 2 views
0

당신이 당신의 이름을 입력하고 2 개의 텍스트를 사용하여 표시를하고 버튼을 누르면 데이터베이스를 저장하고 데이터베이스 결과를 보여주는 두 번째 활동으로 이동하는 간단한 데이터베이스 애플리케이션을 작성한다. 데이터베이스에는 ID, 이름, 표시, 작업 의 4 개의 열이 있습니다. 여기서 "operations"열은 이미 프로그램에 입력 된 ops의 결과를 가져옵니다.SQLite 안드로이드는 내가 어디가 잘못되었는지 알아낼 수 없다.

삽입 할 때 try and catch를 입력하면 성공 여부를 알려주는 대화 상자가 나타납니다. 결과는 성공이지만 활동을보기 위해 이동할 때 앱이 강제 종료됩니다. 여기 내 데이터베이스 클래스 코드 여기

public class DateBase { 

public static final String KEY_ID = "id"; 
public static final String KEY_NAME = "user_name"; 
public static final String KEY_MARK = "user_mark"; 
public static final String KEY_OPERATION = "operation"; 

private static final String DATABASE_NAME = "Statistic"; 
private static final String DATABASE_TABLE = "user_interface"; 
private static final int DATABASE_VERSION = 1; 

private DBInterface MyInterface; 
private final Context MyContext; 
private SQLiteDatabase MyDB; 


private static class DBInterface extends SQLiteOpenHelper 
{ 

    public DBInterface(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + 
       KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
       KEY_NAME + " TEXT NOT NULL, " + 
       KEY_MARK + " TEXT NOT NULL, " + 
       KEY_OPERATION + " TEXT NOT NULL);" 
       ); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
     db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 
     onCreate(db); 
    } 

} 

public DateBase(Context c) 
{ 
    MyContext = c; 
} 

public DateBase open() throws SQLException 
{ 
    MyInterface = new DBInterface(MyContext); 
    MyDB = MyInterface.getWritableDatabase(); 
    return this; 
} 

public void close() 
{ 
    MyInterface.close(); 
} 

public long InsertEntry(String name, String mark, String ops) { 
    // TODO Auto-generated method stub 
    ContentValues cv = new ContentValues(); 
    cv.put(KEY_NAME, name); 
    cv.put(KEY_MARK, mark); 
    cv.put(KEY_OPERATION, ops); 
    return MyDB.insert(DATABASE_TABLE, null, cv); 
    } 

public String getData() { 
    // TODO Auto-generated method stub 
    String[] columns = new String[] { KEY_ID, KEY_NAME, KEY_MARK, KEY_OPERATION}; 
    Cursor c = MyDB.query(DATABASE_TABLE, columns, null, null, null, null, null); 
    String result = ""; 

    int iRow = c.getColumnIndex(KEY_ID); 
    int iName = c.getColumnIndex(KEY_NAME); 
    int iMark = c.getColumnIndex(KEY_MARK); 
    int iOperation = c.getColumnIndex(KEY_OPERATION); 

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) 
    { 
     result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iMark) + " " + c.getString(iOperation) + "\n"; 
    } 


    return result; 
} 
    } 

버튼 clickon 동안 삽입 코드입니다 : 마침내 여기

public void onClick(View v) { 
    // TODO Auto-generated method stub 

    boolean diditwork = true; 
    try {String name = ed1.getText().toString(); 
    String mark = ed2.getText().toString(); 
    String ops = ""; 
    for (int i = 0; i < ops.length(); i++) 
    { 
     switch (op[i]) // int[] op = new int[]{1,3}; 
     { 
     case 1: 
      ops = ops + "+ "; 
      break; 
     case 2: 
      ops = ops + "- "; 
      break; 
     case 3: 
      ops = ops + "* "; 
      break; 
     case 4: 
      ops = ops + "/ "; 
      break; 
     } 
    } 

    DateBase entry = new DateBase(MainActivity.this); 
    entry.open(); 
    entry.InsertEntry(name, mark, ops); 
    entry.close(); 

    } catch (Exception e) 
    { 
     diditwork = false; 
     String error = e.toString(); 
     Dialog d = new Dialog (this); 
     d.setTitle("NO WAY!!"); 
     TextView tv = new TextView(this); 
     tv.setText(error); 
     d.setContentView(tv); 
     d.show(); 
    } finally { 
     if (diditwork) 
     { 
      Dialog d = new Dialog (this); 
      d.setTitle("Heck YA!"); 
      TextView tv = new TextView(this); 
      tv.setText("Success"); 
      d.setContentView(tv); 
      d.show(); 
     } 
    } 

    Intent i = new Intent(); 
    i.setClass(MainActivity.this, DatabaseView.class); 
    startActivity(i); 
    } 

을하고 내 점점 데이터 코드는 다음과 같습니다 공용 클래스 DatabaseView는 활동 {

TextView tv; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.database_view); 

    tv = (TextView) findViewById(R.id.textView1); 

    DateBase info = new DateBase(this); 
    info.open(); 
    String Data = info.getData(); 
    info.close(); 

    tv.setText(Data); 
} 

} 
확장

LogCAT에 대한 예외는 다음과 같습니다.

E/AndroidRuntime(490): FATAL EXCEPTION: main 
    java.lang.RuntimeException: Unable to start activity 
    ComponentInfo{com.database/com.database.DatabaseView}:  
    android.database.sqlite.SQLiteException: no such column: user_mark: , while compiling: SELECT id, user_name, user_mark, operation FROM user_interface 

나는 어디에서 문제인지 파악할 수 없다. 제발 도와주세요 제 나쁜 영어에 대해 미안합니다

시간 내 주셔서 감사합니다.

+0

대답은 매우 명백 ... – JoxTraex

+0

체크 열 이름 user_mark – Senthil

+0

체크 열 이름 user_mark 경우는 동일 여전히 예외가 IT가 동일한 상표 "에 열 이름을 변경하려고하는 열 이름 – Ravi

답변

2

당신이 그렇지 않으면 나는 일이 모든 장소에 꽤 많이있는 시스템이 여전히 응용 프로그램을 제거하려고 기존 데이터베이스를 사용하여 다시 설치되도록이 필드 user_mark 나중에 추가 할 수있다

+0

멋진 답변을 주셔서 감사합니다. :) – Coderji

1

예, 코드에는 잘못이 없다 문제는 Aashish가 말한 것입니다. 따라서 애플리케이션 관리에서 데이터를 지우고 다시 실행하십시오.

관련 문제