Reputation: 45
The SQLite database file (.db) is not being created in Android Device Monitor >DBMS >File Explore >data. No .db file shown in this directory. I found myself unable to figure out why the database file is not created. here are my files.
MainActivity.java
package com.example.zohaib.database;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
SQLiteOpenHelper sqLiteOpenHelper;
SQLiteDatabase sqLiteDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
sqLiteOpenHelper = new DBConnection(this);
sqLiteDatabase = sqLiteOpenHelper.getWritableDatabase();
}
}
DBConnection.java
package com.example.zohaib.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
public class DBConnection extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "products.db";
private static final String TABLE_PRODUCTS = "products";
private static final String COLUMN_ID = "id";
private static final String COLUMN_PRODUCTNAME = "productName";
public DBConnection(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query ="CREATE TABLE " + TABLE_PRODUCTS + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT " +
")";
db.execSQL(query);
Log.d("Create Database:", "Successful");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
onCreate(db);
}
}
Upvotes: 1
Views: 2172
Reputation: 1053
Update: Just remove/uninstall the app from the device you are testing on and fix the query i.e. add the semicolon ";" at the end. Re-built the app.
or
you can use onUpgrade() to recreate the databse table by incrementing version. Also its not good to call lifecycle method method directly by you.
Working sample code (not perfect):
package com.example.zohaib.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBConnection extends SQLiteOpenHelper {
// change version with change in schema or db changes
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "products.db";
private static final String TABLE_PRODUCTS = "products";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_PRODUCTNAME = "productName";
public DBConnection(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
createTable(db);
Log.d("From onCreate : ", " successfully created.");
}
private void createTable(SQLiteDatabase db) {
String query ="CREATE TABLE " + TABLE_PRODUCTS + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT " +
");";
db.execSQL(query);
Log.d("Create Database:", "Successful");
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_PRODUCTNAME, "Sample Data");
db.insert(TABLE_PRODUCTS, null, contentValues);
Log.d("Insert Row:", "Insert Row Successful");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
createTable(db);
Log.d("From onUpgrade : ", " successfully upgraded.");
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
createTable(db);
Log.d("From onDowngrade : ", " successfully Downgraded.");
}
}
Here Goes the Database files.
Upvotes: 1
Reputation: 18112
You are using two table names in one create table script.
Change
String query ="CREATE TABLE test_1 " + TABLE_PRODUCTS + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT " +
")";
to
String query ="CREATE TABLE " + TABLE_PRODUCTS + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_PRODUCTNAME + " TEXT " +
");";
Upvotes: 2