Error:
Unhandled Exception: DatabaseException(no such table: Prospect (code 1 SQLITE_ERROR): , while compiling: INSERT INTO Prospect (id, nombre_nucleo, nombre_contacto, municipio_prospecto, telefono_prospecto, email_prospecto, observaciones_prospecto, fecha_alta, status_pros, hectareas_Aprox) VALUES (NULL, ?, ?, ?, ?, ?, ?, NULL, ?, ?)) sql 'INSERT INTO Prospect (id, nombre_nucleo, nombre_contacto, municipio_prospecto, telefono_prospecto, email_prospecto, observaciones_prospecto, fecha_alta, status_pros, hectareas_Aprox) VALUES (NULL, ?, ?, ?, ?, ?, ?, NULL, ?, ?)' args [nombre, nombre largo muy largo, Guadalajara, 3314550167, [email protected], qwertyuiopñlkjhgfdsazxcvbnmklñpasdcfv, true, 100.0]}
Tengo algunos problemas con mi código para enviar datos a mi db de sqlite,y en la db no sé si el insert esta bien, no sé cual utilizar o si ambas son correctas y lo que esta fallando es mi forma de enviar los datos.
prospectPage:
class ProspectPage extends StatefulWidget {
@override
_ProspectPageState createState() => _ProspectPageState();
}
class _ProspectPageState extends State<ProspectPage> {
final formKey = GlobalKey<FormState>();
final scaffolKey = GlobalKey<ScaffoldState>();
final prospectProvider = new ProspectProvider();
ProspectModel prospect = new ProspectModel();
bool _saving = false;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Nuevo prospecto'),
actions: [],
),
body: SingleChildScrollView(
child: Container(
padding: EdgeInsets.all(15.0),
//form como en Html
child: Form(
key: formKey,
child: Column(
children: [
SizedBox(height: 20.0),
new Image.asset(
'assets/img/logo.png',
width: 200.0,
),
SizedBox(height: 20.0),
_nombreNucleo(),
_botonSubmit(),
],
),
),
),
),
);
}
_nombreNucleo()
return TextFormField(
textCapitalization: TextCapitalization.sentences,
decoration: InputDecoration(
labelText: 'Nombre núcleo',
icon: Icon(Icons.grass),
),
onSaved: (value) => prospect.nombreNucleo = value,
validator: (value) {
if (value.length < 5) {
return 'Ingresar nombre del núcleo minimo 5 letras';
} else {
return null;
}
},
);
}
Widget _botonSubmit() {
return RaisedButton.icon(
onPressed: _submit,
icon: Icon(Icons.save),
label: Text('Guardar'),
);
}
void _submit() async {
await DBProvider.db.newProspect(prospect);
print('se guardo el prospecto');
Navigator.pop(context);
}
void mostrarSnackbar(String mensaje) {
final snackbar = SnackBar(
content: Text(mensaje),
duration: Duration(milliseconds: 1500),
);
scaffolKey.currentState.showSnackBar(snackbar);
}
}
database:
class DBProvider {
static Database _database;
static final DBProvider db = DBProvider._();
DBProvider._();
Future<Database> get database async {
if (_database != null) return _database;
_database = await initDB();
return _database;
}
Future<Database> initDB() async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
final path = join(documentsDirectory.path, 'ProspectsDB.db');
print(path);
return await openDatabase(
path,
version: 1,
onOpen: (db) {},
onCreate: (Database db, int version) async {
//query
await db.execute(" CREATE TABLE Prospect("
"id INTEGER(11) PRIMARY KEY AUTOINCREMENT,"
"nombre_nucleo TEXT(255) NOT NULL,"
")");
},
);
}
Future<int> newProspect(ProspectModel newProspect) async {
final db = await database;
final resp = await db.insert("Prospect", newProspect.toJson());
print(resp);
return resp;
}
Future<List<ProspectModel>> getAllProspects() async {
final db = await database;
final resp = await db.query('Prospect');
return resp.isNotEmpty
? resp.map((p) => ProspectModel.fromJson(p)).toList()
: [];
}
Future<ProspectModel> getProsId(int id) async {
final db = await database;
final resp = await db.query('Prospect', where: 'id = ?', whereArgs: [id]);
return resp.isNotEmpty ? ProspectModel.fromJson(resp.first) : null;
}
Future<List<ProspectModel>> getProsForDate(int fecha_alta) async {
final db = await database;
final resp = await db.rawQuery('''
SELECT * FROM Prospect WHERE fecha_alta = '$fecha_alta'
''');
return resp.isNotEmpty
? resp.map((p) => ProspectModel.fromJson(p)).toList()
: [];
}
Future<int> updatePros(ProspectModel newProspect) async {
final db = await database;
final resp = await db.update('Prospect', newProspect.toJson(),
where: 'id = ?', whereArgs: [newProspect.id]);
return resp;
}
Future<int> deletePros(int id) async {
final db = await database;
final resp = await db.delete('Prospect', where: 'id = ?', whereArgs: [id]);
return resp;
}
}
Otra forma de insert:
Future<int> newProspect(ProspectModel newProspect) async {
final id = newProspect.id;
final nombreNucleo = newProspect.nombreNucleo;
//verificar la db
final db = await database;
final resp = await db.rawInsert(
'''
INSERT INTO Prospect( '$id', '$nombreNucleo')
''',
);
return resp;
}