Table not creating on android device but creating on the virtual device - android

So I am trying to create a Database and a table in android studio. The database is created successfully but table is not creating when I am using my android phone but both the database and the table are created when I use Virtual device from the AVD.
public class Databases extends SQLiteOpenHelper {
public static final String dbname = "AttendanceDb11";
public Databases(#Nullable Context context) {
super(context, dbname, null, 2);
}
#Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE Student12 ( _id INTEGER PRIMARY KEY AUTOINCREMENT, Name Text)";
db.execSQL(CREATE_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("Drop table if exists Student12");
onCreate(db);
}
}
Here is my code

It looks like that app was already installed before you created the tables. I would do one of the following:
1: Uninstalling and re-installing the app.
2: The best and better approach is to drop and recreate table in onUpdate method, and increase the db version whenever you change the schema.

Related

how to add table to the database when database is already created in android?

I have created a database with a table.I want to add more tables dynamically to that database on the user's command.
The onOpen() method is not useful as it also changes my previous tables that already exist in the database.
I assume you are using SQLite database. If that is the case, a working solution exists here:
Create new table in existing DB in separate SQLiteOpenHelper class.
see rmkrishna's answer from that post, below:
First check the current database version for this database
private final static String DATABASE_NAME = "MainDB";
private static final int DATABASE_VERSION = 1;
public BaseSQLiteOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
and increment the database version(DATABASE_VERSION), and add your new table query in on Upgrade and oncreate method like below.
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("old query no need to change");
db.execSQL("Create your new table here");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 2) {
db.execSQL("Create your new table here as well this for update the old DB");
}
}

How to make onUpgrade() statements run even on first installation?

In my application i have a database which is created using DB queries in the DBHelper class. I want the onUpgrade statement to run on installation of the apk as there is a possiblity that the user might unistall and install the same application.
In this case it becomes the first installation on the device and the onUpgrade does not run which causes the application to crash.
It works perfectly if the new app is updated on the previous app.
How do i reslove this?? What is solution for such cases??
Please help !! Thanks in Advance !!
Here is my DbHelper.java
public class Dbhelper extends SQLiteOpenHelper {
public Dbhelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(TABLE_CREATE_IMAGE);
database.execSQL(TABLE_CREATE_ATTEDANCE);
database.execSQL(TABLE_CREATE_STOCK);
}
#Override
public void onUpgrade(SQLiteDatabase database, int oldVersion,
int newVersion) {
Log.e("","upgrade ");
database.execSQL("ALTER TABLE stock ADD db_stock_id varchar(100)");//1.1
//
}
public void dropandcreate(SQLiteDatabase database)
{
database.execSQL("DROP TABLE IF EXISTS image");
database.execSQL("DROP TABLE IF EXISTS attendance");
database.execSQL("DROP TABLE IF EXISTS stock");
onCreate(database);
}
}
Call it yourself:
#Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(TABLE_CREATE_IMAGE);
database.execSQL(TABLE_CREATE_ATTEDANCE);
database.execSQL(TABLE_CREATE_STOCK);
onUpgrade(database, VERSION);
}

Will data lost when onUpgrade is called

I have created DatabaseHelper to make my development easier. If my Database version is upgrade from 1 to 2, onUpgrade will called and table will be dropped. I wonder will this cause my data stored in table dropped too? I means, will I lost my data stored in table?
Here's the code.
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS notes");
onCreate(db);
}
}
By looking at your code, Yes you will lose the current table of the database and a new empty table notes will be created.
So the best way to upgrade the database is before this:
db.execSQL("DROP TABLE IF EXISTS notes");
statement, you should made a backup of your current database (easiest way is to rename the table) and then drop the current table to create the new one.
It is always a good programming practice to keep a backup of information before editing/upgrading it.
When you drop a table all it's data is deleted too. If you want to keep it, rename the table first, create the new one and then select the data from the old into the new one.
You can alter table in onUpgrade method if there is change in coloumn. If you want to make fresh table then only call Drop inside onUpgrade, if there is no change in schema dont do anything inside onUpgrade method.

Android database directory on emulator [duplicate]

This question already has answers here:
Where does Android emulator store SQLite database?
(10 answers)
Closed 7 months ago.
Hello i created a database class on andorid that below.
public class Veritabani extends SQLiteOpenHelper {
private static final String Veritabani_Adi = "Veritabanim";
private static final int Veritabani_Version = 1;
public Veritabani(Context context) {
super(context, Veritabani_Adi, null, Veritabani_Version);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE ARKTABLE (id INTEGER PRIMARY KEY AUTOINCREMENT , ad TEXT, soyad TEXT);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST OperatorTablosu");
onCreate(db);
}
}
**then i declare may Veritabani class in my activity as below.**
public class DictionaryActivity extends Activity {
Spinner spnLanguage;
Veritabani objdb;;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dictionary);
spnLanguage=(Spinner)findViewById(R.id.spnLanguage);
objdb=new Veritabani(this);
spinnerfill();
}
i don't have an exception on logcate or console.
But when i look may data/data/myappp/ i dont see database directory.
i used android 2.2 version
check In the DDMS perspective if you are using Eclipse. there you can easily find whether your database is created or not.
you want to DDBMS
Go to mnt/sdcard/yourdatabase name.it's database store in sdcard
internal memory means data/yourpackagename/yourdatabasename

App Crashes due to Db creation on every launch

I have an application in which i am trying to create a Database. On the first run the application works properly. Subsequent launch of the application it crashes. The error is because the application is trying to create the db again.
So i want to know how to create a database or tables only if they dont exists or only if the application is run the first time.
You can extend the class SQLiteOpenHelper to handle different versions of your database. Here is an example:
public class MyDbOpenHelper extends **SQLiteOpenHelper** {
final static int VERSION = 2;
public MyDbOpenHelper(Context context, String dbname) {
super(context, dbname, null, VERSION);
}
#Override
public void **onCreate**(SQLiteDatabase db) {
/* TODO SQL-Queries for new Database */
}
#Override
public void **onUpgrade**(SQLiteDatabase db, int oldVersion, int newVersion) {
/* SQL-Queries t*/
if (oldVersion < 2) {
db.execSQL("ALTER TABLE my_table ADD COLUMN new_attrubte INTEGER");
}
}
}
Handle of database is new in onCreate and if it exist, onUpgrade is called with existing version number in oldVersion parameter.
SQLite has a CREATE TABLE IF NOT EXISTS command. You could try that.
It is usually an error to attempt to create a new table in a database that already contains a table, index or view of the same name. However, if the "IF NOT EXISTS" clause is specified as part of the CREATE TABLE statement and a table or view of the same name already exists, the CREATE TABLE command simply has no effect (and no error message is returned). An error is still returned if the table cannot be created because of an existing index, even if the "IF NOT EXISTS" clause is specified.

Categories

Resources