Учебное приложение. Создаю базу данных Вот сам класс базы:
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 = "aboonents";
public static final String FIRST_NAME = "First_name";
public static final String SECOND_NAME = "Second_name";
public static final String PATRONYMIC = "Patronymic";
public static final String REGION = "Region";
public static final String ARREARS = "Arrears";
public static final String CONSUMPTION = "Consumption";
public static final String PHONE = "Phone";
private static final String DATABASE_CREATE_SCRIPT = "create table "
+ DATABASE_TABLE + " (" + BaseColumns._ID
+ " integer primary key autoincrement, " +
FIRST_NAME + " text not null, " +
SECOND_NAME + " text not null, " +
PATRONYMIC + " text not null, " +
REGION + " text not null, " +
ARREARS + " text not null, " +
CONSUMPTION + " text not null, " +
PHONE + " integer);";
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,
int version, DatabaseErrorHandler errorHandler) {
super(context, name, factory, version, errorHandler);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE_SCRIPT);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Создание базы и занесение значений.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit_1 = (EditText) findViewById(R.id.editText_1);
text_1 = (TextView) findViewById(R.id.text_1);
button_1 = (Button) findViewById(R.id.button);
mDatabaseHelper = new DatabaseHelper(this, "mydatabase.db", null, 1);
mSqLiteDatabase = mDatabaseHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DatabaseHelper.FIRST_NAME, "firstname");
values.put(DatabaseHelper.SECOND_NAME, "secondname");
values.put(DatabaseHelper.PATRONYMIC, "patronymic");
values.put(DatabaseHelper.REGION, "region");
values.put(DatabaseHelper.ARREARS, "arrears");
values.put(DatabaseHelper.CONSUMPTION, "consumption");
values.put(DatabaseHelper.PHONE , "phone");
mSqLiteDatabase.insert("db1", null, values);
Далее, после нажатия кнопки:
case R.id.button_2:
Cursor cursor = mSqLiteDatabase.query("cats", new String[]
{ DatabaseHelper.FIRST_NAME,
DatabaseHelper.SECOND_NAME,
DatabaseHelper.PATRONYMIC,
DatabaseHelper.REGION,
DatabaseHelper.ARREARS,
DatabaseHelper.CONSUMPTION,
DatabaseHelper.PHONE},
null, null,
null, null, null);
cursor.moveToFirst();
String firstname = cursor.getString(cursor.getColumnIndex(DatabaseHelper.FIRST_NAME));
String secondname = cursor.getString(cursor.getColumnIndex(DatabaseHelper.SECOND_NAME));
String patronymic = cursor.getString(cursor.getColumnIndex(DatabaseHelper.PATRONYMIC));
String region = cursor.getString(cursor.getColumnIndex(DatabaseHelper.REGION));
String arrears = cursor.getString(cursor.getColumnIndex(DatabaseHelper.ARREARS));
String consumption = cursor.getString(cursor.getColumnIndex(DatabaseHelper.CONSUMPTION));
String phone = cursor.getString(cursor.getColumnIndex(DatabaseHelper.PHONE));
text_1.setText(firstname);
cursor.close();
break;
Ловлю ошибку =(
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.xxxxxxxx.admin.the_address_book, PID: xxxx java.lang.IllegalStateException: Could not execute method for android:onClick
Уже второй день как не могу разобраться, как работать с базами данных=(