Моя база данных создана, но таблица не создается. Функция db.insert()
возвращает положительное целое число в журнале, но таблица не создается. Ошибки не отображаются в журналах. Запрос SQL также отлично работает в браузере sqlite. Я использую Android Studio 3.6.
Это мой MainActivity. java
public class MainActivity extends AppCompatActivity {
DbHelper dbHelper;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: called");
dbHelper = new DbHelper(this);
}
public void OpenLibrary(View view){
Log.d(TAG, "OpenLibrary: called");
Intent intent = new Intent(MainActivity.this,LibraryActivity.class);
startActivity(intent);
}
}
Library.class
public class LibraryActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_library);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.lib_menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.createLib:
Intent intent = new Intent(LibraryActivity.this,NewLibrary.class);
startActivity(intent);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
NewLibary.class
public class NewLibrary extends AppCompatActivity {
AppCompatEditText Xet,Yet,Aet,Bet;
AppCompatButton SaveBtn;
DbHelper dbHelper;
SQLiteDatabase sqLiteDatabase;
private static final String TAG = "NewLibrary";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_library);
dbHelper = new DbHelper(this);
Xet = findViewById(R.id.etX);
Yet = findViewById(R.id.etY);
Aet = findViewById(R.id.etA);
Bet = findViewById(R.id.etB);
SaveBtn = findViewById(R.id.Save);
}
public void Save(View view){
int a = Integer.parseInt(Aet.getText().toString());
int b = Integer.parseInt(Bet.getText().toString());
int x = Integer.parseInt(Yet.getText().toString());
int y = Integer.parseInt(Xet.getText().toString());
dbHelper.insertIntoDb(a,b,x,y);
}
}
И, наконец, DbHelper.class
public class DbHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "motionAppDatabase.db";
public static final String VAlUE_TABLE_NAME = "MotionTable";
public static final String VALUE_COLUMN_ID = "id";
public static final String VALUE_COLUMN_A = "a";
public static final String VALUE_COLUMN_B = "b";
public static final String VALUE_COLUMN_X = "x";
public static final String VALUE_COLUMN_Y = "y";
private static final String TAG = "DbHelper";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
Log.d(TAG, "DbHelper: called");
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(TAG, "onCreate: database onCreate() called ");
String CREATE_TABLE_ST = "CREATE TABLE "+VAlUE_TABLE_NAME+
"("+VALUE_COLUMN_ID+" INTEGER PRIMARY KEY,"+VALUE_COLUMN_A+" INTEGER,"
+VALUE_COLUMN_B+" INTEGER,"+VALUE_COLUMN_X +" INTEGER,"+VALUE_COLUMN_Y +" INTEGER"+")";
Log.d(TAG, "onCreate: "+CREATE_TABLE_ST);
db.execSQL(CREATE_TABLE_ST);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.d(TAG, "onUpgrade: called");
db.execSQL("DROP TABLE IF EXISTS "+VAlUE_TABLE_NAME);
onCreate(db);
}
public void insertIntoDb(int a,int b,int x, int y){
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DbHelper.VALUE_COLUMN_A,a);
contentValues.put(DbHelper.VALUE_COLUMN_B,b);
contentValues.put(DbHelper.VALUE_COLUMN_X,x);
contentValues.put(DbHelper.VALUE_COLUMN_Y,y);
long rowId = sqLiteDatabase.insert(VAlUE_TABLE_NAME,null,contentValues);
Log.d(TAG, "Save: "+rowId);
}
}