Educational application. I create a database Here is the base class itself:

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) { } } 

Creating a database and entering values.

 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); 

Further, after pressing the button:

 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; 

I catch an error = (

E / AndroidRuntime: FATAL EXCEPTION: main Process: com.xxxxxxxx.admin.the_address_book, PID: xxxx java.lang.IllegalStateException: Could not execute method for android: onClick

Already the second day I can't figure out how to work with databases = (

  • Your problem is not related to SQLLite and seems to be related to the fact that you specified onClick attribute in the markup but did not implement it. - pavlofff

2 answers 2

Here is this piece of code.

 Cursor cursor = mSqLiteDatabase.query("cats", new String[] { DatabaseHelper.FIRST_NAME, 

You have access to the cats table, but in the initialization code I do not see the creation of the cats table. If I'm right, then instead of cats should be db1

  • The query specifies the table for the sample, not the base. Did you read the mistake itself? - pavlofff
  • Yes, it was so. It is worth forming questions in the morning, not in the evening. - Anton Iskrizhitsky

Maybe you should not work directly with SQLite, it is difficult for beginners. You can use more simple libraries that will give you even more features than SQLite. For example:

Realm

Green dao

perhaps the easiest one is OrmLite

can still read this post