Проблема с БД
Добавлено: 25 дек 2014, 17:34
Доброго времени суток. Мучаюсь уже несколько дней, никак не могу найти ошибку. Буду очень признательным за помощь.
Вот код для создания БД и т.д.
Код для Активити, где заполняется БД и ее вывод(тест)
Чую, что ошибка ясна и пахуча.
Вот код для создания БД и т.д.
Код: Выделить всё
public class DatabaseHelper extends SQLiteOpenHelper implements BaseColumns {
private static final String DATABASE_NAME = "mydatabase.db";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_TABLE = "user_information";
public static final String COLUMN_ID = "_id";
public static final String USER_SEX = "sex";
public static final String USER_WEIGHT = "user_weight";
public static final String USER_HEIGHT = "user height";
public static final String USER_NAME = "user_name";
public static final String USER_SECONDNAME = "user_secondname";
public static final String USER_DATA = "user_data";
public static final String USER_HEALTHNUMBER= "user_healthnumber";
public static final String USER_BLOOD = "user_blood";
private static final String DATABASE_CREATE_SCRIPT = "create table "
+ DATABASE_TABLE + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + USER_SEX + " text not null, " + USER_NAME
+ " text not null, " + USER_SECONDNAME + " text not null, "
+ USER_WEIGHT + " integer, " + USER_HEIGHT + " integer, " + USER_BLOOD + " text not null, "
+ USER_HEALTHNUMBER + " integer, "
+ USER_DATA + " integer" + ");";
public DatabaseHelper(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(DATABASE_CREATE_SCRIPT);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
// Запишем в журнал
Log.w("SQLite", "Обновляемся с версии " + oldVersion + " на версию " + newVersion);
// Удаляем старую таблицу и создаём новую
db.execSQL("DROP TABLE IF IT EXIST " + DATABASE_TABLE);
// Создаём новую таблицу
onCreate(db);
}
}
Код для Активити, где заполняется БД и ее вывод(тест)
Код: Выделить всё
public class FirstScreenActivity extends Activity {
EditText editName, editSecondName, editData, editHeight, editWeight, editBlood, editHealthInsuranceNumber;
TextView textName, textSecondName, textData, textHeight, textWeight,textBlood, textHealthInsuranceNumber, textSex;
String name, secondname, data, height, weight, blood, healthnumber, sex;
Spinner spinnerSex;
String dbd ="0" ;
Button savePersonalBase;
DatabaseHelper databaseHp;
SQLiteDatabase user;
@Override
protected void onCreate(Bundle savedInstaceState){
super.onCreate(savedInstaceState);
setContentView(R.layout.activity_firstscreen);
databaseHp = new DatabaseHelper(this);
user = databaseHp.getWritableDatabase();
editName = (EditText)findViewById(R.id.editName);
editSecondName = (EditText)findViewById(R.id.editSecondName);
editData = (EditText)findViewById(R.id.editData);
editHeight = (EditText)findViewById(R.id.editGrowth);
editWeight = (EditText)findViewById(R.id.editWeight);
editBlood = (EditText)findViewById(R.id.editBlood);
editHealthInsuranceNumber = (EditText)findViewById(R.id.editStrahovka);
textName = (TextView)findViewById(R.id.textName);
textSecondName = (TextView)findViewById(R.id.textSecondName);
textData = (TextView)findViewById(R.id.textData);
textHeight = (TextView)findViewById(R.id.textGrowth);
textWeight = (TextView)findViewById(R.id.textWeight);
textBlood = (TextView)findViewById(R.id.textBlood);
textHealthInsuranceNumber = (TextView)findViewById(R.id.textStrahovka);
textSex = (TextView)findViewById(R.id.textGender);
savePersonalBase = (Button)findViewById(R.id.SaveButton);
spinnerSex = (Spinner)findViewById(R.id.spinnerSex);
}
public void onClickSaveButton(View v){
if (editName.getText().toString().equals("")){
Toast toast = Toast.makeText(getApplicationContext(),
"Пожалуйста, заполните все", Toast.LENGTH_SHORT);
toast.show();
}
else if(editSecondName.getText().toString().equals("")){
Toast toast = Toast.makeText(getApplicationContext(),
"Пожалуйста, заполните все", Toast.LENGTH_SHORT);
toast.show();
}
else if(editData.getText().toString().equals("")){
Toast toast = Toast.makeText(getApplicationContext(),
"Пожалуйста, заполните все", Toast.LENGTH_SHORT);
toast.show();
}
else if(editWeight.getText().toString().equals("")){
Toast toast = Toast.makeText(getApplicationContext(),
"Пожалуйста, заполните все", Toast.LENGTH_SHORT);
toast.show();
}
else if(editHealthInsuranceNumber.getText().toString().equals("")){
Toast toast = Toast.makeText(getApplicationContext(),
"Пожалуйста, заполните все", Toast.LENGTH_SHORT);
toast.show();
}
else if(editHeight.getText().toString().equals("")){
Toast toast = Toast.makeText(getApplicationContext(),
"Пожалуйста, заполните все", Toast.LENGTH_SHORT);
toast.show();
}
else if(editBlood.getText().toString().equals("")){
Toast toast = Toast.makeText(getApplicationContext(),
"Пожалуйста, заполните все", Toast.LENGTH_SHORT);
toast.show();
}
else{
ContentOperation();
}
}
public void ContentOperation(){
ContentValues user_content = new ContentValues();
name = editName.getText().toString();
secondname = editSecondName.getText().toString();
data = editData.getText().toString();
height = editHeight.getText().toString();
weight = editWeight.getText().toString();
blood = editBlood.getText().toString();
healthnumber = editHealthInsuranceNumber.getText().toString();
sex = spinnerSex.getSelectedItem().toString();
user_content.put(DatabaseHelper.USER_NAME, name);
user_content.put(DatabaseHelper.USER_SECONDNAME, secondname);
user_content.put(DatabaseHelper.USER_SEX, sex);
user_content.put(DatabaseHelper.USER_DATA, data);
user_content.put(DatabaseHelper.USER_HEIGHT, height);
user_content.put(DatabaseHelper.USER_WEIGHT, weight);
user_content.put(DatabaseHelper.USER_BLOOD, blood);
user_content.put(DatabaseHelper.USER_HEALTHNUMBER, healthnumber);
user.insert("user_information", null, user_content);
Cursor c = user.query("user_information",null, null, null, null, null, null);
int nameColIndex = c.getColumnIndex(DatabaseHelper.USER_NAME);
int secondnameColIndex = c.getColumnIndex(DatabaseHelper.USER_SECONDNAME);
textHealthInsuranceNumber.setText("name = "+c.getString(nameColIndex) + " Second name = " + c.getString(secondnameColIndex));
c.close();
}
}