DatabaseHelper class:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String BANCO_DADOS = "MakeRequestApp";
private static int VERSAO = 1;
public DatabaseHelper(Context context){
super(context, BANCO_DADOS, null, VERSAO);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL("CREATE TABLE mesa (_id INTEGER PRIMARY KEY, descricao TEXT)");
db.execSQL("CREATE TABLE pedido(_id INTEGER PRIMARY KEY, numero_mesa_id INTEGER, " +
"FOREIGN KEY(numero_mesa_id) REFERENCES mesa(_id))");
db.execSQL("CREATE TABLE itens_pedido (_id INTEGER PRIMARY KEY, produto_id INTEGER, quantidade INTEGER," +
"FOREIGN KEY(produto_id) REFERENCES produto(_id))");
db.execSQL("CREATE TABLE pedido(_id INTEGER PRIMARY KEY, numero_mesa_id INTEGER)" +
"FOREIGN KEY(numero_mesa_id) REFERENCES mesa(_id)");
db.execSQL("CREATE TABLE produto (_id INTEGER PRIMARY KEY, descricao TEXT, valor DOUBLE)");
}
My Activity Class:
public class MesaActivity extends AppCompatActivity {
private DatabaseHelper helper;
private EditText descricao;
public static final String MESA_ID = "_id";
private String id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mesa);
descricao = (EditText)findViewById(R.id.edtDescricao);
helper = new DatabaseHelper(this);
id = getIntent().getStringExtra(MESA_ID);
if (id != null){
prepararEdicao();
}
}
private void prepararEdicao(){
}
@Override
protected void onDestroy(){
helper.close();
super.onDestroy();
}
public void salvarMesa(View view){
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("descricao", descricao.getText().toString());
long resultado;
if (id == null){
resultado = db.insert("mesa", null, values);
} else {
resultado = db.update("mesa", values, "_ID= ?", new String[]{id});
}
if (resultado != -1){
Toast.makeText(this, "Dados salvos com sucesso!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Erro ao salvar dados!", Toast.LENGTH_SHORT).show();
}
}
When trying to save data it gives the following error:
E/SQLiteLog: (1) no such table: mesa
E/SQLiteDatabase: Error inserting descricao=8
android.database.sqlite.SQLiteException: no such table: mesa (code 1): , while compiling: INSERT INTO mesa(descricao) VALUES (?)
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(no such table: mesa (code 1): , while compiling: INSERT INTO mesa(descricao) VALUES (?))