Android won't save data entered to the SQLite databaseHow to save an Android Activity state using save instance state?How do I pass data between Activities in Android application?What are the best practices for SQLite on Android?Android SQL Database - rawQuery() Source not FoundWhy does my exception handler not trap Android SQLite insert error?SQLITE DATABASE RESETS in ANDROID on application opening the second time,once the data is insertedLoadmanager onLoadFinished not calledJava Beans with dynamic identificationssetText on button from another activity androidSearch Firestore query don't show data in RecycleView
Why did moving the mouse cursor cause Windows 95 to run more quickly?
How to respond to someone who condemns behavior similar to what they exhibit?
Creating patterns
Using Sed to add counter to keyword
Contributing to a candidate as a Foreign National US Resident?
Data normalization before or after train-test split?
What happens if the limit of 4 billion files was exceeded in an ext4 partition?
Interview Question - Card betting
Why do we need a bootloader separate than our application program in MCU's?
Minimizing medical costs with HSA
Should I warn my boss I might take sick leave
Why do Klingons use cloaking devices?
Taking advantage when the HR forgets to communicate the rules
Explain how 'Sharing the burden' puzzle from Professor Layton and the Miracle Mask should be solved
How serious is plagiarism in a master’s thesis?
What does this value mean in a BJT datasheet?
Do I need to be legally qualified to install a Hive smart thermostat?
What is this arch-and-tower near a road?
Isn't "Dave's protocol" good if only the database, and not the code, is leaked?
Is there a typical layout to blocking installed for backing in new construction framing?
Why is the saxophone not common in classical repertoire?
How to improve the size of cells in this table?
PhD: When to quit and move on?
Could you sell yourself into slavery in the USA?
Android won't save data entered to the SQLite database
How to save an Android Activity state using save instance state?How do I pass data between Activities in Android application?What are the best practices for SQLite on Android?Android SQL Database - rawQuery() Source not FoundWhy does my exception handler not trap Android SQLite insert error?SQLITE DATABASE RESETS in ANDROID on application opening the second time,once the data is insertedLoadmanager onLoadFinished not calledJava Beans with dynamic identificationssetText on button from another activity androidSearch Firestore query don't show data in RecycleView
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am creating an app which manages ingredients. I want to create an SQLite database which will save all the ingredients which a user enters when they click the save button. However, for some reason my data won't save to the SQLite database. I cannot see what is wrong with the code at all.
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper
private static final String tag = "Database Helper";
private static final String TABLE_NAME = "products_registered";
private static final String col1 = "Name";
private static final String col2 = "Weight";
private static final String col3 = "Price";
private static final String col4 = "Description";
@Override
public void onCreate(SQLiteDatabase db)
String createTable = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME +
" (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
col1 + " TEXT NOT NULL," +
col2 + " DOUBLE NOT NULL," +
col3 + " DOUBLE NOT NULL," +
col4 + " TEXT);";
db.execSQL(createTable);
public DatabaseHelper(Context context)
super(context, TABLE_NAME, null, 1);
@Override
public void onUpgrade(SQLiteDatabase db, int i, int j)
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
public boolean addData(String item1, String item2, String item3, String item4)
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(col1, item1);
contentValues.put(col2, item2);
contentValues.put(col3, item3);
contentValues.put(col4, item4);
Log.d(tag,"Adding name " + item1 + " to " + TABLE_NAME);
long result = db.insert(TABLE_NAME, null, contentValues);
db.close();
if(result == -1)
return false;
else
return true;
The Register Product Activity
public class RegisterProduct extends AppCompatActivity
{
DatabaseHelper databaseHelper;
private Button saveProductButton;
private EditText productName;
private EditText productWeight;
private EditText productPrice;
private EditText productDescription;
DatabaseHelper mDatabaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_product);
saveProductButton = (Button) findViewById(R.id.saveButton);
productName = (EditText) findViewById(R.id.enterName);
productWeight = (EditText) findViewById(R.id.enterWeight);
productPrice = (EditText) findViewById(R.id.enterPrice);
productDescription = (EditText) findViewById(R.id.enterDescription);
mDatabaseHelper = new DatabaseHelper(this);
try
saveProductButton.setOnClickListener(new View.OnClickListener()
public void onClick(View view)
String nameEntered = productName.getText().toString();
String weightEntered = productWeight.getText().toString();
String priceEntered = productPrice.getText().toString();
String descriptionEntered = productDescription.getText().toString();
addData(nameEntered, weightEntered, priceEntered, descriptionEntered);
productName.setText("");
);
finally
mDatabaseHelper.close();
public void addData(String newEntry1, String newEntry2, String newEntry3, String newEntry4)
mDatabaseHelper.addData(newEntry1, newEntry2, newEntry3, newEntry4);
I created a seperate class to create the database and table itself and then created the activity where I called the methods to save the data input in the activity displayed below.
Image of activity display
java android xml android-sqlite
|
show 4 more comments
I am creating an app which manages ingredients. I want to create an SQLite database which will save all the ingredients which a user enters when they click the save button. However, for some reason my data won't save to the SQLite database. I cannot see what is wrong with the code at all.
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper
private static final String tag = "Database Helper";
private static final String TABLE_NAME = "products_registered";
private static final String col1 = "Name";
private static final String col2 = "Weight";
private static final String col3 = "Price";
private static final String col4 = "Description";
@Override
public void onCreate(SQLiteDatabase db)
String createTable = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME +
" (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
col1 + " TEXT NOT NULL," +
col2 + " DOUBLE NOT NULL," +
col3 + " DOUBLE NOT NULL," +
col4 + " TEXT);";
db.execSQL(createTable);
public DatabaseHelper(Context context)
super(context, TABLE_NAME, null, 1);
@Override
public void onUpgrade(SQLiteDatabase db, int i, int j)
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
public boolean addData(String item1, String item2, String item3, String item4)
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(col1, item1);
contentValues.put(col2, item2);
contentValues.put(col3, item3);
contentValues.put(col4, item4);
Log.d(tag,"Adding name " + item1 + " to " + TABLE_NAME);
long result = db.insert(TABLE_NAME, null, contentValues);
db.close();
if(result == -1)
return false;
else
return true;
The Register Product Activity
public class RegisterProduct extends AppCompatActivity
{
DatabaseHelper databaseHelper;
private Button saveProductButton;
private EditText productName;
private EditText productWeight;
private EditText productPrice;
private EditText productDescription;
DatabaseHelper mDatabaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_product);
saveProductButton = (Button) findViewById(R.id.saveButton);
productName = (EditText) findViewById(R.id.enterName);
productWeight = (EditText) findViewById(R.id.enterWeight);
productPrice = (EditText) findViewById(R.id.enterPrice);
productDescription = (EditText) findViewById(R.id.enterDescription);
mDatabaseHelper = new DatabaseHelper(this);
try
saveProductButton.setOnClickListener(new View.OnClickListener()
public void onClick(View view)
String nameEntered = productName.getText().toString();
String weightEntered = productWeight.getText().toString();
String priceEntered = productPrice.getText().toString();
String descriptionEntered = productDescription.getText().toString();
addData(nameEntered, weightEntered, priceEntered, descriptionEntered);
productName.setText("");
);
finally
mDatabaseHelper.close();
public void addData(String newEntry1, String newEntry2, String newEntry3, String newEntry4)
mDatabaseHelper.addData(newEntry1, newEntry2, newEntry3, newEntry4);
I created a seperate class to create the database and table itself and then created the activity where I called the methods to save the data input in the activity displayed below.
Image of activity display
java android xml android-sqlite
Does your app crash?
– forpas
Mar 25 at 19:10
@forpas No it doesn't, it just doesn't save for some odd reason
– Anisa
Mar 25 at 19:16
Remove the try/catch/finally block and try again, just to make sure. If the app crashes then the problem can be spotted.
– forpas
Mar 25 at 19:20
1
Uninstall the app from the emulator just like you would do it on a real device: drag its icon to the bin.
– forpas
Mar 25 at 20:20
1
@forpas I tried doing that and it worked- thank you!
– Anisa
Mar 25 at 20:46
|
show 4 more comments
I am creating an app which manages ingredients. I want to create an SQLite database which will save all the ingredients which a user enters when they click the save button. However, for some reason my data won't save to the SQLite database. I cannot see what is wrong with the code at all.
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper
private static final String tag = "Database Helper";
private static final String TABLE_NAME = "products_registered";
private static final String col1 = "Name";
private static final String col2 = "Weight";
private static final String col3 = "Price";
private static final String col4 = "Description";
@Override
public void onCreate(SQLiteDatabase db)
String createTable = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME +
" (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
col1 + " TEXT NOT NULL," +
col2 + " DOUBLE NOT NULL," +
col3 + " DOUBLE NOT NULL," +
col4 + " TEXT);";
db.execSQL(createTable);
public DatabaseHelper(Context context)
super(context, TABLE_NAME, null, 1);
@Override
public void onUpgrade(SQLiteDatabase db, int i, int j)
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
public boolean addData(String item1, String item2, String item3, String item4)
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(col1, item1);
contentValues.put(col2, item2);
contentValues.put(col3, item3);
contentValues.put(col4, item4);
Log.d(tag,"Adding name " + item1 + " to " + TABLE_NAME);
long result = db.insert(TABLE_NAME, null, contentValues);
db.close();
if(result == -1)
return false;
else
return true;
The Register Product Activity
public class RegisterProduct extends AppCompatActivity
{
DatabaseHelper databaseHelper;
private Button saveProductButton;
private EditText productName;
private EditText productWeight;
private EditText productPrice;
private EditText productDescription;
DatabaseHelper mDatabaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_product);
saveProductButton = (Button) findViewById(R.id.saveButton);
productName = (EditText) findViewById(R.id.enterName);
productWeight = (EditText) findViewById(R.id.enterWeight);
productPrice = (EditText) findViewById(R.id.enterPrice);
productDescription = (EditText) findViewById(R.id.enterDescription);
mDatabaseHelper = new DatabaseHelper(this);
try
saveProductButton.setOnClickListener(new View.OnClickListener()
public void onClick(View view)
String nameEntered = productName.getText().toString();
String weightEntered = productWeight.getText().toString();
String priceEntered = productPrice.getText().toString();
String descriptionEntered = productDescription.getText().toString();
addData(nameEntered, weightEntered, priceEntered, descriptionEntered);
productName.setText("");
);
finally
mDatabaseHelper.close();
public void addData(String newEntry1, String newEntry2, String newEntry3, String newEntry4)
mDatabaseHelper.addData(newEntry1, newEntry2, newEntry3, newEntry4);
I created a seperate class to create the database and table itself and then created the activity where I called the methods to save the data input in the activity displayed below.
Image of activity display
java android xml android-sqlite
I am creating an app which manages ingredients. I want to create an SQLite database which will save all the ingredients which a user enters when they click the save button. However, for some reason my data won't save to the SQLite database. I cannot see what is wrong with the code at all.
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper
private static final String tag = "Database Helper";
private static final String TABLE_NAME = "products_registered";
private static final String col1 = "Name";
private static final String col2 = "Weight";
private static final String col3 = "Price";
private static final String col4 = "Description";
@Override
public void onCreate(SQLiteDatabase db)
String createTable = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME +
" (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
col1 + " TEXT NOT NULL," +
col2 + " DOUBLE NOT NULL," +
col3 + " DOUBLE NOT NULL," +
col4 + " TEXT);";
db.execSQL(createTable);
public DatabaseHelper(Context context)
super(context, TABLE_NAME, null, 1);
@Override
public void onUpgrade(SQLiteDatabase db, int i, int j)
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
public boolean addData(String item1, String item2, String item3, String item4)
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(col1, item1);
contentValues.put(col2, item2);
contentValues.put(col3, item3);
contentValues.put(col4, item4);
Log.d(tag,"Adding name " + item1 + " to " + TABLE_NAME);
long result = db.insert(TABLE_NAME, null, contentValues);
db.close();
if(result == -1)
return false;
else
return true;
The Register Product Activity
public class RegisterProduct extends AppCompatActivity
{
DatabaseHelper databaseHelper;
private Button saveProductButton;
private EditText productName;
private EditText productWeight;
private EditText productPrice;
private EditText productDescription;
DatabaseHelper mDatabaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_product);
saveProductButton = (Button) findViewById(R.id.saveButton);
productName = (EditText) findViewById(R.id.enterName);
productWeight = (EditText) findViewById(R.id.enterWeight);
productPrice = (EditText) findViewById(R.id.enterPrice);
productDescription = (EditText) findViewById(R.id.enterDescription);
mDatabaseHelper = new DatabaseHelper(this);
try
saveProductButton.setOnClickListener(new View.OnClickListener()
public void onClick(View view)
String nameEntered = productName.getText().toString();
String weightEntered = productWeight.getText().toString();
String priceEntered = productPrice.getText().toString();
String descriptionEntered = productDescription.getText().toString();
addData(nameEntered, weightEntered, priceEntered, descriptionEntered);
productName.setText("");
);
finally
mDatabaseHelper.close();
public void addData(String newEntry1, String newEntry2, String newEntry3, String newEntry4)
mDatabaseHelper.addData(newEntry1, newEntry2, newEntry3, newEntry4);
I created a seperate class to create the database and table itself and then created the activity where I called the methods to save the data input in the activity displayed below.
Image of activity display
java android xml android-sqlite
java android xml android-sqlite
edited Mar 25 at 23:30
MikeT
22.1k11 gold badges28 silver badges44 bronze badges
22.1k11 gold badges28 silver badges44 bronze badges
asked Mar 25 at 19:04
AnisaAnisa
335 bronze badges
335 bronze badges
Does your app crash?
– forpas
Mar 25 at 19:10
@forpas No it doesn't, it just doesn't save for some odd reason
– Anisa
Mar 25 at 19:16
Remove the try/catch/finally block and try again, just to make sure. If the app crashes then the problem can be spotted.
– forpas
Mar 25 at 19:20
1
Uninstall the app from the emulator just like you would do it on a real device: drag its icon to the bin.
– forpas
Mar 25 at 20:20
1
@forpas I tried doing that and it worked- thank you!
– Anisa
Mar 25 at 20:46
|
show 4 more comments
Does your app crash?
– forpas
Mar 25 at 19:10
@forpas No it doesn't, it just doesn't save for some odd reason
– Anisa
Mar 25 at 19:16
Remove the try/catch/finally block and try again, just to make sure. If the app crashes then the problem can be spotted.
– forpas
Mar 25 at 19:20
1
Uninstall the app from the emulator just like you would do it on a real device: drag its icon to the bin.
– forpas
Mar 25 at 20:20
1
@forpas I tried doing that and it worked- thank you!
– Anisa
Mar 25 at 20:46
Does your app crash?
– forpas
Mar 25 at 19:10
Does your app crash?
– forpas
Mar 25 at 19:10
@forpas No it doesn't, it just doesn't save for some odd reason
– Anisa
Mar 25 at 19:16
@forpas No it doesn't, it just doesn't save for some odd reason
– Anisa
Mar 25 at 19:16
Remove the try/catch/finally block and try again, just to make sure. If the app crashes then the problem can be spotted.
– forpas
Mar 25 at 19:20
Remove the try/catch/finally block and try again, just to make sure. If the app crashes then the problem can be spotted.
– forpas
Mar 25 at 19:20
1
1
Uninstall the app from the emulator just like you would do it on a real device: drag its icon to the bin.
– forpas
Mar 25 at 20:20
Uninstall the app from the emulator just like you would do it on a real device: drag its icon to the bin.
– forpas
Mar 25 at 20:20
1
1
@forpas I tried doing that and it worked- thank you!
– Anisa
Mar 25 at 20:46
@forpas I tried doing that and it worked- thank you!
– Anisa
Mar 25 at 20:46
|
show 4 more comments
1 Answer
1
active
oldest
votes
Your issue is most likely that you have changed the structure/schema of the database probably adding a column since running the App.
Typically you would get an SQLite exception when running the App indicating that the column was not found. However, as you have wrapped the addData in a try/finally construct the exception has been suppressed thus it appears that all is fine as the App doesn't crash or show anything in the log.
Wrapping database code in try constructs and trapping errors, as you have found can be confusing and it is suggested that you don't.
The reason why changing the schema/structure within the code for the onCreate method (the likely root cause of the issue), is that the onCreate method ONLY RUNS automatically when the database is created.
If the schema/structure of the database is changed then you need some way of either forcing the structure change or when developing an App, as the underlying data, should not need to be saved, then the easiest way is to delete the database and then rerun the App.
The database can be deleted by either deleting the App's data or by uninstalling the App (noting that any existing data will be lost).
An alternative, is to DROP the respective table(s) and recreate them. Frequently the onUpgrade method will written to do this. If this is the case (it is in your case) then another way of changing the structure is to trigger the onUpgrage method by increasing the version number pass to the SQLiteOpenHelper constructor (the super call). In your case you could change super(context, TABLE_NAME, null, 1);
to be super(context, TABLE_NAME, null, 2);
After deleting the database or increasing the version number (preferably removing the try/finally construct from around the call to addData) and reunning the App, it may well just work. If not then the log should show any exceptions and problems can then be pinpointed.
Thank you- the issue has been solved
– Anisa
Apr 7 at 13:30
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55344833%2fandroid-wont-save-data-entered-to-the-sqlite-database%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your issue is most likely that you have changed the structure/schema of the database probably adding a column since running the App.
Typically you would get an SQLite exception when running the App indicating that the column was not found. However, as you have wrapped the addData in a try/finally construct the exception has been suppressed thus it appears that all is fine as the App doesn't crash or show anything in the log.
Wrapping database code in try constructs and trapping errors, as you have found can be confusing and it is suggested that you don't.
The reason why changing the schema/structure within the code for the onCreate method (the likely root cause of the issue), is that the onCreate method ONLY RUNS automatically when the database is created.
If the schema/structure of the database is changed then you need some way of either forcing the structure change or when developing an App, as the underlying data, should not need to be saved, then the easiest way is to delete the database and then rerun the App.
The database can be deleted by either deleting the App's data or by uninstalling the App (noting that any existing data will be lost).
An alternative, is to DROP the respective table(s) and recreate them. Frequently the onUpgrade method will written to do this. If this is the case (it is in your case) then another way of changing the structure is to trigger the onUpgrage method by increasing the version number pass to the SQLiteOpenHelper constructor (the super call). In your case you could change super(context, TABLE_NAME, null, 1);
to be super(context, TABLE_NAME, null, 2);
After deleting the database or increasing the version number (preferably removing the try/finally construct from around the call to addData) and reunning the App, it may well just work. If not then the log should show any exceptions and problems can then be pinpointed.
Thank you- the issue has been solved
– Anisa
Apr 7 at 13:30
add a comment |
Your issue is most likely that you have changed the structure/schema of the database probably adding a column since running the App.
Typically you would get an SQLite exception when running the App indicating that the column was not found. However, as you have wrapped the addData in a try/finally construct the exception has been suppressed thus it appears that all is fine as the App doesn't crash or show anything in the log.
Wrapping database code in try constructs and trapping errors, as you have found can be confusing and it is suggested that you don't.
The reason why changing the schema/structure within the code for the onCreate method (the likely root cause of the issue), is that the onCreate method ONLY RUNS automatically when the database is created.
If the schema/structure of the database is changed then you need some way of either forcing the structure change or when developing an App, as the underlying data, should not need to be saved, then the easiest way is to delete the database and then rerun the App.
The database can be deleted by either deleting the App's data or by uninstalling the App (noting that any existing data will be lost).
An alternative, is to DROP the respective table(s) and recreate them. Frequently the onUpgrade method will written to do this. If this is the case (it is in your case) then another way of changing the structure is to trigger the onUpgrage method by increasing the version number pass to the SQLiteOpenHelper constructor (the super call). In your case you could change super(context, TABLE_NAME, null, 1);
to be super(context, TABLE_NAME, null, 2);
After deleting the database or increasing the version number (preferably removing the try/finally construct from around the call to addData) and reunning the App, it may well just work. If not then the log should show any exceptions and problems can then be pinpointed.
Thank you- the issue has been solved
– Anisa
Apr 7 at 13:30
add a comment |
Your issue is most likely that you have changed the structure/schema of the database probably adding a column since running the App.
Typically you would get an SQLite exception when running the App indicating that the column was not found. However, as you have wrapped the addData in a try/finally construct the exception has been suppressed thus it appears that all is fine as the App doesn't crash or show anything in the log.
Wrapping database code in try constructs and trapping errors, as you have found can be confusing and it is suggested that you don't.
The reason why changing the schema/structure within the code for the onCreate method (the likely root cause of the issue), is that the onCreate method ONLY RUNS automatically when the database is created.
If the schema/structure of the database is changed then you need some way of either forcing the structure change or when developing an App, as the underlying data, should not need to be saved, then the easiest way is to delete the database and then rerun the App.
The database can be deleted by either deleting the App's data or by uninstalling the App (noting that any existing data will be lost).
An alternative, is to DROP the respective table(s) and recreate them. Frequently the onUpgrade method will written to do this. If this is the case (it is in your case) then another way of changing the structure is to trigger the onUpgrage method by increasing the version number pass to the SQLiteOpenHelper constructor (the super call). In your case you could change super(context, TABLE_NAME, null, 1);
to be super(context, TABLE_NAME, null, 2);
After deleting the database or increasing the version number (preferably removing the try/finally construct from around the call to addData) and reunning the App, it may well just work. If not then the log should show any exceptions and problems can then be pinpointed.
Your issue is most likely that you have changed the structure/schema of the database probably adding a column since running the App.
Typically you would get an SQLite exception when running the App indicating that the column was not found. However, as you have wrapped the addData in a try/finally construct the exception has been suppressed thus it appears that all is fine as the App doesn't crash or show anything in the log.
Wrapping database code in try constructs and trapping errors, as you have found can be confusing and it is suggested that you don't.
The reason why changing the schema/structure within the code for the onCreate method (the likely root cause of the issue), is that the onCreate method ONLY RUNS automatically when the database is created.
If the schema/structure of the database is changed then you need some way of either forcing the structure change or when developing an App, as the underlying data, should not need to be saved, then the easiest way is to delete the database and then rerun the App.
The database can be deleted by either deleting the App's data or by uninstalling the App (noting that any existing data will be lost).
An alternative, is to DROP the respective table(s) and recreate them. Frequently the onUpgrade method will written to do this. If this is the case (it is in your case) then another way of changing the structure is to trigger the onUpgrage method by increasing the version number pass to the SQLiteOpenHelper constructor (the super call). In your case you could change super(context, TABLE_NAME, null, 1);
to be super(context, TABLE_NAME, null, 2);
After deleting the database or increasing the version number (preferably removing the try/finally construct from around the call to addData) and reunning the App, it may well just work. If not then the log should show any exceptions and problems can then be pinpointed.
answered Mar 25 at 23:52
MikeTMikeT
22.1k11 gold badges28 silver badges44 bronze badges
22.1k11 gold badges28 silver badges44 bronze badges
Thank you- the issue has been solved
– Anisa
Apr 7 at 13:30
add a comment |
Thank you- the issue has been solved
– Anisa
Apr 7 at 13:30
Thank you- the issue has been solved
– Anisa
Apr 7 at 13:30
Thank you- the issue has been solved
– Anisa
Apr 7 at 13:30
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55344833%2fandroid-wont-save-data-entered-to-the-sqlite-database%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Does your app crash?
– forpas
Mar 25 at 19:10
@forpas No it doesn't, it just doesn't save for some odd reason
– Anisa
Mar 25 at 19:16
Remove the try/catch/finally block and try again, just to make sure. If the app crashes then the problem can be spotted.
– forpas
Mar 25 at 19:20
1
Uninstall the app from the emulator just like you would do it on a real device: drag its icon to the bin.
– forpas
Mar 25 at 20:20
1
@forpas I tried doing that and it worked- thank you!
– Anisa
Mar 25 at 20:46