Flutter sqflite update data was reset after exit












0














I'm using this tutorial for work with sqflite:



https://grokonez.com/flutter/flutter-sqlite-example-crud-sqflite-example



The database was preloaded in my app, but after I ran an update query and exited the app, my data was reset!



How do I make sure my changes to the database persist after I exit the app.



Database Helper:



class DatabaseHelper {
static final DatabaseHelper _instance = new DatabaseHelper.internal();

factory DatabaseHelper() => _instance;

Database _db;

DatabaseHelper.internal();

Future<Database> get db async {
if (_db != null) {
print('---------------Open DB Exist-------------');
return _db;
}
print('---------------Create Copy DB-------------');
_db = await initDb();

return _db;
}

initDb() async {
String databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'mezajxdbs.db'); //On Device

ByteData data =
await rootBundle.load(join("assets", "dbs.db")); //In Assets folder
List<int> bytes =
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
await File(path).writeAsBytes(bytes);

// await deleteDatabase(path); // just for testing

_db = await openDatabase(path, version: 1);
return _db;
}
Future<List> getAllCategories() async {
var dbClient = await db;
// var result = await dbClient.query(tabelCategory, columns: [columnId, columnImage, columnName]);
var result = await dbClient.rawQuery('SELECT * FROM category');
return result.toList();
}

Future<int> getCount() async {
var dbClient = await db;
return Sqflite.firstIntValue(
await dbClient.rawQuery('SELECT COUNT(*) FROM category'));
}

Future<Category> getCategory(int id) async {
var dbClient = await db;
var result = await dbClient
.rawQuery('SELECT * FROM category WHERE ID_Category = $id');
if (result.length == 0) return null;
return new Category.fromMap(result.first);
}

Future<int> updateFavorite(Food food) async {
print('updated fav food');
var dbClient = await db;
return await dbClient.rawUpdate(
'UPDATE food SET Favorite = '${food.favotite}' WHERE ID_Food = ${food.id}');
}



Future<int> saveFood(Food food) async {
var dbClient = await db;
var result = await dbClient.rawInsert(
'INSERT INTO food (FK_Category, FK_Mezaj, Name, Value, Description, ImageName, Favorite) VALUES ('${food.cateory}', '${food.mezaj}', '${food.name}', '${food.value}', '${food.description}', '${food.image}', '${food.favotite}' )');

return result;
}
Future close() async {
var dbClient = await db;
return dbClient.close();
}
}


Food Model:



class Food {
int _id, _mezaj, _favorite, _category;
String _name, _value, _description, _image;

Food(this._category, this._mezaj, this._name, this._image, this._value, this._description, this._favorite);

Food.map(dynamic obj) {
// obj[Database Column Name] --> sample obj["ID_Food"] \
this._id = obj["ID_Food"];
this._category = obj["FK_Category"];
this._mezaj = obj["FK_Mezaj"];
this._name = obj["Name"];
this._value = obj["Value"];
this._description = obj["Description"];
this._image = obj["ImageName"];
this._favorite = obj["Favorite"];
}

int get id => _id;
int get cateory => _category;
int get mezaj => _mezaj;
String get name => _name;
String get value => _value;
String get description => _description;
String get image => _image;
int get favotite => _favorite;


Map<String, dynamic> toMap() {
var map = new Map<String, dynamic>();
if (_id != null) {
map['ID_Food'] = _id;
}
map["FK_Category"] = _category;
map["FK_Mezaj"] = _mezaj;
map["Name"] = _name;
map["Value"] = _value;
map["Description"] = _description;
map["ImageName"] = _image;
map["Favorite"] = _favorite;

return map;
}

Food.fromMap(Map<String, dynamic> map){
this._id = map["ID_Food"];
this._category = map["FK_Category"];
this._mezaj = map["FK_Mezaj"];
this._name = map["Name"];
this._value = map["Value"];
this._description = map["Description"];
this._image = map["ImageName"];
this._favorite = map["Favorite"];
}
}


Insert Code:



await db.saveFood(new Food(1, 2, 'Test2', 'ahoo', '2-2', "Create SQLite Tutorial", 1));


After Stop Debuging, and run again into debug, all inserted Data was reset!










share|improve this question
























  • You should post the error you get along with the relevant code.
    – grrigore
    Nov 13 at 20:55










  • Welcome to stackoverflow. I edited your question for clarity but you can still improve this question by including the query you are running that updates your database and a minimal example of what you expect the changes to be.
    – bunji
    Nov 14 at 2:24










  • all code added, No error message is displayed
    – david
    Nov 14 at 7:56


















0














I'm using this tutorial for work with sqflite:



https://grokonez.com/flutter/flutter-sqlite-example-crud-sqflite-example



The database was preloaded in my app, but after I ran an update query and exited the app, my data was reset!



How do I make sure my changes to the database persist after I exit the app.



Database Helper:



class DatabaseHelper {
static final DatabaseHelper _instance = new DatabaseHelper.internal();

factory DatabaseHelper() => _instance;

Database _db;

DatabaseHelper.internal();

Future<Database> get db async {
if (_db != null) {
print('---------------Open DB Exist-------------');
return _db;
}
print('---------------Create Copy DB-------------');
_db = await initDb();

return _db;
}

initDb() async {
String databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'mezajxdbs.db'); //On Device

ByteData data =
await rootBundle.load(join("assets", "dbs.db")); //In Assets folder
List<int> bytes =
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
await File(path).writeAsBytes(bytes);

// await deleteDatabase(path); // just for testing

_db = await openDatabase(path, version: 1);
return _db;
}
Future<List> getAllCategories() async {
var dbClient = await db;
// var result = await dbClient.query(tabelCategory, columns: [columnId, columnImage, columnName]);
var result = await dbClient.rawQuery('SELECT * FROM category');
return result.toList();
}

Future<int> getCount() async {
var dbClient = await db;
return Sqflite.firstIntValue(
await dbClient.rawQuery('SELECT COUNT(*) FROM category'));
}

Future<Category> getCategory(int id) async {
var dbClient = await db;
var result = await dbClient
.rawQuery('SELECT * FROM category WHERE ID_Category = $id');
if (result.length == 0) return null;
return new Category.fromMap(result.first);
}

Future<int> updateFavorite(Food food) async {
print('updated fav food');
var dbClient = await db;
return await dbClient.rawUpdate(
'UPDATE food SET Favorite = '${food.favotite}' WHERE ID_Food = ${food.id}');
}



Future<int> saveFood(Food food) async {
var dbClient = await db;
var result = await dbClient.rawInsert(
'INSERT INTO food (FK_Category, FK_Mezaj, Name, Value, Description, ImageName, Favorite) VALUES ('${food.cateory}', '${food.mezaj}', '${food.name}', '${food.value}', '${food.description}', '${food.image}', '${food.favotite}' )');

return result;
}
Future close() async {
var dbClient = await db;
return dbClient.close();
}
}


Food Model:



class Food {
int _id, _mezaj, _favorite, _category;
String _name, _value, _description, _image;

Food(this._category, this._mezaj, this._name, this._image, this._value, this._description, this._favorite);

Food.map(dynamic obj) {
// obj[Database Column Name] --> sample obj["ID_Food"] \
this._id = obj["ID_Food"];
this._category = obj["FK_Category"];
this._mezaj = obj["FK_Mezaj"];
this._name = obj["Name"];
this._value = obj["Value"];
this._description = obj["Description"];
this._image = obj["ImageName"];
this._favorite = obj["Favorite"];
}

int get id => _id;
int get cateory => _category;
int get mezaj => _mezaj;
String get name => _name;
String get value => _value;
String get description => _description;
String get image => _image;
int get favotite => _favorite;


Map<String, dynamic> toMap() {
var map = new Map<String, dynamic>();
if (_id != null) {
map['ID_Food'] = _id;
}
map["FK_Category"] = _category;
map["FK_Mezaj"] = _mezaj;
map["Name"] = _name;
map["Value"] = _value;
map["Description"] = _description;
map["ImageName"] = _image;
map["Favorite"] = _favorite;

return map;
}

Food.fromMap(Map<String, dynamic> map){
this._id = map["ID_Food"];
this._category = map["FK_Category"];
this._mezaj = map["FK_Mezaj"];
this._name = map["Name"];
this._value = map["Value"];
this._description = map["Description"];
this._image = map["ImageName"];
this._favorite = map["Favorite"];
}
}


Insert Code:



await db.saveFood(new Food(1, 2, 'Test2', 'ahoo', '2-2', "Create SQLite Tutorial", 1));


After Stop Debuging, and run again into debug, all inserted Data was reset!










share|improve this question
























  • You should post the error you get along with the relevant code.
    – grrigore
    Nov 13 at 20:55










  • Welcome to stackoverflow. I edited your question for clarity but you can still improve this question by including the query you are running that updates your database and a minimal example of what you expect the changes to be.
    – bunji
    Nov 14 at 2:24










  • all code added, No error message is displayed
    – david
    Nov 14 at 7:56
















0












0








0







I'm using this tutorial for work with sqflite:



https://grokonez.com/flutter/flutter-sqlite-example-crud-sqflite-example



The database was preloaded in my app, but after I ran an update query and exited the app, my data was reset!



How do I make sure my changes to the database persist after I exit the app.



Database Helper:



class DatabaseHelper {
static final DatabaseHelper _instance = new DatabaseHelper.internal();

factory DatabaseHelper() => _instance;

Database _db;

DatabaseHelper.internal();

Future<Database> get db async {
if (_db != null) {
print('---------------Open DB Exist-------------');
return _db;
}
print('---------------Create Copy DB-------------');
_db = await initDb();

return _db;
}

initDb() async {
String databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'mezajxdbs.db'); //On Device

ByteData data =
await rootBundle.load(join("assets", "dbs.db")); //In Assets folder
List<int> bytes =
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
await File(path).writeAsBytes(bytes);

// await deleteDatabase(path); // just for testing

_db = await openDatabase(path, version: 1);
return _db;
}
Future<List> getAllCategories() async {
var dbClient = await db;
// var result = await dbClient.query(tabelCategory, columns: [columnId, columnImage, columnName]);
var result = await dbClient.rawQuery('SELECT * FROM category');
return result.toList();
}

Future<int> getCount() async {
var dbClient = await db;
return Sqflite.firstIntValue(
await dbClient.rawQuery('SELECT COUNT(*) FROM category'));
}

Future<Category> getCategory(int id) async {
var dbClient = await db;
var result = await dbClient
.rawQuery('SELECT * FROM category WHERE ID_Category = $id');
if (result.length == 0) return null;
return new Category.fromMap(result.first);
}

Future<int> updateFavorite(Food food) async {
print('updated fav food');
var dbClient = await db;
return await dbClient.rawUpdate(
'UPDATE food SET Favorite = '${food.favotite}' WHERE ID_Food = ${food.id}');
}



Future<int> saveFood(Food food) async {
var dbClient = await db;
var result = await dbClient.rawInsert(
'INSERT INTO food (FK_Category, FK_Mezaj, Name, Value, Description, ImageName, Favorite) VALUES ('${food.cateory}', '${food.mezaj}', '${food.name}', '${food.value}', '${food.description}', '${food.image}', '${food.favotite}' )');

return result;
}
Future close() async {
var dbClient = await db;
return dbClient.close();
}
}


Food Model:



class Food {
int _id, _mezaj, _favorite, _category;
String _name, _value, _description, _image;

Food(this._category, this._mezaj, this._name, this._image, this._value, this._description, this._favorite);

Food.map(dynamic obj) {
// obj[Database Column Name] --> sample obj["ID_Food"] \
this._id = obj["ID_Food"];
this._category = obj["FK_Category"];
this._mezaj = obj["FK_Mezaj"];
this._name = obj["Name"];
this._value = obj["Value"];
this._description = obj["Description"];
this._image = obj["ImageName"];
this._favorite = obj["Favorite"];
}

int get id => _id;
int get cateory => _category;
int get mezaj => _mezaj;
String get name => _name;
String get value => _value;
String get description => _description;
String get image => _image;
int get favotite => _favorite;


Map<String, dynamic> toMap() {
var map = new Map<String, dynamic>();
if (_id != null) {
map['ID_Food'] = _id;
}
map["FK_Category"] = _category;
map["FK_Mezaj"] = _mezaj;
map["Name"] = _name;
map["Value"] = _value;
map["Description"] = _description;
map["ImageName"] = _image;
map["Favorite"] = _favorite;

return map;
}

Food.fromMap(Map<String, dynamic> map){
this._id = map["ID_Food"];
this._category = map["FK_Category"];
this._mezaj = map["FK_Mezaj"];
this._name = map["Name"];
this._value = map["Value"];
this._description = map["Description"];
this._image = map["ImageName"];
this._favorite = map["Favorite"];
}
}


Insert Code:



await db.saveFood(new Food(1, 2, 'Test2', 'ahoo', '2-2', "Create SQLite Tutorial", 1));


After Stop Debuging, and run again into debug, all inserted Data was reset!










share|improve this question















I'm using this tutorial for work with sqflite:



https://grokonez.com/flutter/flutter-sqlite-example-crud-sqflite-example



The database was preloaded in my app, but after I ran an update query and exited the app, my data was reset!



How do I make sure my changes to the database persist after I exit the app.



Database Helper:



class DatabaseHelper {
static final DatabaseHelper _instance = new DatabaseHelper.internal();

factory DatabaseHelper() => _instance;

Database _db;

DatabaseHelper.internal();

Future<Database> get db async {
if (_db != null) {
print('---------------Open DB Exist-------------');
return _db;
}
print('---------------Create Copy DB-------------');
_db = await initDb();

return _db;
}

initDb() async {
String databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'mezajxdbs.db'); //On Device

ByteData data =
await rootBundle.load(join("assets", "dbs.db")); //In Assets folder
List<int> bytes =
data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
await File(path).writeAsBytes(bytes);

// await deleteDatabase(path); // just for testing

_db = await openDatabase(path, version: 1);
return _db;
}
Future<List> getAllCategories() async {
var dbClient = await db;
// var result = await dbClient.query(tabelCategory, columns: [columnId, columnImage, columnName]);
var result = await dbClient.rawQuery('SELECT * FROM category');
return result.toList();
}

Future<int> getCount() async {
var dbClient = await db;
return Sqflite.firstIntValue(
await dbClient.rawQuery('SELECT COUNT(*) FROM category'));
}

Future<Category> getCategory(int id) async {
var dbClient = await db;
var result = await dbClient
.rawQuery('SELECT * FROM category WHERE ID_Category = $id');
if (result.length == 0) return null;
return new Category.fromMap(result.first);
}

Future<int> updateFavorite(Food food) async {
print('updated fav food');
var dbClient = await db;
return await dbClient.rawUpdate(
'UPDATE food SET Favorite = '${food.favotite}' WHERE ID_Food = ${food.id}');
}



Future<int> saveFood(Food food) async {
var dbClient = await db;
var result = await dbClient.rawInsert(
'INSERT INTO food (FK_Category, FK_Mezaj, Name, Value, Description, ImageName, Favorite) VALUES ('${food.cateory}', '${food.mezaj}', '${food.name}', '${food.value}', '${food.description}', '${food.image}', '${food.favotite}' )');

return result;
}
Future close() async {
var dbClient = await db;
return dbClient.close();
}
}


Food Model:



class Food {
int _id, _mezaj, _favorite, _category;
String _name, _value, _description, _image;

Food(this._category, this._mezaj, this._name, this._image, this._value, this._description, this._favorite);

Food.map(dynamic obj) {
// obj[Database Column Name] --> sample obj["ID_Food"] \
this._id = obj["ID_Food"];
this._category = obj["FK_Category"];
this._mezaj = obj["FK_Mezaj"];
this._name = obj["Name"];
this._value = obj["Value"];
this._description = obj["Description"];
this._image = obj["ImageName"];
this._favorite = obj["Favorite"];
}

int get id => _id;
int get cateory => _category;
int get mezaj => _mezaj;
String get name => _name;
String get value => _value;
String get description => _description;
String get image => _image;
int get favotite => _favorite;


Map<String, dynamic> toMap() {
var map = new Map<String, dynamic>();
if (_id != null) {
map['ID_Food'] = _id;
}
map["FK_Category"] = _category;
map["FK_Mezaj"] = _mezaj;
map["Name"] = _name;
map["Value"] = _value;
map["Description"] = _description;
map["ImageName"] = _image;
map["Favorite"] = _favorite;

return map;
}

Food.fromMap(Map<String, dynamic> map){
this._id = map["ID_Food"];
this._category = map["FK_Category"];
this._mezaj = map["FK_Mezaj"];
this._name = map["Name"];
this._value = map["Value"];
this._description = map["Description"];
this._image = map["ImageName"];
this._favorite = map["Favorite"];
}
}


Insert Code:



await db.saveFood(new Food(1, 2, 'Test2', 'ahoo', '2-2', "Create SQLite Tutorial", 1));


After Stop Debuging, and run again into debug, all inserted Data was reset!







android sqlite flutter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 at 7:55

























asked Nov 13 at 20:49









david

33




33












  • You should post the error you get along with the relevant code.
    – grrigore
    Nov 13 at 20:55










  • Welcome to stackoverflow. I edited your question for clarity but you can still improve this question by including the query you are running that updates your database and a minimal example of what you expect the changes to be.
    – bunji
    Nov 14 at 2:24










  • all code added, No error message is displayed
    – david
    Nov 14 at 7:56




















  • You should post the error you get along with the relevant code.
    – grrigore
    Nov 13 at 20:55










  • Welcome to stackoverflow. I edited your question for clarity but you can still improve this question by including the query you are running that updates your database and a minimal example of what you expect the changes to be.
    – bunji
    Nov 14 at 2:24










  • all code added, No error message is displayed
    – david
    Nov 14 at 7:56


















You should post the error you get along with the relevant code.
– grrigore
Nov 13 at 20:55




You should post the error you get along with the relevant code.
– grrigore
Nov 13 at 20:55












Welcome to stackoverflow. I edited your question for clarity but you can still improve this question by including the query you are running that updates your database and a minimal example of what you expect the changes to be.
– bunji
Nov 14 at 2:24




Welcome to stackoverflow. I edited your question for clarity but you can still improve this question by including the query you are running that updates your database and a minimal example of what you expect the changes to be.
– bunji
Nov 14 at 2:24












all code added, No error message is displayed
– david
Nov 14 at 7:56






all code added, No error message is displayed
– david
Nov 14 at 7:56



















active

oldest

votes











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289278%2fflutter-sqflite-update-data-was-reset-after-exit%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289278%2fflutter-sqflite-update-data-was-reset-after-exit%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

How to pass form data using jquery Ajax to insert data in database?

National Museum of Racing and Hall of Fame

Guess what letter conforming each word