2
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)");
}
Class of my Activity:
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();
}
}
While trying to save data 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 (?))
Change the value of VERSION to 2(
private static int VERSAO = 2;
) and test again.– ramaral
I believe it will still cause problems if it was an addition. You need to create a migration script to not try to generate tables that already exist.
– Wakim
@Wakim You’re right, AP does not show the implementation of the method
onUpgrade()
. I assumed that the drop of all tables.– ramaral
Has my table not been created?
– user31040
You changed the version number as I suggested?
– ramaral
I tested your code and the only mistake you made was table pedido already exists. In fact you are creating the "request" table twice, see on
onCreate()
of the Databasehelper.– ramaral