I am trying to create a simple database file with Android Studio. I've written the code like this and no errors were shown. I checked the data/data/com.example.app/databases
file and there are three files: Student.db
, Student.db-shm
, Student.db-wal
but no Student.db-journal
like the tutorials in it. And when I try to open the Student.db
with DB Browser no tables are created.
I have tried multiple syntaxes with the "CREATE TABLE" part but it didn't work.
public class Database extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Student.db";
public static final String TABLE_NAME = "student_table";
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "SURNAME";
public static final String COL_4 = "MARKS";
public Database(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY
KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
and activity will be
public class MainActivity extends AppCompatActivity {
Database myDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new Database(this);
}
}
Hey @trpn39 Try this,
public class DatabaseHandler extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "test.db";
public static final String TABLE_NAME = "userdata_table";
public static final String COLUMN_ID = "id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_COUNTRY = "country";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = " CREATE TABLE IF NOT EXISTS " + TABLENAME + " (" +
KEY_STUID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_SUB1 + " TEXT NOT NULL," +
KEY_SUB2 + " TEXT NOT NULL," +
KEY_SUB3 + " TEXT NOT NULL," +
KEY_MARKS1 + " INTEGER NOT NULL," +
KEY_MARKS2 + " INTEGER NOT NULL," +
KEY_MARKS3 + " INTEGER NOT NULL"
+ ")";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
Initialising in any activity
public class ExampleActivity extends AppCompatActivity {
DatabaseHandler databaseHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
databaseHandler = new DataBaseHandler(this);
//create method to store
storeData();
}
public void store(){
//you need to implement getwritable database here
ContentValues contentValues = new ContentValues();
SQLiteDatabase db =
databaseHandler.getWritableDatabase();
contentValues.put(SarathCityLocalDatabase.SUB_CATEGORY_LIST_ID, id);
contentValues.put(databaseHandler.COLUMN_ID, id);
contentValues.put(databaseHandler.COLUMN_NAME, name);
contentValues.put(databaseHandler.COLUMN_COUNTRY, county);
contentValues.put(databaseHandler.LOGO, logo);
//insert data in table
db.insert(SarathCityLocalDatabase.public class DatabaseHandler extends
SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "test.db";
public static final String TABLE_NAME = "userdata_table";
public static final String COLUMN_ID = "id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_COUNTRY = "country";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE, null,
contentValues);
}
}
Please let me know after you tried this