SQLite 데이터베이스에 정수 값을 저장하는 데 문제가 있습니다.
내 코드는 다음과 같습니다.EditText의 정수 값을 SQLite 데이터베이스에 저장하는 방법 [Android]
위에서 볼 수 있듯이, 나는에 글고에서 텍스트 값을 저장하기 위해 관리해야
Button btnCreate = (Button)findViewById(R.id.saveButton);
btnCreate.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
calDB.open();
long _id;
EditText txtTitle = (EditText) findViewById(R.id.txtTitle);
String title = txtTitle.getText().toString();
EditText txtLocation = (EditText) findViewById(R.id.editText1);
String location = txtLocation.getText().toString();
EditText startTime = (EditText) findViewById(R.id.editText2);
String startTime = startTime.getText().toString();
_id = calDB.insertEvent(title, location, 0, 0);
calDB.close();
}
});
CalendarAdapter.java (데이터베이스 클래스)
package main.page;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class CalendarAdapter
{
public static final String KEY_ROWID = "_id";
public static final String KEY_TITLE = "title";
public static final String KEY_LOCATION = "location";
public static final String KEY_START = "starttime";
public static final String KEY_END = "endtime";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "anniversary";
private static final String DATABASE_TABLE = "calendar";
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_CREATE = "create table calendar(_id integer primary key autoincrement,"+
"title text not null, location text not null, starttime integer, endtime integer);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public CalendarAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try
{
db.execSQL(DATABASE_CREATE);
}catch(SQLException e)
{
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version "+oldVersion+" to "+newVersion+", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS calendar");
onCreate(db);
}
}
public CalendarAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
public void close()
{
DBHelper.close();
}
public long insertEvent(String title, String location, int starttime, int endtime)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_LOCATION, location);
initialValues.put(KEY_START, starttime);
initialValues.put(KEY_END, endtime);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public boolean deleteEvent(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
public Cursor getAllEvents()
{
return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_LOCATION, KEY_START, KEY_END}, null, null, null, null, null);
}
public Cursor getEvent(long rowId) throws SQLException
{
Cursor c = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_LOCATION, KEY_START, KEY_END}, KEY_ROWID + "=" + rowId, null, null, null, null, null);
if(c != null)
{
c.moveToFirst();
}
return c;
}
public boolean updateEvent(long rowId, String title, String location, int starttime, int endtime)
{
ContentValues args = new ContentValues();
args.put(KEY_TITLE, title);
args.put(KEY_LOCATION, location);
args.put(KEY_START, starttime);
args.put(KEY_END, endtime);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
Create_Events.java의 내 삽입 코드 데이터베이스가 성공적으로. 그래서 지금 데이터베이스에 starttime (Integer)을 저장하려고합니다.
도움이 될 것입니다. 무리 감사!
정확히 무엇이 문제입니까? 'starttime'에 대해 항상 0을 전달하는 것처럼 보입니까? 저장되지 않는다는 것을 어떻게 알 수 있습니까? – cklab
아니요, 텍스트 값을 데이터베이스에 삽입 할 수 있는지 테스트하려는 경우 0을 넣습니다. 데이터베이스에 삽입되지만 editText에서 삽입하려고하면 정수를 삽입 할 수 없습니다. – CallMyName