Last active
September 11, 2019 06:21
-
-
Save Dalmangyi/f65d772d542b4e81d3e42bf705510038 to your computer and use it in GitHub Desktop.
dart_sqflite_crud
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Create | |
createData(Dog dog) async { | |
final db = await database; | |
var res = await db.rawInsert('INSERT INTO $TableName(name) VALUES(?)', [dog.name]); | |
return res; | |
} | |
//Read | |
getDog(int id) async { | |
final db = await database; | |
var res = await db.rawQuery('SELECT * FROM $TableName WHERE id = ?', [id]); | |
return res.isNotEmpty ? Dog(id: res.first['id'], name: res.first['name']) : Null; | |
} | |
//Read All | |
Future<List<Dog>> getAllDogs() async { | |
final db = await database; | |
var res = await db.rawQuery('SELECT * FROM $TableName'); | |
List<Dog> list = res.isNotEmpty ? res.map((c) => Dog(id:c['id'], name:c['name'])).toList() : []; | |
return list; | |
} | |
//Delete | |
deleteDog(int id) async { | |
final db = await database; | |
var res = db.rawDelete('DELETE FROM $TableName WHERE id = ?', [id]); | |
return res; | |
} | |
//Delete All | |
deleteAllDogs() async { | |
final db = await database; | |
db.rawDelete('DELETE FROM $TableName'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Create | |
createData(Dog dog) async { | |
final db = await database; | |
var res = await db.insert(TableName, dog.toJson()); | |
return res; | |
} | |
//Read | |
getDog(int id) async { | |
final db = await database; | |
var res = await db.query(TableName, where: 'id = ?', whereArgs: [id]); | |
return res.isNotEmpty ? Dog.fromJson(res.first) : Null; | |
} | |
//Read All | |
Future<List<Dog>> getAllDogs() async { | |
final db = await database; | |
var res = await db.query(TableName); | |
List<Dog> list = res.isNotEmpty ? res.map((c) => Dog.fromJson(c)).toList() : []; | |
return list; | |
} | |
//Delete | |
deleteDog(int id) async { | |
final db = await database; | |
var res = db.delete(TableName, where: 'id = ?', whereArgs: [id]); | |
return res; | |
} | |
//Delete All | |
deleteAllDogs() async { | |
final db = await database; | |
db.rawDelete('DELETE FROM $TableName'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment