Reputation: 4616
I am trying to create an SQLite database for my app in Androis Studio 2.3.
I have tried the following-
Approach-1: Created a class extending from SQLiteOpenHelper
and overriding OnCreate
public AppDB(Context context){
super(context, DBName, null, 1);
SQLiteDatabase db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TableName + " (RecordingID TEXT PRIMARY KEY,RecordItemName TEXT,RecordFilePath TEXT,RecordFileDate TEXT)");
}
Approach-2: Tried adding these two lines in my Main activity's OnCreate
SQLiteDatabase db = openOrCreateDatabase("AppDB.db", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS Recordings (RecordingID TEXT PRIMARY KEY,RecordItemName TEXT,RecordFilePath TEXT,RecordFileDate TEXT)");
My app launches fine on emulator. When I try to verify the created database using Android Device Monitor > File Manager, I can't see my database under the data folder.
I am very new to Android and can't figure what am I doing wrong. Please tell me if I can add more details on this.
Upvotes: 0
Views: 941
Reputation: 51
You can create database like this so we can create database on install of application.
public class DataBaseHelp extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "studentinfo.db";
public static final String TABLE_NAME = "student";
public static final String STUDENT_ID = "id";
public static final String STUDENT_NAME = "name";
public static final String STUDENT_ENROLLMENT_NO = "number";
public static final String STUDENT_CONTACT = "contact_number";
private String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ STUDENT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + STUDENT_NAME + " TEXT,"
+ STUDENT_ENROLLMENT_NO + " TEXT," + STUDENT_CONTACT + " TEXT" + ")";
public DataBaseHelp(Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_USER_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE OF EXISTS "+TABLE_NAME);
onCreate(sqLiteDatabase);
}
}
Upvotes: 0
Reputation: 2815
You missed the semicolon in the create query.
db.execSQL("CREATE TABLE " + TableName + " (RecordingID TEXT PRIMARY KEY,RecordItemName TEXT,RecordFilePath TEXT,RecordFileDate TEXT);");
Upvotes: 0
Reputation: 1645
Due to permission issues after Marshmallow you have to create an AVD targeting 23 or lower , then data would appear. Another approach is updating the SDK to ver 3.0 beta 2 on canary channel, although it's not stable but this problem is solved and you can access db on higher Apis.
Upvotes: 1