2012-11-05 2 views
0

안드로이드에서 어떻게하면 EditText에서 값을 얻을 수 있고 정수 필드를 가진 데이터베이스에 삽입 할 수 있습니까?안드로이드 데이터베이스에 정수 값을 저장

아래 코드를 사용하고 있지만 정수 데이터를 데이터베이스에 저장하지 않습니다!

EditText Price; 
name = (EditText)findViewById(R.id.txt_GoodsName); 
Price = (EditText)findViewById(R.id.txt_Price); 
db.insertQuote(name.getText().toString(),Price.getText()); 

// ---------------------------------------------------------- 

public long insertQuote(String Quote,int Price1) 
{ 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(GoodName, Quote); 
    initialValues.put(Price, Price1); 
    return db.insert(DATABASE_TABLE, null, initialValues); 
} 
+0

이것은 가능하지 않습니다. 틀린'fieldname' 또는'tablename'을 주셨을지도 모릅니다. –

답변

0

사용

db.insertQuote(name.getText().toString(),Integer.valueOf(Price.getText().toString())); 

또는

db.insertQuote(name.getText().toString(),new Integer(Price.getText().toString())); 

대신

db.insertQuote(name.getText().toString(),Price.getText()); 

db.insertQuote(String,Integer) 때문에 정수로하지 문자열로 두 번째 매개 변수를 받아들입니다.

+0

@hotveryspicy : 예 두 옵션 모두 작동합니다. –

+0

: 당신의 코드를 사용하지만 데이터베이스에 데이터를 저장하지 않습니다 !!! 정수 값을 사용하지 않고 그냥 첫 번째 매개 변수를 저장하면 !!!!하지만 정수를 저장하려고 할 때 (심지어 soloution으로도 작동) – shadi

+0

@shadi : 대답의 두 옵션 모두 String으로 Integer로 변환 할 수 있습니다. 다른 문제가있을 수 있습니다. –

0

시도해보십시오. 함수에서 두 번째 매개 변수가 int가 아니기 때문에 String을 int로 파싱해야합니다.

db.insertQuote(name.getText().toString(),Integer.parseInt(Price.getText().toString())); 
+0

아니요, ContentValues를 사용하면이 문제가 발생하지 않습니다. –

+1

@hotveryspicy : 나는 당신과 동의하지만 String을 Integer로 전달하여 ContentValues가 아닌 insertQuote를 전달합니다 –

+1

그러면 직접 솔루션을 제공하는 대신 잘못된 매개 변수를 호출하는 이유를 알려야합니다. –

관련 문제