• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps foreign key doesn't work

Rubinc

Lurker
Jun 25, 2015
1
0
i add a list of products with their own cost, when i finish i add the list into the prodotto table and the total cost of all the product into the spesa table.
the id of spesa is the foreign key in prodotto table.
if i want to retrieve the name, the cost and the id of spesa from the prodotto table, the foreign key is always 0.
this is my DataBaseHelper.java

Code:
public  class DataBaseHelper extends SQLiteOpenHelper {

public static final String TAG = "DBHelper";

// columns of the contatti table
public static final String TABLE_CONTACT = "contact";
public static final String KEY_CONTACTID = "_id";
public static final String KEY_NAME = "username";
public static final String KEY_PASSWORD = "password";


// columns of the spesa table
public static final String TABLE_SPESA = "spesa";
public static final String SPESA_ID ="_ids";
public static final String TOTALE = "totale";
public static final String COLUMN_CONTACT_ID = "contact_id";

// columns of the prodotti table
public static final String TABLE_PRODOTTO = "prodotto";
public static final String PRODOTTO_ID = "_idp";
public static final String NOME = "nome";
public static final String PREZZO = "prezzo";
public static final String COLUMN_SPESA_ID = "spesa_id";

private static final String DATABASE_NAME = "datab.db";
private static final int DATABASE_VERSION = 11;

// SQL statement of the companies table creation
private static final String SQL_CREATE_TABLE_CONTACTS = "CREATE TABLE " + TABLE_CONTACT + "("
         + KEY_CONTACTID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
         + KEY_NAME + " TEXT NOT NULL, "
         + KEY_PASSWORD + " TEXT NOT NULL "
         +");";

// SQL statement of the spesa table creation
private static final String SQL_CREATE_TABLE_SPESA = "CREATE TABLE " + TABLE_SPESA + "("
+ SPESA_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ TOTALE + " INTEGER NOT NULL, "
         + COLUMN_CONTACT_ID + " INTEGER, "
         + " FOREIGN KEY (" + COLUMN_CONTACT_ID + ") REFERENCES "+TABLE_CONTACT+" ("+KEY_CONTACTID+"));";


// SQL statement of the costo table creation
private static final String SQL_CREATE_TABLE_PRODOTTO = "CREATE TABLE " + TABLE_PRODOTTO + "("
         + PRODOTTO_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
         + NOME + " TEXT NOT NULL,"
         + PREZZO + " TEXT NOT NULL, "
         + COLUMN_SPESA_ID + " INTEGER, "
         + " FOREIGN KEY (" + COLUMN_SPESA_ID + ") REFERENCES "+TABLE_SPESA+" ("+SPESA_ID+"));";


public DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase database) {
     database.execSQL(SQL_CREATE_TABLE_CONTACTS);
database.execSQL(SQL_CREATE_TABLE_SPESA);
  database.execSQL(SQL_CREATE_TABLE_PRODOTTO);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG,
         "Upgrading the database from version " + oldVersion + " to " + newVersion);
// clear all data
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACT);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SPESA);
  db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODOTTO);

// recreate the tables
onCreate(db);
}

    @Override
    public void onConfigure(SQLiteDatabase db) {
        db.setForeignKeyConstraintsEnabled(true);
    }

public DataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
}

Storico.java( the code used to retrieve values from the db)
Code:
public class Storico extends ListActivity {

    private ArrayList<String> results = new ArrayList<String>();
    private String tableName = DataBaseHelper.TABLE_PRODOTTO;
    private String tablename2= DataBaseHelper.TABLE_SPESA;
    private SQLiteDatabase newDB;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        openAndQueryDatabase();

        displayResultList();

    }
    private void displayResultList() {
        TextView tView = new TextView(this);
        tView.setText("Elenco dei prodotti comprati");
        getListView().addHeaderView(tView);

        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, results));
        getListView().setTextFilterEnabled(true);

    }
    private void openAndQueryDatabase() {
        try {
            DataBaseHelper dbHelper = new DataBaseHelper(this.getApplicationContext());
            newDB = dbHelper.getWritableDatabase();
            Cursor c = newDB.rawQuery("SELECT nome,prezzo,spesa_id FROM " +
                    tableName, null);

            if (c != null ) {
                if  (c.moveToFirst()) {
                    do {
                        String firstName = c.getString(c.getColumnIndex("nome"));
                        Integer costo = c.getInt(c.getColumnIndex("prezzo"));
                        //Integer id= c.getInt(c.getColumnIndex("_ids"));
                        Integer ids= c.getInt(c.getColumnIndex("spesa_id"));
                        results.add("nome: " + firstName + "costo: " + costo + "foreign: " +ids);
                    }while (c.moveToNext());
                }
            }
        } catch (SQLiteException se ) {
            Log.e(getClass().getSimpleName(), "Could not create or Open the database");
        } finally {
            newDB.close();
        }

    }

}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones